diff options
-rw-r--r-- | src/lib.rs | 70 |
1 files changed, 18 insertions, 52 deletions
@@ -1,57 +1,23 @@ | |||
1 | extern crate proc_macro; | ||
2 | |||
3 | use std::path::{Path, PathBuf}; | 1 | use std::path::{Path, PathBuf}; |
4 | 2 | ||
5 | use proc_macro::TokenStream; | 3 | pub use serde::{de::DeserializeOwned, Serialize}; |
6 | use quote::quote; | 4 | pub use directories::ProjectDirs; |
7 | use syn::{parse_macro_input, AttrStyle, Attribute, DeriveInput, Lit, Meta, MetaNameValue}; | 5 | pub use toml; |
8 | 6 | pub use serde_json; | |
9 | use serde::{de::DeserializeOwned, Serialize}; | 7 | pub use serde_yaml; |
10 | 8 | ||
11 | trait Config<T> | 9 | #[derive(Debug)] |
12 | where | 10 | pub enum FondantError { |
13 | T: Serialize + DeserializeOwned + Default, | 11 | InvalidHomeDir, |
14 | { | 12 | ConfigParseError, |
15 | fn load() -> Result<T, String>; | 13 | DirCreateErr(std::io::Error), |
16 | fn store() -> Result<(), String>; | 14 | LoadError, |
17 | } | 15 | FileWriteError, |
18 | 16 | FileReadError, | |
19 | fn is_outer_attribute(a: &Attribute) -> bool { | 17 | FileOpenError, |
20 | match a.style { | ||
21 | AttrStyle::Outer => true, | ||
22 | _ => false, | ||
23 | } | ||
24 | } | 18 | } |
25 | 19 | ||
26 | #[proc_macro_derive(Config, attributes(filename, filetype))] | 20 | pub trait Config: Serialize + DeserializeOwned + Default { |
27 | pub fn config_attribute(item: TokenStream) -> TokenStream { | 21 | fn load() -> Result<Self, FondantError>; |
28 | let mut ast: DeriveInput = syn::parse(item).unwrap(); | 22 | fn store(&self) -> Result<(), FondantError>; |
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 | } | 23 | } |