1use std::fmt;
10
11pub type Result<T> = std::result::Result<T, ProtocolError>;
13
14#[derive(Debug, Clone, PartialEq)]
16pub enum ProtocolError {
17 InvalidFormat(String),
19 UnsupportedProtocol(String),
21 Base64DecodeError(String),
23 JsonParseError(String),
25 UrlParseError(String),
27 MissingField(String),
29 InvalidField(String),
31 IoError(String),
33}
34
35impl fmt::Display for ProtocolError {
36 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37 match self {
38 ProtocolError::InvalidFormat(msg) => write!(f, "Invalid format: {}", msg),
39 ProtocolError::UnsupportedProtocol(msg) => write!(f, "Unsupported protocol: {}", msg),
40 ProtocolError::Base64DecodeError(msg) => write!(f, "Base64 decode error: {}", msg),
41 ProtocolError::JsonParseError(msg) => write!(f, "JSON parse error: {}", msg),
42 ProtocolError::UrlParseError(msg) => write!(f, "URL parse error: {}", msg),
43 ProtocolError::MissingField(msg) => write!(f, "Missing required field: {}", msg),
44 ProtocolError::InvalidField(msg) => write!(f, "Invalid field value: {}", msg),
45 ProtocolError::IoError(msg) => write!(f, "IO error: {}", msg),
46 }
47 }
48}
49
50impl std::error::Error for ProtocolError {}
51
52impl From<base64::DecodeError> for ProtocolError {
53 fn from(err: base64::DecodeError) -> Self {
54 ProtocolError::Base64DecodeError(err.to_string())
55 }
56}
57
58impl From<serde_json::Error> for ProtocolError {
59 fn from(err: serde_json::Error) -> Self {
60 ProtocolError::JsonParseError(err.to_string())
61 }
62}
63
64impl From<std::num::ParseIntError> for ProtocolError {
65 fn from(err: std::num::ParseIntError) -> Self {
66 ProtocolError::InvalidField(format!("Parse integer error: {}", err))
67 }
68}