summaryrefslogtreecommitdiff
path: root/src/error.rs
blob: f101a615cd0ab804bc4eb127fdf7ee7d18f436c8 (plain)
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
//! Error types for the geek-szitman-supercamera crate

use thiserror::Error;

/// Result type for the crate
pub type Result<T> = std::result::Result<T, Error>;

/// Main error type for the crate
#[derive(Error, Debug)]
pub enum Error {
    /// USB communication errors
    #[error("USB error: {0}")]
    Usb(#[from] UsbError),

    /// Video backend errors
    #[error("Video backend error: {0}")]
    Video(#[from] VideoError),

    /// Protocol errors
    #[error("Protocol error: {0}")]
    Protocol(#[from] ProtocolError),

    /// JPEG processing errors
    #[error("JPEG error: {0}")]
    Jpeg(#[from] JpegError),

    /// System errors
    #[error("System error: {0}")]
    System(#[from] SystemError),

    /// Generic error wrapper
    #[error("Generic error: {0}")]
    Generic(String),
}

/// USB-specific errors
#[derive(Error, Debug)]
pub enum UsbError {
    /// Device not found
    #[error("USB device not found")]
    DeviceNotFound,

    /// Device disconnected
    #[error("USB device disconnected")]
    DeviceDisconnected,

    /// Permission denied
    #[error("USB permission denied")]
    PermissionDenied,

    /// Interface claim failed
    #[error("Failed to claim USB interface: {0}")]
    InterfaceClaimFailed(String),

    /// Bulk transfer failed
    #[error("USB bulk transfer failed: {0}")]
    BulkTransferFailed(String),

    /// Timeout error
    #[error("USB operation timed out")]
    Timeout,

    /// Generic USB error
    #[error("USB error: {0}")]
    Generic(String),
}

impl UsbError {
    /// Check if this error indicates device disconnection
    pub fn is_device_disconnected(&self) -> bool {
        matches!(self, Self::DeviceDisconnected)
    }
}

/// Video backend errors
#[derive(Error, Debug)]
pub enum VideoError {
    /// PipeWire errors
    #[error("PipeWire error: {0}")]
    PipeWire(String),

    /// V4L2 errors (for future use)
    #[error("V4L2 error: {0}")]
    V4L2(String),

    /// Stdout errors
    #[error("Stdout error: {0}")]
    Stdout(String),

    /// Format not supported
    #[error("Video format not supported: {0}")]
    FormatNotSupported(String),

    /// Device initialization failed
    #[error("Video device initialization failed: {0}")]
    InitializationFailed(String),

    /// Frame push failed
    #[error("Failed to push frame: {0}")]
    FramePushFailed(String),

    /// Device not ready
    #[error("Device not ready")]
    DeviceNotReady,
}

/// Protocol errors
#[derive(Error, Debug)]
pub enum ProtocolError {
    /// Invalid frame format
    #[error("Invalid frame format: {0}")]
    InvalidFrameFormat(String),

    /// Frame too small
    #[error(
        "Frame too small: expected at least {} bytes, got {}",
        expected,
        actual
    )]
    FrameTooSmall { expected: usize, actual: usize },

    /// Invalid magic number
    #[error(
        "Invalid magic number: expected 0x{:04X}, got 0x{:04X}",
        expected,
        actual
    )]
    InvalidMagic { expected: u16, actual: u16 },

    /// Unknown camera ID
    #[error("Unknown camera ID: {0}")]
    UnknownCameraId(u8),

    /// Frame length mismatch
    #[error("Frame length mismatch: expected {}, got {}", expected, actual)]
    FrameLengthMismatch { expected: usize, actual: usize },

    /// Protocol parsing error
    #[error("Protocol parsing error: {0}")]
    ParsingError(String),
}

/// JPEG processing errors
#[derive(Error, Debug)]
pub enum JpegError {
    /// Invalid JPEG header
    #[error("Invalid JPEG header")]
    InvalidHeader,

    /// Unsupported JPEG format
    #[error("Unsupported JPEG format: {0}")]
    UnsupportedFormat(String),

    /// JPEG parsing failed
    #[error("JPEG parsing failed: {0}")]
    ParsingFailed(String),

    /// Image dimensions not found
    #[error("Could not determine image dimensions")]
    DimensionsNotFound,
}

/// System errors
#[derive(Error, Debug)]
pub enum SystemError {
    /// File operation failed
    #[error("File operation failed: {0}")]
    FileError(String),

    /// Permission denied
    #[error("Permission denied: {0}")]
    PermissionDenied(String),

    /// Resource not available
    #[error("Resource not available: {0}")]
    ResourceNotAvailable(String),

    /// Signal handling error
    #[error("Signal handling error: {0}")]
    SignalError(String),
}

impl From<std::io::Error> for Error {
    fn from(err: std::io::Error) -> Self {
        match err.kind() {
            std::io::ErrorKind::NotFound => Error::Usb(UsbError::DeviceNotFound),
            std::io::ErrorKind::PermissionDenied => Error::Usb(UsbError::PermissionDenied),
            std::io::ErrorKind::TimedOut => Error::Usb(UsbError::Timeout),
            _ => Error::System(SystemError::FileError(err.to_string())),
        }
    }
}

impl From<rusb::Error> for Error {
    fn from(err: rusb::Error) -> Self {
        match err {
            rusb::Error::NoDevice => Error::Usb(UsbError::DeviceDisconnected),
            rusb::Error::Access => Error::Usb(UsbError::PermissionDenied),
            rusb::Error::Timeout => Error::Usb(UsbError::Timeout),
            rusb::Error::NotFound => Error::Usb(UsbError::DeviceNotFound),
            _ => Error::Usb(UsbError::Generic(err.to_string())),
        }
    }
}

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

impl From<&str> for Error {
    fn from(err: &str) -> Self {
        Error::Generic(err.to_string())
    }
}

impl From<crate::usb::UsbTransferError> for Error {
    fn from(err: crate::usb::UsbTransferError) -> Self {
        Error::Usb(UsbError::Generic(err.to_string()))
    }
}

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

    #[test]
    fn test_usb_error_is_device_disconnected() {
        let err = UsbError::DeviceDisconnected;
        assert!(err.is_device_disconnected());

        let err = UsbError::DeviceNotFound;
        assert!(!err.is_device_disconnected());
    }

    #[test]
    fn test_error_conversion() {
        let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "not found");
        let err: Error = io_err.into();
        assert!(matches!(err, Error::Usb(UsbError::DeviceNotFound)));

        let usb_err = rusb::Error::NoDevice;
        let err: Error = usb_err.into();
        assert!(matches!(err, Error::Usb(UsbError::DeviceDisconnected)));
    }
}