fynx_platform/
error.rs

1//! Error types for Fynx
2
3use std::fmt;
4
5/// Unified error type for all Fynx operations
6#[derive(Debug)]
7pub enum FynxError {
8    /// I/O error
9    Io(std::io::Error),
10
11    /// Configuration error
12    Config(String),
13
14    /// Protocol error
15    Protocol(String),
16
17    /// Security error (authentication, authorization, etc.)
18    Security(String),
19
20    /// Not implemented
21    NotImplemented(String),
22
23    /// Other error
24    Other(Box<dyn std::error::Error + Send + Sync>),
25}
26
27impl fmt::Display for FynxError {
28    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29        match self {
30            FynxError::Io(e) => write!(f, "IO error: {}", e),
31            FynxError::Config(msg) => write!(f, "Configuration error: {}", msg),
32            FynxError::Protocol(msg) => write!(f, "Protocol error: {}", msg),
33            FynxError::Security(msg) => write!(f, "Security error: {}", msg),
34            FynxError::NotImplemented(msg) => write!(f, "Not implemented: {}", msg),
35            FynxError::Other(e) => write!(f, "Error: {}", e),
36        }
37    }
38}
39
40impl std::error::Error for FynxError {
41    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
42        match self {
43            FynxError::Io(e) => Some(e),
44            FynxError::Other(e) => Some(e.as_ref()),
45            _ => None,
46        }
47    }
48}
49
50impl From<std::io::Error> for FynxError {
51    fn from(err: std::io::Error) -> Self {
52        FynxError::Io(err)
53    }
54}
55
56/// Result type for Fynx operations
57pub type FynxResult<T> = Result<T, FynxError>;
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62
63    #[test]
64    fn test_error_display() {
65        let err = FynxError::Config("Invalid configuration".to_string());
66        assert_eq!(
67            err.to_string(),
68            "Configuration error: Invalid configuration"
69        );
70    }
71
72    #[test]
73    fn test_io_error_conversion() {
74        let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
75        let fynx_err: FynxError = io_err.into();
76        assert!(matches!(fynx_err, FynxError::Io(_)));
77    }
78
79    #[test]
80    fn test_result_type() {
81        fn example() -> FynxResult<i32> {
82            Ok(42)
83        }
84
85        assert_eq!(example().unwrap(), 42);
86    }
87}