1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use std::{io, result};

use err_derive::Error;
use uuid::Uuid;

use crate::{serializer, storage};

/// Repository error.
#[derive(Debug, Error)]
pub enum Error {
    /// Entity not found, so the requested operation could not be performed.
    #[error(display = "entity not found: {}", _0)]
    NotFound(Uuid),
    /// Serialization/deserialization error.
    #[error(display = "bad format: {}", _0)]
    Format(String),
    /// Io error (unexpected EOF, interrupted, ...).
    #[error(display = "io error: {}", _0)]
    Io(#[error(source)] io::Error),
    /// Entity doesn't have an id, and the requested operation could not be performed.
    #[error(display = "entity doesn't have an id")]
    Unidentified,
}

impl From<storage::Error> for Error {
    fn from(error: storage::Error) -> Self {
        match error {
            storage::Error::NotFound(e) => Self::NotFound(e),
            storage::Error::Format(e) => Self::Format(format!("{}", e)),
            storage::Error::Io(e) => Self::Io(e),
        }
    }
}

impl From<serializer::Error> for Error {
    fn from(error: serializer::Error) -> Self {
        match error {
            serializer::Error::Format(e) => Self::Format(e),
            serializer::Error::Io(e) => Self::Io(e),
        }
    }
}

/// Repository result.
pub type Result<T> = result::Result<T, Error>;