From 62f185fe76ea97bbc2914c28a47a952fa07e6b6b Mon Sep 17 00:00:00 2001 From: Akshay Date: Fri, 13 Mar 2020 21:37:03 +0530 Subject: begin work on attr macros --- src/lib.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/lib.rs (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..bd6ed2b --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,57 @@ +extern crate proc_macro; + +use std::path::{Path, PathBuf}; + +use proc_macro::TokenStream; +use quote::quote; +use syn::{parse_macro_input, AttrStyle, Attribute, DeriveInput, Lit, Meta, MetaNameValue}; + +use serde::{de::DeserializeOwned, Serialize}; + +trait Config +where + T: Serialize + DeserializeOwned + Default, +{ + fn load() -> Result; + fn store() -> Result<(), String>; +} + +fn is_outer_attribute(a: &Attribute) -> bool { + match a.style { + AttrStyle::Outer => true, + _ => false, + } +} + +#[proc_macro_derive(Config, attributes(filename, filetype))] +pub fn config_attribute(item: TokenStream) -> TokenStream { + let mut ast: DeriveInput = syn::parse(item).unwrap(); + + let mut filename: Option = None; + let mut filetype: Option = None; + + for option in ast.attrs.into_iter() { + let option = option.parse_meta().unwrap(); + match option { + Meta::NameValue(MetaNameValue { + ref path, ref lit, .. + }) if path.is_ident("filename") => { + if let Lit::Str(f) = lit { + filename = Some(PathBuf::from(f.value())); + } + } + Meta::NameValue(MetaNameValue { + ref path, ref lit, .. + }) if path.is_ident("filetype") => { + if let Lit::Str(f) = lit { + filetype = Some(PathBuf::from(f.value())); + } + } + _ => {} + } + } + + println!("{:?} {:?}", filename, filetype); + + TokenStream::new() +} -- cgit v1.2.3