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