Skip to content

Commit

Permalink
add some doc
Browse files Browse the repository at this point in the history
  • Loading branch information
ccqpein committed Jul 16, 2023
1 parent 8e48e98 commit 34d36c8
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 86 deletions.
2 changes: 1 addition & 1 deletion cl-format-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
167 changes: 83 additions & 84 deletions cl-format-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ impl TildeAble for String {
}
trait TildeKindChar {
fn format(&self, tkind: &TildeKind) -> Result<Option<String>, Box<dyn std::error::Error>> {
fn format(&self, tkind: &TildeKind, buf: &mut String) -> Result<(), TildeError> {
Err("un-implenmented yet".into())
}
}
trait TildeKindVa {
fn format(&self, tkind: &TildeKind) -> Result<Option<String>, Box<dyn std::error::Error>> {
fn format(&self, tkind: &TildeKind, buf: &mut String) -> Result<(), TildeError> {
Err("un-implenmented yet".into())
}
}
Expand All @@ -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();
Expand Down Expand Up @@ -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<Option<String>, 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<Option<String>, 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<Item = Ident> + '_) {
Expand Down
2 changes: 1 addition & 1 deletion cl-format/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }

0 comments on commit 34d36c8

Please sign in to comment.