summaryrefslogtreecommitdiff
path: root/src/repo/error.rs
blob: a5961e2b798932e4d80663813d94e6e213dc47c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
pub trait NotFound {
    type Ok;
    fn not_found<E, F>(self, map: F) -> Result<Self::Ok, E>
    where
        E: From<sqlx::Error>,
        F: FnOnce() -> E;
}

impl<T> NotFound for Result<T, sqlx::Error> {
    type Ok = T;

    fn not_found<E, F>(self, map: F) -> Result<T, E>
    where
        E: From<sqlx::Error>,
        F: FnOnce() -> E,
    {
        match self {
            Err(sqlx::Error::RowNotFound) => Err(map()),
            Err(other) => Err(other.into()),
            Ok(value) => Ok(value),
        }
    }
}