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
46
47
48
49
50
51
52
53
use persy::{PersyError, PE};
use std::fmt::{Display, Formatter};
use std::{error::Error, io::Error as IOError, sync::PoisonError};
/// All the possible Structsy errors
#[derive(Debug)]
pub enum StructsyError {
    PersyError(PersyError),
    StructAlreadyDefined(String),
    StructNotDefined(String),
    IOError,
    PoisonedLock,
    MigrationNotSupported(String),
    InvalidId,
    ValueChangeError(String),
    TypeError(String),
}

impl<T: Into<PersyError>> From<PE<T>> for StructsyError {
    fn from(err: PE<T>) -> StructsyError {
        StructsyError::PersyError(err.error().into())
    }
}
impl<T> From<PoisonError<T>> for StructsyError {
    fn from(_err: PoisonError<T>) -> StructsyError {
        StructsyError::PoisonedLock
    }
}

impl From<IOError> for StructsyError {
    fn from(_err: IOError) -> StructsyError {
        StructsyError::IOError
    }
}

pub type SRes<T> = Result<T, StructsyError>;

impl Error for StructsyError {}

impl Display for StructsyError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            StructsyError::PersyError(pe) => write!(f, "Persy Error: {}", pe),
            StructsyError::StructAlreadyDefined(name) => write!(f, "Struct with name '{}' already defined ", name),
            StructsyError::StructNotDefined(name) => writeln!(f, "Struct with name '{}', not defined", name),
            StructsyError::IOError => writeln!(f, "IOError"),
            StructsyError::PoisonedLock => writeln!(f, "PoisonedLock"),
            StructsyError::MigrationNotSupported(name) => writeln!(f, "Migration of Struct '{}' not supported", name),
            StructsyError::InvalidId => writeln!(f, "Invalid ID"),
            StructsyError::ValueChangeError(message) => writeln!(f, "Value change: {}", message),
            StructsyError::TypeError(message) => writeln!(f, "Type Error : {}", message),
        }
    }
}