1pub type Result<T> = std::result::Result<T, Error>;
5
6#[derive(Debug, Clone, thiserror::Error)]
8pub enum Error {
9 #[error("Invalid JSON syntax at position {position}: {message}")]
11 InvalidJson {
12 position: usize,
14 message: String,
16 },
17
18 #[error("Invalid frame format: {0}")]
20 InvalidFrame(String),
21
22 #[error("Schema validation failed: {0}")]
24 SchemaValidation(String),
25
26 #[error("Semantic type mismatch: expected {expected}, got {actual}")]
28 SemanticTypeMismatch {
29 expected: String,
31 actual: String,
33 },
34
35 #[error("Buffer error: {0}")]
37 Buffer(String),
38
39 #[error("Memory allocation failed: {0}")]
41 Memory(String),
42
43 #[error("I/O error: {0}")]
45 Io(String),
46
47 #[error("Connection failed: {0}")]
49 ConnectionFailed(String),
50
51 #[error("Client error: {0}")]
53 ClientError(String),
54
55 #[error("Invalid session: {0}")]
57 InvalidSession(String),
58
59 #[error("Invalid URL: {0}")]
61 InvalidUrl(String),
62
63 #[error("Serialization error: {0}")]
65 Serialization(String),
66
67 #[error("UTF-8 conversion failed: {0}")]
69 Utf8(String),
70
71 #[error("{0}")]
73 Other(String),
74}
75
76impl Error {
77 pub fn invalid_json(position: usize, message: impl Into<String>) -> Self {
79 Self::InvalidJson {
80 position,
81 message: message.into(),
82 }
83 }
84
85 pub fn invalid_frame(message: impl Into<String>) -> Self {
87 Self::InvalidFrame(message.into())
88 }
89
90 pub fn buffer(message: impl Into<String>) -> Self {
92 Self::Buffer(message.into())
93 }
94
95 pub fn memory(message: impl Into<String>) -> Self {
97 Self::Memory(message.into())
98 }
99
100 pub fn other(message: impl Into<String>) -> Self {
102 Self::Other(message.into())
103 }
104}
105
106impl From<std::io::Error> for Error {
107 fn from(err: std::io::Error) -> Self {
108 Error::Io(err.to_string())
109 }
110}
111
112impl From<std::str::Utf8Error> for Error {
113 fn from(err: std::str::Utf8Error) -> Self {
114 Error::Utf8(err.to_string())
115 }
116}