1
pub use std::fs::OpenOptions;
2
use std::{borrow::Cow, error, fmt, io, str, sync};
3

4
use crate::id::PersyId;
5

6
/// Enum of possible errors from Persy
7 0
#[derive(Debug)]
8
pub enum PersyError {
9 0
    Io(io::Error),
10 0
    DecodingUtf8(str::Utf8Error),
11 0
    DecodingDataEncoding(data_encoding::DecodeError),
12 0
    Custom(Box<dyn error::Error + Send + Sync + 'static>),
13
    VersionNotLastest,
14 0
    RecordNotFound(PersyId),
15
    SegmentNotFound,
16
    SegmentAlreadyExists,
17
    CannotDropSegmentCreatedInTx,
18
    Lock,
19
    IndexMinElementsShouldBeAtLeastDoubleOfMax,
20
    IndexNotFound,
21 0
    IndexTypeMismatch(Cow<'static, str>),
22 0
    IndexDuplicateKey(String, String),
23
    TransactionTimeout,
24 0
    InvalidId(String),
25

26
    #[doc(hidden)]
27
    __NonExhausive,
28
}
29

30
pub type PRes<T> = Result<T, PersyError>;
31

32
impl<T> From<sync::PoisonError<T>> for PersyError {
33 0
    fn from(_: sync::PoisonError<T>) -> PersyError {
34 0
        PersyError::Lock
35 0
    }
36
}
37

38
impl From<io::Error> for PersyError {
39 1
    fn from(err: io::Error) -> PersyError {
40 1
        PersyError::Io(err)
41 1
    }
42
}
43

44
impl From<str::Utf8Error> for PersyError {
45 0
    fn from(err: str::Utf8Error) -> PersyError {
46 0
        PersyError::DecodingUtf8(err)
47 0
    }
48
}
49

50
impl From<data_encoding::DecodeError> for PersyError {
51 1
    fn from(err: data_encoding::DecodeError) -> PersyError {
52 1
        PersyError::DecodingDataEncoding(err)
53 1
    }
54
}
55

56
impl From<Box<dyn error::Error + Send + Sync + 'static>> for PersyError {
57
    fn from(err: Box<dyn error::Error + Send + Sync + 'static>) -> PersyError {
58
        PersyError::Custom(err)
59
    }
60
}
61

62
impl error::Error for PersyError {
63
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
64
        use PersyError::*;
65
        match self {
66
            Io(ref e) => Some(e),
67
            DecodingUtf8(ref e) => Some(e),
68
            DecodingDataEncoding(ref e) => Some(e),
69
            Custom(ref e) => Some(e.as_ref()),
70
            _ => None,
71
        }
72
    }
73
}
74

75
impl fmt::Display for PersyError {
76
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
77
        use PersyError::*;
78
        match self {
79
            Io(m) => write!(f, "IO Error: {}", m),
80
            DecodingUtf8(e) => write!(f, "String decoding error: {}", e),
81
            DecodingDataEncoding(e) => write!(f, "Data Encoding Decoding error: {}", e),
82
            Custom(e) => write!(f, "{}", e),
83
            VersionNotLastest => write!(f, "The record version is not latest"),
84
            RecordNotFound(r) => write!(f, "Record not found: {}", r),
85

86
            SegmentNotFound => write!(f, "Segment not found"),
87

88
            SegmentAlreadyExists => write!(f, "Segment already exist"),
89

90
            CannotDropSegmentCreatedInTx => {
91
                write!(f, "Create and drop of a segment in the same transaction is not allowed")
92
            }
93

94
            Lock => write!(f, "Failure acquiring lock for poisoning"),
95

96
            IndexMinElementsShouldBeAtLeastDoubleOfMax => write!(
97
                f,
98
                "Index min page elements should be maximum half of the maximum elements"
99
            ),
100

101
            IndexNotFound => write!(f, "Index not found"),
102
            IndexTypeMismatch(m) => write!(f, "Index method type mismatch persistent types: {}", m),
103

104
            IndexDuplicateKey(i, k) => write!(f, "Found duplicate key:{} for index: {}", k, i),
105
            TransactionTimeout => write!(f, "Timeout acquiring the data locks for the transaction"),
106
            InvalidId(id) => write!(f, "The id '{}' has no valid format", id),
107

108
            __NonExhausive => write!(f, "__non-Exhausive"),
109
        }
110
    }
111
}

Read our documentation on viewing source code .

Loading