1use std::fmt;
4
5pub type Result<T> = std::result::Result<T, EdgeError>;
7
8#[derive(Debug, thiserror::Error)]
10pub enum EdgeError {
11 #[error("I/O error: {0}")]
13 Io(#[from] std::io::Error),
14
15 #[error("Serialization error: {0}")]
17 Serialization(String),
18
19 #[error("Deserialization error: {0}")]
21 Deserialization(String),
22
23 #[error("Compression error: {0}")]
25 Compression(String),
26
27 #[error("Decompression error: {0}")]
29 Decompression(String),
30
31 #[error("Cache error: {0}")]
33 Cache(String),
34
35 #[error("Sync error: {0}")]
37 Sync(String),
38
39 #[error("Conflict resolution error: {0}")]
41 Conflict(String),
42
43 #[error("Resource constraint violated: {0}")]
45 ResourceConstraint(String),
46
47 #[error("Runtime error: {0}")]
49 Runtime(String),
50
51 #[error("Invalid configuration: {0}")]
53 InvalidConfig(String),
54
55 #[error("Network error: {0}")]
57 Network(String),
58
59 #[error("Storage error: {0}")]
61 Storage(String),
62
63 #[error("Operation not supported: {0}")]
65 NotSupported(String),
66
67 #[error("Operation timed out: {0}")]
69 Timeout(String),
70
71 #[error("{0}")]
73 Other(String),
74}
75
76impl EdgeError {
77 pub fn serialization<S: fmt::Display>(msg: S) -> Self {
79 Self::Serialization(msg.to_string())
80 }
81
82 pub fn deserialization<S: fmt::Display>(msg: S) -> Self {
84 Self::Deserialization(msg.to_string())
85 }
86
87 pub fn compression<S: fmt::Display>(msg: S) -> Self {
89 Self::Compression(msg.to_string())
90 }
91
92 pub fn decompression<S: fmt::Display>(msg: S) -> Self {
94 Self::Decompression(msg.to_string())
95 }
96
97 pub fn cache<S: fmt::Display>(msg: S) -> Self {
99 Self::Cache(msg.to_string())
100 }
101
102 pub fn sync<S: fmt::Display>(msg: S) -> Self {
104 Self::Sync(msg.to_string())
105 }
106
107 pub fn conflict<S: fmt::Display>(msg: S) -> Self {
109 Self::Conflict(msg.to_string())
110 }
111
112 pub fn resource_constraint<S: fmt::Display>(msg: S) -> Self {
114 Self::ResourceConstraint(msg.to_string())
115 }
116
117 pub fn runtime<S: fmt::Display>(msg: S) -> Self {
119 Self::Runtime(msg.to_string())
120 }
121
122 pub fn invalid_config<S: fmt::Display>(msg: S) -> Self {
124 Self::InvalidConfig(msg.to_string())
125 }
126
127 pub fn network<S: fmt::Display>(msg: S) -> Self {
129 Self::Network(msg.to_string())
130 }
131
132 pub fn storage<S: fmt::Display>(msg: S) -> Self {
134 Self::Storage(msg.to_string())
135 }
136
137 pub fn not_supported<S: fmt::Display>(msg: S) -> Self {
139 Self::NotSupported(msg.to_string())
140 }
141
142 pub fn timeout<S: fmt::Display>(msg: S) -> Self {
144 Self::Timeout(msg.to_string())
145 }
146
147 pub fn other<S: fmt::Display>(msg: S) -> Self {
149 Self::Other(msg.to_string())
150 }
151}
152
153#[cfg(test)]
154mod tests {
155 use super::*;
156
157 #[test]
158 fn test_error_creation() {
159 let err = EdgeError::cache("test error");
160 assert_eq!(err.to_string(), "Cache error: test error");
161
162 let err = EdgeError::sync("sync failed");
163 assert_eq!(err.to_string(), "Sync error: sync failed");
164
165 let err = EdgeError::resource_constraint("memory limit exceeded");
166 assert_eq!(
167 err.to_string(),
168 "Resource constraint violated: memory limit exceeded"
169 );
170 }
171
172 #[test]
173 fn test_error_conversion() {
174 let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
175 let edge_err: EdgeError = io_err.into();
176 assert!(matches!(edge_err, EdgeError::Io(_)));
177 }
178}