diff --git a/error_set/tests/mod.rs b/error_set/tests/mod.rs index e0a332a..b5067ee 100644 --- a/error_set/tests/mod.rs +++ b/error_set/tests/mod.rs @@ -494,6 +494,7 @@ pub mod error_struct_and_enums { #[test] fn test() { let x: AuthError = AuthError::UserDoesNotExist1 { + /// This is a doc comment name: "john".to_string(), role: 30, }; diff --git a/error_set_impl/src/ast.rs b/error_set_impl/src/ast.rs index fde5be7..d2b118e 100644 --- a/error_set_impl/src/ast.rs +++ b/error_set_impl/src/ast.rs @@ -649,18 +649,30 @@ fn extract_cfg(attributes: Vec) -> (Vec, Vec) { (attributes, cfgs) } -#[derive(Clone, PartialEq)] +#[derive(Clone)] pub(crate) struct AstInlineErrorVariantField { + pub(crate) attributes: Vec, pub(crate) name: Ident, pub(crate) r#type: syn::Type, } impl Parse for AstInlineErrorVariantField { fn parse(input: ParseStream) -> Result { + let attributes = input.call(Attribute::parse_outer)?; let name: Ident = input.parse()?; let _: syn::Token![:] = input.parse()?; let r#type: syn::Type = input.parse()?; - Ok(AstInlineErrorVariantField { name, r#type }) + Ok(AstInlineErrorVariantField { + attributes, + name, + r#type, + }) + } +} + +impl PartialEq for AstInlineErrorVariantField { + fn eq(&self, other: &Self) -> bool { + self.name == other.name && self.r#type == other.r#type } } diff --git a/error_set_impl/src/expand.rs b/error_set_impl/src/expand.rs index 50b3d84..3b59e6b 100644 --- a/error_set_impl/src/expand.rs +++ b/error_set_impl/src/expand.rs @@ -201,13 +201,14 @@ fn add_enum(error_enum_node: &ErrorEnumGraphNode, token_stream: &mut TokenStream let cfg_attributes = &r#struct.cfg_attributes; let name = &r#struct.name; let fields = &r#struct.fields; + let field_attributes = fields.iter().map(|e| &e.attributes); let field_names = fields.iter().map(|e| &e.name); let field_types = fields.iter().map(|e| &e.r#type); error_variant_tokens.append_all(quote::quote! { #(#cfg_attributes)* #(#attributes)* #name { - #(#field_names : #field_types),* + #(#(#field_attributes)* #field_names : #field_types),* }, }); } @@ -216,6 +217,7 @@ fn add_enum(error_enum_node: &ErrorEnumGraphNode, token_stream: &mut TokenStream let cfg_attributes = &source_struct.cfg_attributes; let name = &source_struct.name; let fields = &source_struct.fields; + let field_attributes = fields.iter().map(|e| &e.attributes); let field_names = fields.iter().map(|e| &e.name); let field_types = fields.iter().map(|e| &e.r#type); let source_type = &source_struct.source_type; @@ -224,7 +226,7 @@ fn add_enum(error_enum_node: &ErrorEnumGraphNode, token_stream: &mut TokenStream #(#attributes)* #name { source: #source_type, - #(#field_names : #field_types),* + #(#(#field_attributes)* #field_names : #field_types),* }, }); } diff --git a/error_set_impl/src/resolve.rs b/error_set_impl/src/resolve.rs index b644188..036454d 100644 --- a/error_set_impl/src/resolve.rs +++ b/error_set_impl/src/resolve.rs @@ -371,6 +371,7 @@ fn replace_generics_in_fields( if old_to_new.contains_key(&field.r#type) { let new_type = old_to_new.get(&field.r#type).unwrap().clone(); return AstInlineErrorVariantField { + attributes: field.attributes.clone(), name: field.name.clone(), r#type: new_type.clone(), }; @@ -384,6 +385,7 @@ fn replace_generics_in_fields( let new_type = syn::parse_str::(&replaced) .expect("Failed to parse replaced type back into type"); return AstInlineErrorVariantField { + attributes: field.attributes.clone(), name: field.name.clone(), r#type: new_type.clone(), };