Skip to main content

kitty_graphics_protocol/
error.rs

1//! Error types for the Kitty graphics protocol
2
3use thiserror::Error;
4
5/// Result type alias for Kitty graphics protocol operations
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Error type for Kitty graphics protocol operations
9#[derive(Error, Debug)]
10pub enum Error {
11    /// Base64 decoding error
12    #[error("Base64 decoding error: {0}")]
13    Base64Decode(#[from] base64::DecodeError),
14
15    /// Invalid image dimensions
16    #[error("Invalid image dimensions: width={width}, height={height}")]
17    InvalidDimensions { width: u32, height: u32 },
18
19    /// Invalid image ID
20    #[error("Invalid image ID: {0}")]
21    InvalidImageId(u32),
22
23    /// Invalid placement ID
24    #[error("Invalid placement ID: {0}")]
25    InvalidPlacementId(u32),
26
27    /// Invalid chunk size
28    #[error("Invalid chunk size: {0} (must be multiple of 4, max 4096)")]
29    InvalidChunkSize(usize),
30
31    /// Missing required field
32    #[error("Missing required field: {0}")]
33    MissingField(&'static str),
34
35    /// Terminal response error
36    #[error("Terminal error: {0}")]
37    TerminalError(String),
38
39    /// IO error
40    #[error("IO error: {0}")]
41    Io(#[from] std::io::Error),
42
43    /// UTF-8 error (FromUtf8Error)
44    #[error("UTF-8 error: {0}")]
45    Utf8(#[from] std::string::FromUtf8Error),
46
47    /// UTF-8 error (Utf8Error)
48    #[error("UTF-8 error: {0}")]
49    Utf8Error(#[from] std::str::Utf8Error),
50
51    /// Invalid response from terminal
52    #[error("Invalid response from terminal: {0}")]
53    InvalidResponse(String),
54
55    /// Protocol error
56    #[error("Protocol error: {0}")]
57    Protocol(String),
58}
59
60impl Error {
61    /// Create a new protocol error
62    pub fn protocol(msg: impl Into<String>) -> Self {
63        Self::Protocol(msg.into())
64    }
65
66    /// Create a new terminal error
67    pub fn terminal(msg: impl Into<String>) -> Self {
68        Self::TerminalError(msg.into())
69    }
70}