aboutsummaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs57
1 files changed, 57 insertions, 0 deletions
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 @@
1extern crate proc_macro;
2
3use std::path::{Path, PathBuf};
4
5use proc_macro::TokenStream;
6use quote::quote;
7use syn::{parse_macro_input, AttrStyle, Attribute, DeriveInput, Lit, Meta, MetaNameValue};
8
9use serde::{de::DeserializeOwned, Serialize};
10
11trait Config<T>
12where
13 T: Serialize + DeserializeOwned + Default,
14{
15 fn load() -> Result<T, String>;
16 fn store() -> Result<(), String>;
17}
18
19fn is_outer_attribute(a: &Attribute) -> bool {
20 match a.style {
21 AttrStyle::Outer => true,
22 _ => false,
23 }
24}
25
26#[proc_macro_derive(Config, attributes(filename, filetype))]
27pub fn config_attribute(item: TokenStream) -> TokenStream {
28 let mut ast: DeriveInput = syn::parse(item).unwrap();
29
30 let mut filename: Option<PathBuf> = None;
31 let mut filetype: Option<PathBuf> = None;
32
33 for option in ast.attrs.into_iter() {
34 let option = option.parse_meta().unwrap();
35 match option {
36 Meta::NameValue(MetaNameValue {
37 ref path, ref lit, ..
38 }) if path.is_ident("filename") => {
39 if let Lit::Str(f) = lit {
40 filename = Some(PathBuf::from(f.value()));
41 }
42 }
43 Meta::NameValue(MetaNameValue {
44 ref path, ref lit, ..
45 }) if path.is_ident("filetype") => {
46 if let Lit::Str(f) = lit {
47 filetype = Some(PathBuf::from(f.value()));
48 }
49 }
50 _ => {}
51 }
52 }
53
54 println!("{:?} {:?}", filename, filetype);
55
56 TokenStream::new()
57}