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 --- Cargo.toml | 22 ++++++++++++++++++++++ src/lib.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 Cargo.toml create mode 100644 src/lib.rs diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..3fa7044 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "confondant" +version = "0.1.0" +authors = ["Akshay "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +quote = "1.0" +toml = "^0.5" + +[dependencies.serde] +version = "1.0.103" +features = ["derive"] + +[dependencies.syn] +version = "1.0" +features = ["full"] + +[lib] +proc-macro = true 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