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
use thiserror::Error;

#[derive(Error, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum PsDataChunkError {
    #[error(transparent)]
    PsCypherError(#[from] ps_cypher::PsCypherError),
    #[error("Failed to convert a hash to [u8; 50]")]
    HashConversionError,
    #[error("Failed to serialize into an AlignedDataChunk")]
    SerializationError,
    #[error("Failed to deserialize from an AlignedDataChunk")]
    DeserializationError,
    #[error("The data chunk was not correctly layed out")]
    InvalidDataChunk,
    #[error("The hash of a chunk was incorrect")]
    InvalidChecksum,
    #[error("This should never happen: {0}")]
    ShouldNotHaveFailed(&'static str),
}

impl PsDataChunkError {
    pub fn map_option<T>(item: Option<T>, error: PsDataChunkError) -> Result<T, Self> {
        match item {
            Some(value) => Ok(value),
            None => Err(error),
        }
    }
    pub fn map_result<T, E>(item: Result<T, E>, error: PsDataChunkError) -> Result<T, Self> {
        match item {
            Ok(value) => Ok(value),
            Err(_) => Err(error),
        }
    }
}