Skip to content

Commit

Permalink
doc: Update
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmah309 committed Sep 10, 2024
1 parent acd01e4 commit 7153cb7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 16 deletions.
35 changes: 19 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -393,19 +393,18 @@ Error sets also supports inline structs and custom display messages. Similar to
add the `#[display(...)]` attribute to the variant.
```rust
error_set! {
SetX = {
#[display("My name is {} and my age is {}", name, age)]
A {
name: String,
age: u32,
},
#[display("This is the display message for B")]
B,
IoError(std::io::Error),
};
SetY = {
C,
} || SetX;
AuthError = {
#[display("User `{}` with role `{}` does not exist", name, role)]
UserDoesNotExist {
name: String,
role: u32,
},
#[display("The provided credentials are invalid")]
InvalidCredentials
};
LoginError = {
IoError(std::io::Error),
} || AuthError;
}
```
<details>
Expand All @@ -414,11 +413,15 @@ error_set! {

```rust
fn main() {
let x = SetX::A {
let x: AuthError = AuthError::UserDoesNotExist {
name: "john".to_string(),
age: 32,
role: 30,
};
assert_eq!(x.to_string(), "My name is john and my age is 32".to_string());
assert_eq!(x.to_string(), "User `john` with role `30` does not exist".to_string());
let y: LoginError = x.into();
assert_eq!(y.to_string(), "User `john` with role `30` does not exist".to_string());
let x = AuthError::InvalidCredentials;
assert_eq!(x.to_string(), "The provided credentials are invalid".to_string());
}
```

Expand Down
28 changes: 28 additions & 0 deletions tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,34 @@ pub mod value_variants {
"error `3` happened because `oops`".to_string()
);
}

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

#[test]
fn from_readme() {
let x: AuthError = AuthError::UserDoesNotExist {
name: "john".to_string(),
role: 30,
};
assert_eq!(x.to_string(), "User `john` with role `30` does not exist".to_string());
let y: LoginError = x.into();
assert_eq!(y.to_string(), "User `john` with role `30` does not exist".to_string());
let x = AuthError::InvalidCredentials;
assert_eq!(x.to_string(), "The provided credentials are invalid".to_string());
}
}

#[cfg(test)]
Expand Down

0 comments on commit 7153cb7

Please sign in to comment.