zeal-sdk 1.0.5

Rust SDK for Zeal Integration Protocol (ZIP)
Documentation
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! Error types for the Zeal SDK

/// Result type alias for Zeal SDK operations
pub type Result<T> = std::result::Result<T, ZealError>;

/// Main error type for the Zeal SDK  
#[derive(Debug, thiserror::Error)]
pub enum ZealError {
    /// Network-related errors
    #[error("Network error: {source}")]
    NetworkError {
        #[source]
        source: reqwest::Error,
        retryable: bool,
    },

    /// WebSocket-related errors
    #[error("WebSocket error: {message}")]
    WebSocketError { message: String },

    /// JSON parsing errors
    #[error("JSON error: {source}")]
    JsonError {
        #[source]
        source: serde_json::Error,
    },

    /// Invalid configuration
    #[error("Configuration error: {message}")]
    ConfigurationError { message: String },

    /// API errors from the server
    #[error("API error ({status}): {message}")]
    ApiError {
        status: u16,
        message: String,
        error_code: Option<String>,
    },

    /// Resource not found
    #[error("Resource not found: {resource} with ID '{id}'")]
    NotFound { resource: String, id: String },

    /// Validation errors
    #[error("Validation error in field '{field}': {message}")]
    ValidationError { field: String, message: String },

    /// Authentication/authorization errors
    #[error("Authentication error: {message}")]
    AuthenticationError { message: String },

    /// Rate limiting errors
    #[error("Rate limit exceeded: {message}")]
    RateLimitError {
        message: String,
        retry_after: Option<std::time::Duration>,
    },

    /// Timeout errors
    #[error("Operation timed out: {operation}")]
    TimeoutError { operation: String },

    /// Connection errors
    #[error("Connection error: {message}")]
    ConnectionError { message: String },

    /// Serialization errors
    #[error("Serialization error: {source}")]
    SerializationError {
        #[source]
        source: Box<dyn std::error::Error + Send + Sync>,
    },

    /// URL parsing errors
    #[error("Invalid URL: {source}")]
    InvalidUrl {
        #[source]
        source: url::ParseError,
    },

    /// I/O errors
    #[error("I/O error: {source}")]
    IoError {
        #[source]
        source: std::io::Error,
    },

    /// Generic errors
    #[error("Error: {message}")]
    Other { message: String },
}

impl Clone for ZealError {
    fn clone(&self) -> Self {
        match self {
            Self::NetworkError { .. } => Self::Other {
                message: "Network error".to_string(),
            },
            Self::WebSocketError { message } => Self::WebSocketError {
                message: message.clone(),
            },
            Self::JsonError { .. } => Self::Other {
                message: "JSON parsing error".to_string(),
            },
            Self::ConfigurationError { message } => Self::ConfigurationError {
                message: message.clone(),
            },
            Self::ApiError {
                status,
                message,
                error_code,
            } => Self::ApiError {
                status: *status,
                message: message.clone(),
                error_code: error_code.clone(),
            },
            Self::NotFound { resource, id } => Self::NotFound {
                resource: resource.clone(),
                id: id.clone(),
            },
            Self::ValidationError { field, message } => Self::ValidationError {
                field: field.clone(),
                message: message.clone(),
            },
            Self::AuthenticationError { message } => Self::AuthenticationError {
                message: message.clone(),
            },
            Self::RateLimitError {
                message,
                retry_after,
            } => Self::RateLimitError {
                message: message.clone(),
                retry_after: *retry_after,
            },
            Self::TimeoutError { operation } => Self::TimeoutError {
                operation: operation.clone(),
            },
            Self::ConnectionError { message } => Self::ConnectionError {
                message: message.clone(),
            },
            Self::SerializationError { .. } => Self::Other {
                message: "Serialization error".to_string(),
            },
            Self::InvalidUrl { .. } => Self::Other {
                message: "Invalid URL".to_string(),
            },
            Self::IoError { .. } => Self::Other {
                message: "IO error".to_string(),
            },
            Self::Other { message } => Self::Other {
                message: message.clone(),
            },
        }
    }
}

impl ZealError {
    /// Create a network error
    pub fn network_error(source: reqwest::Error) -> Self {
        let retryable = source.is_timeout()
            || source.is_connect()
            || source
                .status()
                .is_some_and(|s| matches!(s.as_u16(), 408 | 429 | 500..=599));

        Self::NetworkError { source, retryable }
    }

    /// Create a WebSocket error
    pub fn websocket_error<S: Into<String>>(message: S) -> Self {
        Self::WebSocketError {
            message: message.into(),
        }
    }

    /// Create a configuration error
    pub fn configuration_error<S: Into<String>>(message: S) -> Self {
        Self::ConfigurationError {
            message: message.into(),
        }
    }

    /// Create an API error
    pub fn api_error(status: u16, message: String, error_code: Option<String>) -> Self {
        Self::ApiError {
            status,
            message,
            error_code,
        }
    }

    /// Create a not found error
    pub fn not_found<S: Into<String>>(resource: S, id: S) -> Self {
        Self::NotFound {
            resource: resource.into(),
            id: id.into(),
        }
    }

    /// Create a validation error
    pub fn validation_error<S: Into<String>>(field: S, message: S) -> Self {
        Self::ValidationError {
            field: field.into(),
            message: message.into(),
        }
    }

