From df30100e28a311402f4251db71aa43ea69f4222a Mon Sep 17 00:00:00 2001 From: zaaarf Date: Tue, 26 Nov 2024 20:17:50 +0100 Subject: [PATCH 1/3] feat: error as string --- src/lib.rs | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 8d4cbe6..1a3cad7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -137,7 +137,13 @@ mod attributes; /// match s { /// "One" => Ok(Self::One), /// "Two" => Ok(Self::Two), -/// _ => Err(()), +/// _ => { +/// let mut err_msg = "Failed parse string ".to_string(); +/// err_msg.push_str(s); +/// err_msg.push_str(" for enum "); +/// err_msg.push_str("Numbers"); +/// Err(err_msg) +/// } /// } /// } /// } @@ -207,14 +213,21 @@ fn impl_from_str( identifiers: &Vec<&syn::Ident>, names: &Vec, ) -> TokenStream { + let name_str = name.to_string(); let gen = quote! { impl TryFrom<&str> for #name { - type Error = (); + type Error = String; fn try_from(s: &str) -> Result { match s { #(#names => Ok(Self::#identifiers),)* - _ => Err(()), + _ => { + let mut err_msg = "Failed parse string ".to_string(); + err_msg.push_str(s); + err_msg.push_str(" for enum "); + err_msg.push_str(#name_str); + Err(err_msg) + } } } } @@ -227,7 +240,7 @@ fn impl_from_str( fn impl_from_string(name: &syn::Ident) -> TokenStream { let gen = quote! { impl TryFrom for #name { - type Error = (); + type Error = String; fn try_from(s: String) -> Result { s.as_str().try_into() @@ -242,7 +255,7 @@ fn impl_from_string(name: &syn::Ident) -> TokenStream { fn impl_from_str_trait(name: &syn::Ident) -> TokenStream { let gen = quote! { impl ::std::str::FromStr for #name { - type Err = (); + type Err = String; fn from_str(s: &str) -> Result { s.try_into() From fabf39cf258c4a41d93399a05db71cb57946f9c3 Mon Sep 17 00:00:00 2001 From: zaaarf Date: Thu, 28 Nov 2024 16:16:41 +0100 Subject: [PATCH 2/3] fix: update doctest error type --- src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 1a3cad7..3b45ce6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -131,7 +131,7 @@ mod attributes; /// } /// /// impl TryFrom<&str> for Numbers { -/// type Error = (); +/// type Error = String; /// /// fn try_from(s: &str) -> Result { /// match s { @@ -149,7 +149,7 @@ mod attributes; /// } /// /// impl TryFrom for Numbers { -/// type Error = (); +/// type Error = String; /// /// fn try_from(s: String) -> Result { /// s.as_str().try_into() @@ -157,7 +157,7 @@ mod attributes; /// } /// /// impl ::std::str::FromStr for Numbers { -/// type Err = (); +/// type Err = String; /// /// fn from_str(s: &str) -> Result { /// s.try_into() From e29c8507b74fb247f2b5b954fdad59c06ddc6958 Mon Sep 17 00:00:00 2001 From: zaaarf Date: Thu, 28 Nov 2024 16:21:56 +0100 Subject: [PATCH 3/3] fix: latest version of clippy doesn't want spaces between doc and fn --- src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 3b45ce6..5d9646b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -164,7 +164,6 @@ mod attributes; /// } /// } /// ``` - #[proc_macro_derive(EnumStringify, attributes(enum_stringify))] pub fn enum_stringify(input: TokenStream) -> TokenStream { let ast = parse_macro_input!(input as DeriveInput);