mockforge-core 0.3.116

Shared logic for MockForge - routing, validation, latency, proxy
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
//! Error types for MockForge Core

/// Result type alias for MockForge operations
pub type Result<T> = std::result::Result<T, Error>;

/// Core error types for MockForge operations
#[derive(Debug, thiserror::Error)]
pub enum Error {
    /// Validation error (schema/format validation failed)
    #[error("Validation error: {message}")]
    Validation {
        /// Validation error message
        message: String,
    },

    /// Routing error (route not found or invalid)
    #[error("Routing error: {message}")]
    Routing {
        /// Routing error message
        message: String,
    },

    /// Proxy error (proxy request failed)
    #[error("Proxy error: {message}")]
    Proxy {
        /// Proxy error message
        message: String,
    },

    /// Latency simulation error (latency injection failed)
    #[error("Latency simulation error: {message}")]
    Latency {
        /// Latency error message
        message: String,
    },

    /// Configuration error (invalid config or missing required fields)
    #[error("Configuration error: {message}")]
    Config {
        /// Configuration error message
        message: String,
    },

    /// Protocol not found (requested protocol is not registered)
    #[error("Protocol not found: {message}")]
    ProtocolNotFound {
        /// Protocol not found error message
        message: String,
    },

    /// Protocol disabled (protocol exists but is disabled)
    #[error("Protocol disabled: {message}")]
    ProtocolDisabled {
        /// Protocol disabled error message
        message: String,
    },

    /// Protocol handler in use (handler already registered)
    #[error("Protocol handler in use: {message}")]
    ProtocolHandlerInUse {
        /// Protocol handler conflict error message
        message: String,
    },

    /// Protocol validation error (protocol-specific validation failed)
    #[error("Protocol validation error: {message}")]
    ProtocolValidationError {
        /// Protocol name that failed validation
        protocol: String,
        /// Validation error message
        message: String,
    },

    /// I/O error (file read/write operations)
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),

    /// JSON serialization/deserialization error
    #[error("JSON error: {0}")]
    Json(#[from] serde_json::Error),

    /// YAML serialization/deserialization error
    #[error("YAML error: {0}")]
    Yaml(#[from] serde_yaml::Error),

    /// HTTP client request error
    #[error("HTTP error: {0}")]
    Http(#[from] reqwest::Error),

    /// URL parsing error
    #[error("URL parse error: {0}")]
    UrlParse(#[from] url::ParseError),

    /// Regular expression compilation error
    #[error("Regex error: {0}")]
    Regex(#[from] regex::Error),

    /// Route not found error with method and path context
    #[error("Route not found: {method} {path}")]
    RouteNotFound {
        /// HTTP method
        method: String,
        /// Request path
        path: String,
    },

    /// Schema validation failed with structured context
    #[error("Schema validation failed at '{path}': expected {expected}, got {actual}")]
    SchemaValidationFailed {
        /// JSON path where validation failed
        path: String,
        /// Expected type or value
        expected: String,
        /// Actual type or value encountered
        actual: String,
    },

    /// Configuration error with source
    #[error("Configuration error: {message}")]
    ConfigWithSource {
        /// Configuration error message
        message: String,
        /// Underlying cause
        #[source]
        source: Box<dyn std::error::Error + Send + Sync>,
    },

    /// Entity not found
    #[error("{entity} not found: {id}")]
    NotFound {
        /// Entity type that was not found
        entity: String,
        /// Identifier that was looked up
        id: String,
    },

    /// Feature is disabled
    #[error("Feature disabled: {feature}")]
    FeatureDisabled {
        /// Feature name that is disabled
        feature: String,
    },

    /// Global component already initialized
    #[error("Already initialized: {component}")]
    AlreadyInitialized {
        /// Component name that was already initialized
        component: String,
    },

    /// Invalid state for the requested operation
    #[error("Invalid state: {message}")]
    InvalidState {
        /// Description of the invalid state
        message: String,
    },

    /// SIEM transport error
    #[error("SIEM transport error: {message}")]
    SiemTransport {
        /// SIEM transport error message
        message: String,
    },

    /// I/O error with additional context
    #[error("I/O error ({context}): {message}")]
    IoWithContext {
        /// Context describing what operation failed
        context: String,
        /// The underlying error message
        message: String,
    },

    /// Internal error wrapping an underlying cause
    #[error("Internal error: {message}")]
    Internal {
        /// Internal error message
        message: String,
    },

    /// Generic error with message string
    #[error("Generic error: {0}")]
    Generic(String),

    /// Encryption/decryption operation error
    #[error("Encryption error: {0}")]
    Encryption(#[from] crate::encryption::EncryptionError),

    /// JavaScript evaluation error (template engine, etc.)
    #[cfg(feature = "scripting")]
    #[error("JavaScript error: {0}")]
    JavaScript(#[from] rquickjs::Error),
}

impl From<String> for Error {
    fn from(message: String) -> Self {
        Self::Generic(message)
    }
}

impl Error {
    /// Create a validation error
    pub fn validation<S: Into<String>>(message: S) -> Self {
        Self::Validation {
            message: message.into(),
        }
    }

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

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

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

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

    /// Create a protocol not found error
    pub fn protocol_not_found<S: Into<String>>(message: S) -> Self {
        Self::ProtocolNotFound {
            message: message.into(),
        }
    }

    /// Create a protocol disabled error
    pub fn protocol_disabled<S: Into<String>>(message: S) -> Self {
        Self::ProtocolDisabled {
            message: message.into(),
        }
    }

    /// Create a protocol handler in use error
    pub fn protocol_handler_in_use<S: Into<String>>(message: S) -> Self {
        Self::ProtocolHandlerInUse {
            message: message.into(),
        }
    }

    /// Create a protocol validation error
    pub fn protocol_validation_error<S: Into<String>>(protocol: S, message: S) -> Self {
        Self::ProtocolValidationError {
            protocol: protocol.into(),
            message: message.into(),
        }
    }

    /// Create a route-not-found error with method and path context
    pub fn route_not_found<S: Into<String>>(method: S, path: S) -> Self {
        Self::RouteNotFound {
            method: method.into(),
            path: path.into(),
        }
    }

    /// Create a schema validation error with path, expected, and actual context
    pub fn schema_validation_failed<S: Into<String>>(path: S, expected: S, actual: S) -> Self {
        Self::SchemaValidationFailed {
            path: path.into(),
            expected: expected.into(),
            actual: actual.into(),
        }
    }

    /// Create a configuration error with an underlying source error
    pub fn config_with_source<S: Into<String>>(
        message: S,
        source: impl std::error::Error + Send + Sync + 'static,
    ) -> Self {
        Self::ConfigWithSource {
            message: message.into(),
            source: Box::new(source),
        }
    }

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

    /// Create a feature-disabled error
    pub fn feature_disabled<S: Into<String>>(feature: S) -> Self {
        Self::FeatureDisabled {
            feature: feature.into(),
        }
    }

    /// Create an already-initialized error
    pub fn already_initialized<S: Into<String>>(component: S) -> Self {
        Self::AlreadyInitialized {
            component: component.into(),
        }
    }

    /// Create an invalid-state error
    pub fn invalid_state<S: Into<String>>(message: S) -> Self {
        Self::InvalidState {
            message: message.into(),
        }
    }

    /// Create a SIEM transport error
    pub fn siem_transport<S: Into<String>>(message: S) -> Self {
        Self::SiemTransport {
            message: message.into(),
        }
    }

    /// Create an I/O error with context
    pub fn io_with_context<S1: Into<String>, S2: Into<String>>(context: S1, message: S2) -> Self {
        Self::IoWithContext {
            context: context.into(),
            message: message.into(),
        }
    }

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

    /// Create a generic error
    #[deprecated(note = "Use a specific error variant instead of Generic")]
    pub fn generic<S: Into<String>>(message: S) -> Self {
        Self::Generic(message.into())
    }
}

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

    #[test]
    fn test_validation_error() {
        let err = Error::validation("test validation");
        assert!(err.to_string().contains("Validation error"));
        assert!(err.to_string().contains("test validation"));
    }

    #[test]
    fn test_routing_error() {
        let err = Error::routing("test routing");
        assert!(err.to_string().contains("Routing error"));
        assert!(err.to_string().contains("test routing"));
    }

    #[test]
    fn test_proxy_error() {
        let err = Error::proxy("test proxy");
        assert!(err.to_string().contains("Proxy error"));
        assert!(err.to_string().contains("test proxy"));
    }

    #[test]
    fn test_latency_error() {
        let err = Error::latency("test latency");
        assert!(err.to_string().contains("Latency simulation error"));
        assert!(err.to_string().contains("test latency"));
    }

    #[test]
    fn test_config_error() {
        let err = Error::config("test config");
        assert!(err.to_string().contains("Configuration error"));
        assert!(err.to_string().contains("test config"));
    }

    #[test]
    fn test_internal_error() {
        let err = Error::internal("test internal");
        assert!(err.to_string().contains("Internal error"));
        assert!(err.to_string().contains("test internal"));
    }

    #[test]
    #[allow(deprecated)]
    fn test_generic_error() {
        let err = Error::generic("test generic");
        assert!(err.to_string().contains("Generic error"));
        assert!(err.to_string().contains("test generic"));
    }

    #[test]
    fn test_not_found_error() {
        let err = Error::not_found("User", "user-123");
        assert_eq!(err.to_string(), "User not found: user-123");
    }

    #[test]
    fn test_feature_disabled_error() {
        let err = Error::feature_disabled("access review");
        assert_eq!(err.to_string(), "Feature disabled: access review");
    }

    #[test]
    fn test_already_initialized_error() {
        let err = Error::already_initialized("SIEM emitter");
        assert_eq!(err.to_string(), "Already initialized: SIEM emitter");
    }

    #[test]
    fn test_invalid_state_error() {
        let err = Error::invalid_state("Request is not pending approval");
        assert_eq!(err.to_string(), "Invalid state: Request is not pending approval");
    }

    #[test]
    fn test_siem_transport_error() {
        let err = Error::siem_transport("Connection refused");
        assert_eq!(err.to_string(), "SIEM transport error: Connection refused");
    }

    #[test]
    fn test_io_with_context_error() {
        let err = Error::io_with_context("reading risk register", "file not found");
        assert_eq!(err.to_string(), "I/O error (reading risk register): file not found");
    }

    #[test]
    fn test_from_string() {
        let err: Error = "test message".to_string().into();
        assert!(matches!(err, Error::Generic(_)));
        assert!(err.to_string().contains("test message"));
    }

    #[test]
    fn test_json_error_conversion() {
        let json_err = serde_json::from_str::<serde_json::Value>("invalid json");
        assert!(json_err.is_err());
        let err: Error = json_err.unwrap_err().into();
        assert!(matches!(err, Error::Json(_)));
    }

    #[test]
    fn test_url_parse_error_conversion() {
        let url_err = url::Url::parse("not a url");
        assert!(url_err.is_err());
        let err: Error = url_err.unwrap_err().into();
        assert!(matches!(err, Error::UrlParse(_)));
    }

    #[test]
    #[allow(clippy::invalid_regex)]
    fn test_regex_error_conversion() {
        let regex_err = regex::Regex::new("[invalid(");
        assert!(regex_err.is_err());
        let err: Error = regex_err.unwrap_err().into();
        assert!(matches!(err, Error::Regex(_)));
    }

    #[test]
    #[allow(deprecated)]
    fn test_error_display() {
        let errors = vec![
            (Error::validation("msg"), "Validation error: msg"),
            (Error::routing("msg"), "Routing error: msg"),
            (Error::proxy("msg"), "Proxy error: msg"),
            (Error::latency("msg"), "Latency simulation error: msg"),
            (Error::config("msg"), "Configuration error: msg"),
            (Error::internal("msg"), "Internal error: msg"),
        ];

        for (err, expected) in errors {
            assert_eq!(err.to_string(), expected);
        }
    }
}