Skip to main content

syncular_protocol/
error.rs

1use std::fmt;
2
3pub type Result<T> = std::result::Result<T, ProtocolError>;
4
5#[derive(Debug, Clone, PartialEq, Eq)]
6pub struct ProtocolError {
7    message: String,
8}
9
10impl ProtocolError {
11    pub fn message(message: impl fmt::Display) -> Self {
12        Self {
13            message: message.to_string(),
14        }
15    }
16
17    pub fn context(self, context: impl fmt::Display) -> Self {
18        Self {
19            message: format!("{context}: {}", self.message),
20        }
21    }
22}
23
24impl fmt::Display for ProtocolError {
25    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26        f.write_str(&self.message)
27    }
28}
29
30impl std::error::Error for ProtocolError {}
31
32impl From<serde_json::Error> for ProtocolError {
33    fn from(source: serde_json::Error) -> Self {
34        Self::message(source)
35    }
36}