cuddle-v2/crates/cuddle-actions-sdk-derive/src/lib.rs

59 lines
1.5 KiB
Rust
Raw Normal View History

use darling::FromMeta;
use proc_macro::TokenStream;
use syn::{parse_macro_input, ItemImpl};
extern crate proc_macro;
mod args {
use darling::FromMeta;
#[derive(FromMeta, Default)]
#[darling(default)]
pub struct Object {}
}
mod actions {
use proc_macro::TokenStream;
use syn::ItemImpl;
use crate::{args, utils::GeneratorResult};
pub fn generate(
object_args: &args::Object,
item_impl: &mut ItemImpl,
) -> GeneratorResult<TokenStream> {
todo!()
}
}
mod utils {
pub type GeneratorResult<T> = std::result::Result<T, syn::Error>;
}
macro_rules! parse_nested_meta {
($ty:ty, $args:expr) => {{
let meta = match darling::ast::NestedMeta::parse_meta_list(proc_macro2::TokenStream::from(
$args,
)) {
Ok(v) => v,
Err(e) => {
return TokenStream::from(darling::Error::from(e).write_errors());
}
};
match <$ty>::from_list(&meta) {
Ok(object_args) => object_args,
Err(err) => return TokenStream::from(err.write_errors()),
}
}};
}
#[proc_macro_attribute]
#[allow(non_snake_case)]
pub fn Actions(args: TokenStream, input: TokenStream) -> TokenStream {
let object_args = parse_nested_meta!(args::Object, args);
let mut item_impl = parse_macro_input!(input as ItemImpl);
match actions::generate(&object_args, &mut item_impl) {
Ok(expanded) => expanded,
Err(err) => err.to_compile_error().into(),
}
}