Skip to content

Commit

Permalink
feat: Support more types for inline structs
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmah309 committed Oct 28, 2024
1 parent 324c663 commit aa8ae29
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 23 deletions.
26 changes: 3 additions & 23 deletions impl/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,39 +385,19 @@ fn display_tokens(attribute: &Attribute) -> Option<DisplayAttribute> {
};
}

#[derive(Clone)]
#[derive(Clone, PartialEq)]
pub(crate) struct AstInlineErrorVariantField {
pub(crate) name: Ident,
pub(crate) r#type: syn::TypePath,
pub(crate) r#type: syn::Type,
}

impl Parse for AstInlineErrorVariantField {
fn parse(input: ParseStream) -> Result<Self> {
let name: Ident = input.parse()?;
let _: syn::Token![:] = input.parse()?;
let r#type: syn::TypePath = input.parse()?;
let r#type: syn::Type = input.parse()?;
Ok(AstInlineErrorVariantField { name, r#type })
}
}

impl PartialEq for AstInlineErrorVariantField {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
&& self
.r#type
.path
.segments
.iter()
.map(|e| &e.ident)
.collect::<Vec<_>>()
== other
.r#type
.path
.segments
.iter()
.map(|e| &e.ident)
.collect::<Vec<_>>()
}
}

impl Eq for AstInlineErrorVariantField {}
41 changes: 41 additions & 0 deletions tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,47 @@ pub mod display_ref_error {
}
}

#[cfg(test)]
pub mod fields_with_unique_types {
use error_set::error_set;

error_set! {
AuthError = {
#[display("User `{name}` with role `{}` does not exist", role.1)]
UserDoesNotExist1 {
name: &'static str,
role: (u32,String),
},
UserDoesNotExist2 {
name: String,
role: u32,
},
#[display("The provided credentials are invalid")]
InvalidCredentials
};
LoginError = {
IoError(std::io::Error),
} || AuthError;
}

#[test]
fn test() {
let x: AuthError = AuthError::UserDoesNotExist1 {
name: "john",
role: (30, "1".to_string()),
};
assert_eq!(
x.to_string(),
"User `john` with role `1` does not exist".to_string()
);
let y: LoginError = x.into();
assert_eq!(
y.to_string(),
"User `john` with role `1` does not exist".to_string()
);
}
}

#[cfg(test)]
pub mod should_not_compile_tests {

Expand Down

0 comments on commit aa8ae29

Please sign in to comment.