datarust_profile/
error.rs1use std::error::Error as StdError;
4use std::fmt;
5
6#[derive(Debug)]
8pub enum ProfileError {
9 InvalidInput(String),
11 EmptyInput(String),
13 Datarust(datarust::error::DatarustError),
15 Io(std::io::Error),
17 #[cfg(feature = "serde")]
20 Serde(serde_json::Error),
21}
22
23impl fmt::Display for ProfileError {
24 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25 match self {
26 ProfileError::InvalidInput(s) => write!(f, "invalid input: {}", s),
27 ProfileError::EmptyInput(s) => write!(f, "empty input: {}", s),
28 ProfileError::Datarust(e) => write!(f, "datarust error: {}", e),
29 ProfileError::Io(e) => write!(f, "io error: {}", e),
30 #[cfg(feature = "serde")]
31 ProfileError::Serde(e) => write!(f, "serialization error: {}", e),
32 }
33 }
34}
35
36impl StdError for ProfileError {
37 fn source(&self) -> Option<&(dyn StdError + 'static)> {
38 match self {
39 ProfileError::Datarust(e) => Some(e),
40 ProfileError::Io(e) => Some(e),
41 #[cfg(feature = "serde")]
42 ProfileError::Serde(e) => Some(e),
43 _ => None,
44 }
45 }
46}
47
48impl From<datarust::error::DatarustError> for ProfileError {
49 fn from(e: datarust::error::DatarustError) -> Self {
50 ProfileError::Datarust(e)
51 }
52}
53
54impl From<std::io::Error> for ProfileError {
55 fn from(e: std::io::Error) -> Self {
56 ProfileError::Io(e)
57 }
58}
59
60#[cfg(feature = "serde")]
61impl From<serde_json::Error> for ProfileError {
62 fn from(e: serde_json::Error) -> Self {
63 ProfileError::Serde(e)
64 }
65}
66
67pub type Result<T> = std::result::Result<T, ProfileError>;