minor cleanup with traits
This commit is contained in:
parent
73f9c95e37
commit
8a37a7b2da
17 changed files with 81 additions and 71 deletions
|
|
@ -1,6 +1,27 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
use thiserror::Error;
|
||||
|
||||
/// Extension trait for Option to convert to AppError::NotFound.
|
||||
///
|
||||
/// Reduces boilerplate when fetching entities that may not exist.
|
||||
///
|
||||
/// # Example
|
||||
/// ```rust
|
||||
/// use chattyness_error::OptionExt;
|
||||
///
|
||||
/// let scene = get_scene_by_id(&pool, id).await?.or_not_found("Scene")?;
|
||||
/// ```
|
||||
pub trait OptionExt<T> {
|
||||
/// Convert None to AppError::NotFound with a descriptive message.
|
||||
fn or_not_found(self, entity: &str) -> Result<T, AppError>;
|
||||
}
|
||||
|
||||
impl<T> OptionExt<T> for Option<T> {
|
||||
fn or_not_found(self, entity: &str) -> Result<T, AppError> {
|
||||
self.ok_or_else(|| AppError::NotFound(format!("{} not found", entity)))
|
||||
}
|
||||
}
|
||||
|
||||
/// Application error types for chattyness.
|
||||
///
|
||||
/// All errors derive From for automatic conversion where applicable.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue