Skip to main content

fionn_stream/skiptape/
error.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2//! Error types for SIMD-JSONL Skip Tape processing
3
4use std::fmt;
5
6/// Errors that can occur during skip tape processing
7#[derive(Debug, Clone)]
8pub enum SkipTapeError {
9    /// JSON parsing error
10    ParseError(String),
11    /// Schema compilation error
12    SchemaError(String),
13    /// Memory allocation error
14    MemoryError(String),
15    /// SIMD processing error
16    SimdError(String),
17    /// I/O error during batch processing
18    IoError(String),
19    /// Schema validation error
20    ValidationError(String),
21}
22
23impl fmt::Display for SkipTapeError {
24    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25        match self {
26            Self::ParseError(msg) => write!(f, "Parse error: {msg}"),
27            Self::SchemaError(msg) => write!(f, "Schema error: {msg}"),
28            Self::MemoryError(msg) => write!(f, "Memory error: {msg}"),
29            Self::SimdError(msg) => write!(f, "SIMD error: {msg}"),
30            Self::IoError(msg) => write!(f, "I/O error: {msg}"),
31            Self::ValidationError(msg) => write!(f, "Validation error: {msg}"),
32        }
33    }
34}
35
36impl std::error::Error for SkipTapeError {}
37
38impl From<SkipTapeError> for fionn_core::DsonError {
39    fn from(error: SkipTapeError) -> Self {
40        Self::SkipTapeError(error.to_string())
41    }
42}
43
44impl From<fionn_core::DsonError> for SkipTapeError {
45    fn from(error: fionn_core::DsonError) -> Self {
46        Self::ParseError(error.to_string())
47    }
48}
49
50/// Result type alias for skip tape operations
51pub type Result<T> = std::result::Result<T, SkipTapeError>;
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn test_parse_error() {
59        let error = SkipTapeError::ParseError("test".to_string());
60        assert!(error.to_string().contains("test"));
61    }
62
63    #[test]
64    fn test_schema_error() {
65        let error = SkipTapeError::SchemaError("schema".to_string());
66        assert!(error.to_string().contains("schema"));
67    }
68
69    #[test]
70    fn test_memory_error() {
71        let error = SkipTapeError::MemoryError("mem".to_string());
72        assert!(error.to_string().contains("mem"));
73    }
74
75    #[test]
76    fn test_simd_error() {
77        let error = SkipTapeError::SimdError("simd".to_string());
78        assert!(error.to_string().contains("simd"));
79    }
80
81    #[test]
82    fn test_io_error() {
83        let error = SkipTapeError::IoError("io".to_string());
84        assert!(error.to_string().contains("io"));
85    }
86
87    #[test]
88    fn test_validation_error() {
89        let error = SkipTapeError::ValidationError("val".to_string());
90        assert!(error.to_string().contains("val"));
91    }
92
93    #[test]
94    fn test_error_clone() {
95        let error = SkipTapeError::ParseError("clone".to_string());
96        let cloned = error.clone();
97        assert_eq!(error.to_string(), cloned.to_string());
98    }
99
100    #[test]
101    fn test_error_debug() {
102        let error = SkipTapeError::ParseError("debug".to_string());
103        let debug = format!("{error:?}");
104        assert!(!debug.is_empty());
105    }
106
107    #[test]
108    fn test_from_dson_error() {
109        let dson_error = fionn_core::DsonError::ParseError("dson parse".to_string());
110        let skip_error: SkipTapeError = dson_error.into();
111        assert!(matches!(skip_error, SkipTapeError::ParseError(_)));
112        assert!(skip_error.to_string().contains("Parse error"));
113    }
114
115    #[test]
116    fn test_error_trait_impl() {
117        // Test that SkipTapeError implements std::error::Error
118        let error: Box<dyn std::error::Error> =
119            Box::new(SkipTapeError::ParseError("error trait".to_string()));
120        assert!(error.to_string().contains("error trait"));
121    }
122
123    #[test]
124    fn test_all_variants_clone() {
125        let errors = vec![
126            SkipTapeError::ParseError("p".to_string()),
127            SkipTapeError::SchemaError("s".to_string()),
128            SkipTapeError::MemoryError("m".to_string()),
129            SkipTapeError::SimdError("i".to_string()),
130            SkipTapeError::IoError("o".to_string()),
131            SkipTapeError::ValidationError("v".to_string()),
132        ];
133        for error in errors {
134            let cloned = error.clone();
135            assert_eq!(error.to_string(), cloned.to_string());
136        }
137    }
138}