Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions error_set/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down
16 changes: 14 additions & 2 deletions error_set_impl/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,18 +649,30 @@ fn extract_cfg(attributes: Vec<Attribute>) -> (Vec<Attribute>, Vec<Attribute>) {
(attributes, cfgs)
}

#[derive(Clone, PartialEq)]
#[derive(Clone)]
pub(crate) struct AstInlineErrorVariantField {
pub(crate) attributes: Vec<Attribute>,
pub(crate) name: Ident,
pub(crate) r#type: syn::Type,
}

impl Parse for AstInlineErrorVariantField {
fn parse(input: ParseStream) -> Result<Self> {
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
}
}

Expand Down
6 changes: 4 additions & 2 deletions error_set_impl/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),*
},
});
}
Expand All @@ -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;
Expand All @@ -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),*
},
});
}
Expand Down
2 changes: 2 additions & 0 deletions error_set_impl/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
};
Expand All @@ -384,6 +385,7 @@ fn replace_generics_in_fields(
let new_type = syn::parse_str::<syn::Type>(&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(),
};
Expand Down