tor_client_lib/
error.rs

1use tokio_util::codec::LinesCodecError;
2
3/// Error type returned by this library
4#[derive(Debug)]
5pub enum TorError {
6    /// Authentication error
7    AuthenticationError(String),
8
9    /// General protocol error
10    ProtocolError(String),
11
12    /// I/O Error
13    IOError(std::io::Error),
14}
15
16impl std::fmt::Display for TorError {
17    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
18        match self {
19            Self::AuthenticationError(error) => write!(f, "Authentication Error: {}", error),
20            Self::ProtocolError(error) => write!(f, "Protocol Error: {}", error),
21            Self::IOError(error) => write!(f, "IO Error: {}", error),
22        }
23    }
24}
25
26impl std::error::Error for TorError {}
27
28impl TorError {
29    pub fn authentication_error(msg: &str) -> TorError {
30        TorError::AuthenticationError(msg.to_string())
31    }
32
33    pub fn protocol_error(msg: &str) -> TorError {
34        TorError::ProtocolError(msg.to_string())
35    }
36}
37
38impl From<std::io::Error> for TorError {
39    fn from(error: std::io::Error) -> TorError {
40        TorError::IOError(error)
41    }
42}
43
44impl From<LinesCodecError> for TorError {
45    fn from(error: LinesCodecError) -> TorError {
46        match error {
47            LinesCodecError::MaxLineLengthExceeded => TorError::ProtocolError(error.to_string()),
48            LinesCodecError::Io(error) => error.into(),
49        }
50    }
51}