Skip to content

Commit

Permalink
doc: Update
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmah309 committed Aug 21, 2024
1 parent 04d5e41 commit 906d801
Showing 1 changed file with 75 additions and 2 deletions.
77 changes: 75 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,6 @@ error_set! {
Any above subset can be converted into a superset with `.into()` or `?`.
This makes correctly scoping and passing around errors a breeze.
Error enums and error variants can also accept doc comments and attributes like `#[derive(...)]`.
The typical project approach is to have one `errors.rs` file with a single `error_set`. This keeps
all the errors in one place and allows your IDE to autocomplete `crate::errors::` with of all errors.

<details>

Expand Down Expand Up @@ -316,6 +314,81 @@ fn main() {
```
</details>


The typical project approach is to have one `errors.rs` file with a single `error_set`. This keeps
all the errors in one place and allows your IDE to autocomplete `crate::errors::` with of all errors.
But `error_set!` can also be used for quick errors "unions", no longer requiring users to
hand write `From<..>` or use `.map_err(..)` for these simple cases.
e.g.
```rust
impl FirebaseJwtVerifier {
pub async fn new(project_id: String) -> Result<Self, FirebaseJwtVerifierCreationError> {
let public_keys = Self::fetch_public_keys().await?;
let decoding_keys: Result<HashMap<String, DecodingKey>, _> = public_keys
.into_iter()
.map(|(key, value)| {
DecodingKey::from_rsa_pem(value.as_bytes()).map(|decoding_key| (key, decoding_key))
})
.collect();

let decoding_keys = decoding_keys?;
...
}
}
```rust
error_set! {
FirebaseJwtVerifierCreationError = {
Reqwest(reqwest::Error),
Jwt(jsonwebtoken::errors::Error),
};
}
```
<details>

<summary>Cargo Expand</summary>

```rust
#[derive(Debug)]
pub enum FirebaseJwtVerifierCreationError {
Reqwest(reqwest::Error),
Jwt(jsonwebtoken::errors::Error),
}
#[allow(unused_qualifications)]
impl std::error::Error for FirebaseJwtVerifierCreationError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match *self {
FirebaseJwtVerifierCreationError::Reqwest(ref source) => source.source(),
FirebaseJwtVerifierCreationError::Jwt(ref source) => source.source(),
#[allow(unreachable_patterns)]
_ => None,
}
}
}
impl core::fmt::Display for FirebaseJwtVerifierCreationError {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
let variant_name = match *self {
FirebaseJwtVerifierCreationError::Reqwest(_) => {
"FirebaseJwtVerifierCreationError::Reqwest"
}
FirebaseJwtVerifierCreationError::Jwt(_) => "FirebaseJwtVerifierCreationError::Jwt",
};
f.write_fmt($crate::format_args!("{}", variant_name))
}
}
impl From<reqwest::Error> for FirebaseJwtVerifierCreationError {
fn from(error: reqwest::Error) -> Self {
FirebaseJwtVerifierCreationError::Reqwest(error)
}
}
impl From<jsonwebtoken::errors::Error> for FirebaseJwtVerifierCreationError {
fn from(error: jsonwebtoken::errors::Error) -> Self {
FirebaseJwtVerifierCreationError::Jwt(error)
}
}
```
</details>

### Feature Flags

**coerce_macro:** Each error set will generates a `coerce!` macro to help handle coercion between partially intersecting sets.
Expand Down

0 comments on commit 906d801

Please sign in to comment.