ssh_agent/proto/
error.rs

1use std::{string, io};
2use std::error::Error;
3use std::fmt::Display;
4
5#[derive(Debug)]
6pub enum ProtoError {
7    UnexpectedVariant,
8    MessageTooLong,
9    StringEncoding(string::FromUtf8Error),
10    IO(io::Error),
11    Serialization(String),
12    Deserialization(String)
13}
14
15impl From<ProtoError> for () {
16    fn from(_e: ProtoError) -> () {
17        ()
18    }
19}
20
21impl From<io::Error> for ProtoError {
22    fn from(e: io::Error) -> ProtoError {
23        ProtoError::IO(e)
24    }
25}
26
27impl From<string::FromUtf8Error> for ProtoError {
28    fn from(e: string::FromUtf8Error) -> ProtoError {
29        ProtoError::StringEncoding(e)
30    }
31}
32
33impl serde::ser::Error for ProtoError {
34    fn custom<T: Display>(msg: T) -> Self {
35        ProtoError::Serialization(msg.to_string())
36    }
37}
38
39impl serde::de::Error for ProtoError {
40    fn custom<T: Display>(msg: T) -> Self {
41        ProtoError::Deserialization(msg.to_string())
42    }
43}
44
45impl std::error::Error for ProtoError {
46    fn source(&self) -> Option<&(dyn Error + 'static)> {
47        match self {
48            ProtoError::UnexpectedVariant => None,
49            ProtoError::MessageTooLong => None,
50            ProtoError::StringEncoding(e) => Some(e),
51            ProtoError::IO(e) => Some(e),
52            ProtoError::Serialization(_) => None,
53            ProtoError::Deserialization(_) => None
54        }
55    }
56}
57
58impl std::fmt::Display for ProtoError {
59    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
60        match self {
61            ProtoError::UnexpectedVariant => f.write_str("Unexpected variant"),
62            ProtoError::MessageTooLong => f.write_str("Message too long"),
63            ProtoError::StringEncoding(_) => f.write_str("String encoding failed"),
64            ProtoError::IO(_) => f.write_str("I/O Error"),
65            ProtoError::Serialization(_) => f.write_str("Serialization Error"),
66            ProtoError::Deserialization(_) => f.write_str("Deserialization Error")
67        }
68    }
69}
70
71pub type ProtoResult<T> = Result<T, ProtoError>;