    /// Create an authentication error
    pub fn authentication_error<S: Into<String>>(message: S) -> Self {
        Self::AuthenticationError {
            message: message.into(),
        }
    }

    /// Create a rate limit error
    pub fn rate_limit_error<S: Into<String>>(
        message: S,
        retry_after: Option<std::time::Duration>,
    ) -> Self {
        Self::RateLimitError {
            message: message.into(),
            retry_after,
        }
    }

    /// Create a timeout error
    pub fn timeout_error<S: Into<String>>(operation: S) -> Self {
        Self::TimeoutError {
            operation: operation.into(),
        }
    }

    /// Create a connection error
    pub fn connection_error<S: Into<String>>(message: S) -> Self {
        Self::ConnectionError {
            message: message.into(),
        }
    }

    /// Create a generic error
    pub fn other<S: Into<String>>(message: S) -> Self {
        Self::Other {
            message: message.into(),
        }
    }

    /// Check if the error is retryable
    pub fn is_retryable(&self) -> bool {
        match self {
            Self::NetworkError { retryable, .. } => *retryable,
            Self::RateLimitError { .. } => true,
            Self::TimeoutError { .. } => true,
            Self::ConnectionError { .. } => true,
            Self::ApiError { status, .. } => matches!(*status, 408 | 429 | 500..=599),
            _ => false,
        }
    }

    /// Get the retry delay if applicable
    pub fn retry_after(&self) -> Option<std::time::Duration> {
        match self {
            Self::RateLimitError { retry_after, .. } => *retry_after,
            _ => None,
        }
    }

    /// Check if the error is a client error (4xx)
    pub fn is_client_error(&self) -> bool {
        match self {
            Self::ApiError { status, .. } => matches!(*status, 400..=499),
            Self::NotFound { .. } => true,
            Self::ValidationError { .. } => true,
            Self::AuthenticationError { .. } => true,
            _ => false,
        }
    }

    /// Check if the error is a server error (5xx)
    pub fn is_server_error(&self) -> bool {
        match self {
            Self::ApiError { status, .. } => matches!(*status, 500..=599),
            _ => false,
        }
    }
}

impl From<reqwest::Error> for ZealError {
    fn from(err: reqwest::Error) -> Self {
        Self::network_error(err)
    }
}

impl From<serde_json::Error> for ZealError {
    fn from(err: serde_json::Error) -> Self {
        Self::JsonError { source: err }
    }
}

impl From<url::ParseError> for ZealError {
    fn from(err: url::ParseError) -> Self {
        Self::InvalidUrl { source: err }
    }
}

impl From<std::io::Error> for ZealError {
    fn from(err: std::io::Error) -> Self {
        Self::IoError { source: err }
    }
}

impl From<tokio_tungstenite::tungstenite::Error> for ZealError {
    fn from(err: tokio_tungstenite::tungstenite::Error) -> Self {
        Self::websocket_error(err.to_string())
    }
}

/// Error builder for constructing complex errors
#[derive(Debug, Default)]
pub struct ErrorBuilder {
    message: Option<String>,
    source: Option<Box<dyn std::error::Error + Send + Sync>>,
    retryable: bool,
    status: Option<u16>,
    error_code: Option<String>,
}

impl ErrorBuilder {
    /// Create a new error builder
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the error message
    pub fn message<S: Into<String>>(mut self, message: S) -> Self {
        self.message = Some(message.into());
        self
    }

    /// Set the source error
    pub fn source<E: std::error::Error + Send + Sync + 'static>(mut self, source: E) -> Self {
        self.source = Some(Box::new(source));
        self
    }

    /// Mark the error as retryable
    pub fn retryable(mut self, retryable: bool) -> Self {
        self.retryable = retryable;
        self
    }

    /// Set the HTTP status code
    pub fn status(mut self, status: u16) -> Self {
        self.status = Some(status);
        self
    }

    /// Set the error code
    pub fn error_code<S: Into<String>>(mut self, code: S) -> Self {
        self.error_code = Some(code.into());
        self
    }

    /// Build the error
    pub fn build(self) -> ZealError {
        let message = self.message.unwrap_or_else(|| "Unknown error".to_string());

        if let Some(status) = self.status {
            ZealError::ApiError {
                status,
                message,
                error_code: self.error_code,
            }
        } else if let Some(source) = self.source {
            ZealError::SerializationError { source }
        } else {
            ZealError::Other { message }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_error_creation() {
        let err = ZealError::not_found("template", "test-id");
        assert!(matches!(err, ZealError::NotFound { .. }));
    }

    #[test]
    fn test_retryable_errors() {
        let err = ZealError::api_error(500, "Server error".to_string(), None);
        assert!(err.is_retryable());

        let err = ZealError::api_error(400, "Bad request".to_string(), None);
        assert!(!err.is_retryable());
    }

    #[test]
    fn test_error_builder() {
        let err = ErrorBuilder::new()
            .message("Test error")
            .status(404)
            .build();

        assert!(matches!(err, ZealError::ApiError { status: 404, .. }));
    }

    #[test]
    fn test_client_server_error_classification() {
        let client_err = ZealError::api_error(400, "Bad request".to_string(), None);
        assert!(client_err.is_client_error());
        assert!(!client_err.is_server_error());

        let server_err = ZealError::api_error(500, "Server error".to_string(), None);
        assert!(!server_err.is_client_error());
        assert!(server_err.is_server_error());
    }
}