libime_history_merge/
error.rs

1use std::fmt::{self, Display};
2
3use serde::{de, ser};
4
5pub type Result<T> = std::result::Result<T, Error>;
6
7#[derive(Clone, Debug, PartialEq)]
8pub enum Error {
9    Message(String),
10    LogicError(String),
11    EofError,
12    IoError(String),
13    SerializeError(String),
14    DeserializeError(String),
15}
16
17impl Display for Error {
18    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19        match self {
20            Error::Message(msg) => f.write_str(msg),
21            Error::EofError => f.write_str("Unexpected EOF"),
22            Error::IoError(msg) => f.write_str(&format!("IO Error: {}", msg)),
23            Error::LogicError(msg) => f.write_str(&format!("Logic Error: {}", msg)),
24            Error::SerializeError(msg) => f.write_str(&format!("Serialize Error: {}", msg)),
25            Error::DeserializeError(msg) => f.write_str(&format!("Deserialize Error: {}", msg)),
26        }
27    }
28}
29
30impl std::error::Error for Error {}
31
32impl ser::Error for Error {
33    fn custom<T>(msg: T) -> Self
34    where
35        T: Display,
36    {
37        Error::SerializeError(msg.to_string())
38    }
39}
40
41impl de::Error for Error {
42    fn custom<T>(msg: T) -> Self
43    where
44        T: Display,
45    {
46        Error::DeserializeError(msg.to_string())
47    }
48}
49
50impl From<std::array::TryFromSliceError> for Error {
51    fn from(err: std::array::TryFromSliceError) -> Self {
52        Self::Message(err.to_string())
53    }
54}
55impl From<std::string::FromUtf8Error> for Error {
56    fn from(err: std::string::FromUtf8Error) -> Self {
57        Self::Message(err.to_string())
58    }
59}
60impl From<std::io::Error> for Error {
61    fn from(err: std::io::Error) -> Self {
62        Self::IoError(err.to_string())
63    }
64}
65
66// Author: Blurgy <gy@blurgy.xyz>
67// Date:   Feb 03 2022, 12:10 [CST]