together_rs/
errors.rs

1use std::sync::mpsc;
2
3pub type TogetherResult<T> = std::result::Result<T, TogetherError>;
4
5#[derive(Debug)]
6pub enum TogetherError {
7    Io(std::io::Error),
8    Yaml(serde_yml::Error),
9    TomlSerialize(toml::ser::Error),
10    TomlDeserialize(toml::de::Error),
11    ChannelRecvError(mpsc::RecvError),
12    PopenErrorError(subprocess::PopenError),
13    InternalError(TogetherInternalError),
14    DynError(Box<dyn std::error::Error>),
15}
16
17#[derive(Debug)]
18pub enum TogetherInternalError {
19    ProcessFailedToExit,
20    UnexpectedResponse,
21    InvalidConfigExtension,
22}
23
24impl std::fmt::Display for TogetherError {
25    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
26        use TogetherInternalError as TIE;
27        match self {
28            TogetherError::Io(e) => write!(f, "IO error: {}", e),
29            TogetherError::Yaml(e) => write!(f, "YAML error: {}", e),
30            TogetherError::TomlSerialize(e) => write!(f, "TOML serialization error: {}", e),
31            TogetherError::TomlDeserialize(e) => write!(f, "TOML deserialization error: {}", e),
32            TogetherError::ChannelRecvError(e) => write!(f, "Channel receive error: {}", e),
33            TogetherError::PopenErrorError(e) => write!(f, "Process error: {}", e),
34            TogetherError::InternalError(TIE::ProcessFailedToExit) => {
35                write!(f, "Process failed to exit")
36            }
37            TogetherError::InternalError(TIE::UnexpectedResponse) => {
38                write!(f, "Unexpected response from process")
39            }
40            TogetherError::InternalError(TIE::InvalidConfigExtension) => {
41                write!(f, "Invalid configuration file extension")
42            }
43            TogetherError::DynError(e) => write!(f, "Error: {}", e),
44        }
45    }
46}
47
48impl std::error::Error for TogetherError {
49    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
50        match self {
51            TogetherError::Io(e) => Some(e),
52            TogetherError::Yaml(e) => Some(e),
53            TogetherError::TomlSerialize(e) => Some(e),
54            TogetherError::TomlDeserialize(e) => Some(e),
55            TogetherError::ChannelRecvError(e) => Some(e),
56            TogetherError::PopenErrorError(e) => Some(e),
57            TogetherError::InternalError(_) => None,
58            TogetherError::DynError(e) => Some(e.as_ref()),
59        }
60    }
61}
62
63impl From<std::io::Error> for TogetherError {
64    fn from(e: std::io::Error) -> Self {
65        TogetherError::Io(e)
66    }
67}
68
69impl From<serde_yml::Error> for TogetherError {
70    fn from(e: serde_yml::Error) -> Self {
71        TogetherError::Yaml(e)
72    }
73}
74
75impl From<toml::ser::Error> for TogetherError {
76    fn from(e: toml::ser::Error) -> Self {
77        TogetherError::TomlSerialize(e)
78    }
79}
80
81impl From<toml::de::Error> for TogetherError {
82    fn from(e: toml::de::Error) -> Self {
83        TogetherError::TomlDeserialize(e)
84    }
85}
86
87impl From<mpsc::RecvError> for TogetherError {
88    fn from(e: mpsc::RecvError) -> Self {
89        TogetherError::ChannelRecvError(e)
90    }
91}
92
93impl From<subprocess::PopenError> for TogetherError {
94    fn from(e: subprocess::PopenError) -> Self {
95        TogetherError::PopenErrorError(e)
96    }
97}
98
99impl From<TogetherInternalError> for TogetherError {
100    fn from(e: TogetherInternalError) -> Self {
101        TogetherError::InternalError(e)
102    }
103}
104
105impl From<Box<dyn std::error::Error>> for TogetherError {
106    fn from(e: Box<dyn std::error::Error>) -> Self {
107        TogetherError::DynError(e)
108    }
109}