From 0fdd4ff1c3c5e6d915fdd7c3278a7fe404f23904 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Lecrenier?= Date: Sat, 26 Dec 2020 18:15:31 +0100 Subject: [PATCH] Use explicit paths for types in generated code --- src/lib.rs | 162 ++++++++++++++++------------------------------------- 1 file changed, 49 insertions(+), 113 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 751d6ae..faf6dc0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,6 +39,10 @@ pub fn derive_from_toml(input: proc_macro::TokenStream) -> proc_macro::TokenStre fn derive_from_toml_struct(parsed_struct: parser::Struct, tb: &mut TokenBuilder) { let from_toml = ts!(":: decent_toml_rs_alternative :: FromToml"); + let toml_value = ts!(":: decent_toml_rs_alternative :: TomlValue"); + let option = ts!(":: std :: option :: Option"); + let some = ts!(option ":: Some"); + let none = ts!(option ":: None"); let mut where_clause_in_impl_from = parsed_struct.where_clause.clone().unwrap_or_else(|| WhereClause::default()); @@ -50,35 +54,13 @@ fn derive_from_toml_struct(parsed_struct: parser::Struct, tb: &mut TokenBuilder) }) } - /* - let mut field1 = None; - let mut field2 = None; - for (key, value) in o.iter() { - match key { - "field1" => { - field1 = Some(<_>::from_toml(value)?); - } - "field2" => { - field2 = Some(<_>::from_toml(value)?); - } - _ => { } - } - } - let field1 = field1?; - let field2 = field2?; - - Some ( Self { - field1: field1, - field2: field2, - } ) - */ extend_ts!(tb, "impl" parsed_struct.generics.removing_eq_type() from_toml "for" parsed_struct.ident parsed_struct.generics.removing_bounds_and_eq_type() where_clause_in_impl_from "{ - fn from_toml ( fromtoml : :: std :: option :: Option < & :: decent_toml_rs_alternative :: TomlValue > ) -> Option < Self > { - if let Some ( :: decent_toml_rs_alternative :: TomlValue :: Table ( o ) ) = fromtoml {" + fn from_toml ( fromtoml : " option " < & " toml_value " > ) -> " option " < Self > { + if let " some " ( " toml_value " :: Table ( o ) ) = fromtoml {" join_ts!(&parsed_struct.struct_fields, f, - "let mut" f.safe_ident() "= None ;" + "let mut" f.safe_ident() "= " none " ;" ) "for ( key , value ) in o . iter ( ) { match key . as_str ( ) { @@ -87,7 +69,7 @@ fn derive_from_toml_struct(parsed_struct: parser::Struct, tb: &mut TokenBuilder) let literal_key = Literal::string(&f.access().to_string()); ts!( literal_key " => {" - f.safe_ident() " = Some ( value ) ;" + f.safe_ident() " = " some " ( value ) ;" " }" ) @@ -100,13 +82,13 @@ fn derive_from_toml_struct(parsed_struct: parser::Struct, tb: &mut TokenBuilder) "let" f.safe_ident() "= < _ > :: from_toml ( " f.safe_ident() " ) ? ; " ) - "Some ( Self {" + some " ( Self {" join_ts!(&parsed_struct.struct_fields, f, f.access() ":" f.safe_ident() , separator: ",") "} )" "} else { - None + " none " } " "} @@ -114,17 +96,12 @@ fn derive_from_toml_struct(parsed_struct: parser::Struct, tb: &mut TokenBuilder) ) } -/* -{ - kind: "VariantA", - payload: { - ... - } -} -*/ - fn derive_from_toml_enum(parsed_enum: parser::Enum, tb: &mut TokenBuilder) { let from_toml = ts!(":: decent_toml_rs_alternative :: FromToml"); + let toml_value = ts!(":: decent_toml_rs_alternative :: TomlValue"); + let option = ts!(":: std :: option :: Option"); + let some = ts!(option ":: Some"); + let none = ts!(option ":: None"); let mut where_clause_in_impl_from = parsed_enum.where_clause.clone().unwrap_or_else(|| WhereClause::default()); @@ -142,15 +119,15 @@ fn derive_from_toml_enum(parsed_enum: parser::Enum, tb: &mut TokenBuilder) { extend_ts!(tb, "impl" parsed_enum.generics.removing_eq_type() from_toml "for" parsed_enum.ident parsed_enum.generics.removing_bounds_and_eq_type() where_clause_in_impl_from "{ - fn from_toml ( fromtoml : Option < & decent_toml_rs_alternative :: TomlValue > ) -> Option < Self > { - if let Some ( :: decent_toml_rs_alternative :: TomlValue :: Table ( table ) ) = fromtoml { - if ! table . contains_key ( " kind_literal " ) { return None } + fn from_toml ( fromtoml : " option " < & " toml_value " > ) -> " option " < Self > { + if let " some " ( " toml_value " :: Table ( table ) ) = fromtoml { + if ! table . contains_key ( " kind_literal " ) { return " none " } let kind = match & table [ " kind_literal " ] { - :: decent_toml_rs_alternative :: TomlValue :: String ( x ) => { + " toml_value " :: String ( x ) => { x . clone ( ) } _ => { - return None + return " none " } } ; match kind . as_str ( ) {" @@ -161,11 +138,11 @@ fn derive_from_toml_enum(parsed_enum: parser::Enum, tb: &mut TokenBuilder) { if let Some(fields) = item.get_struct_data().map(|d| d.1) { ts!( "if ! table . contains_key ( " payload_literal ") { - return None + return " none " } - if let :: decent_toml_rs_alternative :: TomlValue :: Table ( payload ) = & table [ " payload_literal " ] {" + if let " toml_value " :: Table ( payload ) = & table [ " payload_literal " ] {" join_ts!(fields, f, - "let mut" f.safe_ident() "= None ;" + "let mut" f.safe_ident() "= " none " ;" ) "for ( key , value ) in payload . iter ( ) { match key . as_str ( ) { @@ -174,7 +151,7 @@ fn derive_from_toml_enum(parsed_enum: parser::Enum, tb: &mut TokenBuilder) { let literal_key = Literal::string(&f.access().to_string()); ts!( literal_key " => {" - f.safe_ident() " = Some ( value ) ;" + f.safe_ident() " = " some " ( value ) ;" " }" ) @@ -186,63 +163,28 @@ fn derive_from_toml_enum(parsed_enum: parser::Enum, tb: &mut TokenBuilder) { join_ts!(fields, f, "let" f.safe_ident() "= < _ > :: from_toml ( " f.safe_ident() " ) ? ;" ) - "Some ( Self :: " item.ident " {" + some " ( Self :: " item.ident " {" join_ts!(fields, f, f.access() ":" f.safe_ident() , separator: ",") "} )" "} else { - return None + return " none " }" ) } else { ts!( - "Some ( Self :: " item.ident " )" + some " ( Self :: " item.ident " )" ) } "}" ) }) - "_ => { return None }" - // "Variant1" => { - // if !o.contains_key("payload") { - // return None - // } - // if let TomlValue::Table(payload) = o["payload"] { - // let mut field1 = None; - // let mut field2 = None; - // for (key, value) in payload.iter() { - // match key { - // "field1" => { - // field1 = Some(<_>::from_toml(value)?); - // } - // "field2" => { - // field2 = Some(<_>::from_toml(value)?); - // } - // _ => { } - // } - // } - // let field1 = field1?; - // let field2 = field2?; - - // Some ( Self::Variant1 { - // field1: field1, - // field2: field2, - // } ) - // } else { - // return None - // } - // } - // "Variant2" => { - // Some(Self::Variant2) - // } - // _ => { - // return None - // } - "} + "_ => { return " none " } + } } else { - return None + return " none " } } }" @@ -276,6 +218,10 @@ pub fn derive_to_toml(input: proc_macro::TokenStream) -> proc_macro::TokenStream fn derive_to_toml_struct(parsed_struct: parser::Struct, tb: &mut TokenBuilder) { let to_toml = ts!(":: decent_toml_rs_alternative :: ToToml"); + let toml_value = ts!(":: decent_toml_rs_alternative :: TomlValue"); + let option = ts!(":: std :: option :: Option"); + let hash_map = ts!(":: std :: collections :: HashMap"); + let some = ts!(option ":: Some"); let mut where_clause_in_impl_from = parsed_struct.where_clause.clone().unwrap_or_else(|| WhereClause::default()); @@ -286,33 +232,20 @@ fn derive_to_toml_struct(parsed_struct: parser::Struct, tb: &mut TokenBuilder) { rhs: to_toml.clone(), }) } - /* - let mut table = :: decent_toml_rs_alternative :: table :: Table :: new () ; - table . insert ( "field1" , self . field1 . to_toml ( ) ) ; - table . insert ( "field2" , self . field2 . to_toml ( ) ) ; - :: decent_toml_rs_alternative :: TomlValue :: Table ( table ) - */ - /* - let mut table = HashMap::new(); - if let Some(value) = value.to_toml() { - table.insert(key.clone(), value); - } - Some(TomlValue::Table(table)) - */ + extend_ts!(tb, "impl" parsed_struct.generics.removing_eq_type() to_toml "for" parsed_struct.ident parsed_struct.generics.removing_bounds_and_eq_type() where_clause_in_impl_from "{ - fn to_toml ( & self ) -> Option < :: decent_toml_rs_alternative :: TomlValue > { - let mut table = :: std :: collections :: HashMap :: < String , :: decent_toml_rs_alternative :: TomlValue > :: new ( ) ; + fn to_toml ( & self ) -> " option " < " toml_value " > { + let mut table = " hash_map " :: < String , " toml_value " > :: new ( ) ; " join_ts!(parsed_struct.struct_fields, f, " - if let Some ( " f.safe_ident() " ) = self . " f.access() " . to_toml ( ) { + if let " some " ( " f.safe_ident() " ) = self . " f.access() " . to_toml ( ) { table . insert ( " Literal::string(&f.access().to_string()) " . to_string ( ) , " f.safe_ident() ") ; }" ) - " - Some ( :: decent_toml_rs_alternative :: TomlValue :: Table ( table ) ) + some " ( " toml_value " :: Table ( table ) ) } }" ) @@ -320,6 +253,10 @@ fn derive_to_toml_struct(parsed_struct: parser::Struct, tb: &mut TokenBuilder) { fn derive_to_toml_enum(parsed_enum: parser::Enum, tb: &mut TokenBuilder) { let to_toml = ts!(":: decent_toml_rs_alternative :: ToToml"); + let toml_value = ts!(":: decent_toml_rs_alternative :: TomlValue"); + let option = ts!(":: std :: option :: Option"); + let some = ts!(option ":: Some"); + let hash_map = ts!(":: std :: collections :: HashMap"); let mut where_clause_in_impl_from = parsed_enum.where_clause.clone().unwrap_or_else(|| WhereClause::default()); @@ -335,28 +272,27 @@ fn derive_to_toml_enum(parsed_enum: parser::Enum, tb: &mut TokenBuilder) { "# [ allow ( non_shorthand_field_patterns ) ]" "impl" parsed_enum.generics.removing_eq_type() to_toml "for" parsed_enum.ident parsed_enum.generics.removing_bounds_and_eq_type() where_clause_in_impl_from "{ - fn to_toml ( & self ) -> Option < :: decent_toml_rs_alternative :: TomlValue > { - let mut table = :: std :: collections :: HashMap :: < String , :: decent_toml_rs_alternative :: TomlValue > :: new ( ) ; + fn to_toml ( & self ) -> " option " < " toml_value " > { + let mut table = " hash_map " :: < String , " toml_value " > :: new ( ) ; match self {" join_ts!(parsed_enum.items, item, item.pattern_match(&parsed_enum.ident, None) "=> {" - "table . insert ( " Literal::string("kind") " . to_string ( ) , :: decent_toml_rs_alternative :: TomlValue :: String (" Literal::string(&item.ident.to_string()) " . to_string ( ) ) ) ;" + "table . insert ( " Literal::string("kind") " . to_string ( ) , " toml_value " :: String (" Literal::string(&item.ident.to_string()) " . to_string ( ) ) ) ;" if let Some(fields) = item.get_struct_data().map(|d| d.1) { ts!(" - let mut payload = :: std :: collections :: HashMap :: < String , :: decent_toml_rs_alternative :: TomlValue > :: new ( ) ; + let mut payload = " hash_map " :: < String , " toml_value " > :: new ( ) ; " join_ts!(fields, f, - "if let Some ( " f.safe_ident() " ) = " f.safe_ident() " . to_toml ( ) { + "if let " some " ( " f.safe_ident() " ) = " f.safe_ident() " . to_toml ( ) { payload . insert ( " Literal::string(&f.access().to_string()) " . to_string ( ) , " f.safe_ident() ") ; }" ) - "table . insert ( " Literal::string("payload") " . to_string ( ) , :: decent_toml_rs_alternative :: TomlValue :: Table ( payload ) ) ; " + "table . insert ( " Literal::string("payload") " . to_string ( ) , " toml_value " :: Table ( payload ) ) ; " ) } else { ts!() } - " - Some ( :: decent_toml_rs_alternative :: TomlValue :: Table ( table ) ) + some " ( " toml_value " :: Table ( table ) ) }" ) "} -- 2.45.2