From 34d36c84297d331bbb5e66c9243a43a28672cc3f Mon Sep 17 00:00:00 2001 From: ccQpein Date: Sat, 15 Jul 2023 21:12:21 -0400 Subject: [PATCH] add some doc --- cl-format-macros/Cargo.toml | 2 +- cl-format-macros/src/lib.rs | 167 ++++++++++++++++++------------------ cl-format/Cargo.toml | 2 +- 3 files changed, 85 insertions(+), 86 deletions(-) diff --git a/cl-format-macros/Cargo.toml b/cl-format-macros/Cargo.toml index 2e96ff1..c9bdcaf 100644 --- a/cl-format-macros/Cargo.toml +++ b/cl-format-macros/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cl-format-macros" -version = "0.1.3" +version = "0.1.4" edition = "2021" description = "Proc macro for cl-format" license = "MIT" diff --git a/cl-format-macros/src/lib.rs b/cl-format-macros/src/lib.rs index 2c0fa43..0fcdaf5 100644 --- a/cl-format-macros/src/lib.rs +++ b/cl-format-macros/src/lib.rs @@ -65,13 +65,13 @@ impl TildeAble for String { } trait TildeKindChar { - fn format(&self, tkind: &TildeKind) -> Result, Box> { + fn format(&self, tkind: &TildeKind, buf: &mut String) -> Result<(), TildeError> { Err("un-implenmented yet".into()) } } trait TildeKindVa { - fn format(&self, tkind: &TildeKind) -> Result, Box> { + fn format(&self, tkind: &TildeKind, buf: &mut String) -> Result<(), TildeError> { Err("un-implenmented yet".into()) } } @@ -90,7 +90,6 @@ use syn::{parse_macro_input, spanned::Spanned, Attribute, Data, DataEnum, Derive pub fn derive_tilde_able(input: TokenStream) -> TokenStream { let input = parse_macro_input!(input as DeriveInput); - //let mut all_methods_headers = vec![]; let mut return_types_traits = vec![]; let mut all_default_methods = vec![]; let mut types_impl_methods = HashMap::new(); @@ -167,89 +166,89 @@ pub fn derive_tilde_able(input: TokenStream) -> TokenStream { proc_macro2::TokenStream::from_iter(result.into_iter()).into() } -/// new macro for optimizing -/// Give different methods to trait rahter than all same format -#[proc_macro_derive(TildeAble2, attributes(implTo))] -pub fn derive_tilde_able_2(input: TokenStream) -> TokenStream { - let input = parse_macro_input!(input as DeriveInput); - - //let mut all_methods_headers = vec![]; - let mut return_types_traits = vec![]; - //let mut all_default_methods = vec![]; - //let mut types_impl_methods = HashMap::new(); - - match input.data { - Data::Enum(DataEnum { ref variants, .. }) => { - let all_vars = variants.iter().map(|var| parse_variant_attrs(var)); - - all_vars.for_each(|(field, _)| { - // let fname = Ident::new( - // &(String::from("into_tildekind_") + &field.to_lowercase()), - // Span::call_site(), - // ); - - let return_type = - Ident::new(&(String::from("TildeKind") + &field), Span::call_site()); - - // add default methods to TildeAble - // all_default_methods - // .push(quote! { - // fn #fname(&self) -> Option<&dyn #return_type> { - // None - // }}); - - // impl for types - // tys.for_each(|ty| { - // let en = types_impl_methods.entry(ty).or_insert(vec![]); - // en.push(quote! {fn #fname(&self) -> Option<&dyn #return_type> { - // Some(self) - // }}) - // }); - - // - let method_name = Ident::new(&(String::from("format_to_") + &field.to_lowercase()), Span::call_site()); - return_types_traits.push(quote! { - pub trait #return_type: Debug { //:= TODO: change this name - fn #method_name(&self, tkind: &TildeKind) -> Result, TildeError> { //:= TODO: also change this name - Err(TildeError::new(ErrorKind::EmptyImplenmentError, "haven't implenmented yet").into(),) - } - }}) - }); - } - _ => panic!("only support the enum"), - }; - - let mut result = vec![]; - - // trait TildeAble defination - // let tilde_able_trait = quote! { - // pub trait TildeAble:Debug { - // fn len(&self) -> usize; - // #(#all_default_methods)* - // } - // }; - - // let mut auto_impl_for_types = types_impl_methods - // .iter() - // .map(|(ty, methods)| { - // quote! { - // impl TildeAble for #ty { - // fn len(&self) -> usize { - // 1 - // } - // #(#methods)* - // } - // } - // }) - // .collect(); +// /// new macro for optimizing +// /// Give different methods to trait rahter than all same format +// #[proc_macro_derive(TildeAble2, attributes(implTo))] +// pub fn derive_tilde_able_2(input: TokenStream) -> TokenStream { +// let input = parse_macro_input!(input as DeriveInput); + +// //let mut all_methods_headers = vec![]; +// let mut return_types_traits = vec![]; +// //let mut all_default_methods = vec![]; +// //let mut types_impl_methods = HashMap::new(); + +// match input.data { +// Data::Enum(DataEnum { ref variants, .. }) => { +// let all_vars = variants.iter().map(|var| parse_variant_attrs(var)); + +// all_vars.for_each(|(field, _)| { +// // let fname = Ident::new( +// // &(String::from("into_tildekind_") + &field.to_lowercase()), +// // Span::call_site(), +// // ); + +// let return_type = +// Ident::new(&(String::from("TildeKind") + &field), Span::call_site()); + +// // add default methods to TildeAble +// // all_default_methods +// // .push(quote! { +// // fn #fname(&self) -> Option<&dyn #return_type> { +// // None +// // }}); + +// // impl for types +// // tys.for_each(|ty| { +// // let en = types_impl_methods.entry(ty).or_insert(vec![]); +// // en.push(quote! {fn #fname(&self) -> Option<&dyn #return_type> { +// // Some(self) +// // }}) +// // }); + +// // +// let method_name = Ident::new(&(String::from("format_to_") + &field.to_lowercase()), Span::call_site()); +// return_types_traits.push(quote! { +// pub trait #return_type: Debug { //:= TODO: change this name +// fn #method_name(&self, tkind: &TildeKind) -> Result, TildeError> { //:= TODO: also change this name +// Err(TildeError::new(ErrorKind::EmptyImplenmentError, "haven't implenmented yet").into(),) +// } +// }}) +// }); +// } +// _ => panic!("only support the enum"), +// }; - // merge together - //result.push(tilde_able_trait); - //result.append(&mut auto_impl_for_types); - result.append(&mut return_types_traits); +// let mut result = vec![]; - proc_macro2::TokenStream::from_iter(result.into_iter()).into() -} +// // trait TildeAble defination +// // let tilde_able_trait = quote! { +// // pub trait TildeAble:Debug { +// // fn len(&self) -> usize; +// // #(#all_default_methods)* +// // } +// // }; + +// // let mut auto_impl_for_types = types_impl_methods +// // .iter() +// // .map(|(ty, methods)| { +// // quote! { +// // impl TildeAble for #ty { +// // fn len(&self) -> usize { +// // 1 +// // } +// // #(#methods)* +// // } +// // } +// // }) +// // .collect(); + +// // merge together +// //result.push(tilde_able_trait); +// //result.append(&mut auto_impl_for_types); +// result.append(&mut return_types_traits); + +// proc_macro2::TokenStream::from_iter(result.into_iter()).into() +// } /// return the field Ident and all types implTo. Empty if there is no implTo types fn parse_variant_attrs(variant: &Variant) -> (String, impl Iterator + '_) { diff --git a/cl-format/Cargo.toml b/cl-format/Cargo.toml index 04ac62d..b624b2f 100644 --- a/cl-format/Cargo.toml +++ b/cl-format/Cargo.toml @@ -14,4 +14,4 @@ readme = "../README.md" bench = false [dependencies] -cl-format-macros = { version = "0.1.3", path = "../cl-format-macros" } +cl-format-macros = { version = "0.1.4", path = "../cl-format-macros" }