pjson_rs/
error.rs

1//! Error types for PJS operations
2
3/// Result type alias for PJS operations
4pub type Result<T> = std::result::Result<T, Error>;
5
6/// Main error type for PJS operations
7#[derive(Debug, Clone, thiserror::Error)]
8pub enum Error {
9    /// Invalid JSON syntax
10    #[error("Invalid JSON syntax at position {position}: {message}")]
11    InvalidJson {
12        /// Position in the input where error occurred
13        position: usize,
14        /// Error description
15        message: String,
16    },
17
18    /// Frame format error
19    #[error("Invalid frame format: {0}")]
20    InvalidFrame(String),
21
22    /// Schema validation error
23    #[error("Schema validation failed: {0}")]
24    SchemaValidation(String),
25
26    /// Semantic type mismatch
27    #[error("Semantic type mismatch: expected {expected}, got {actual}")]
28    SemanticTypeMismatch {
29        /// Expected semantic type
30        expected: String,
31        /// Actual semantic type
32        actual: String,
33    },
34
35    /// Buffer overflow or underflow
36    #[error("Buffer error: {0}")]
37    Buffer(String),
38
39    /// Memory allocation error
40    #[error("Memory allocation failed: {0}")]
41    Memory(String),
42
43    /// I/O error
44    #[error("I/O error: {0}")]
45    Io(String),
46
47    /// Connection failed error
48    #[error("Connection failed: {0}")]
49    ConnectionFailed(String),
50
51    /// Client error
52    #[error("Client error: {0}")]
53    ClientError(String),
54
55    /// Invalid session error
56    #[error("Invalid session: {0}")]
57    InvalidSession(String),
58
59    /// Invalid URL error
60    #[error("Invalid URL: {0}")]
61    InvalidUrl(String),
62
63    /// Serialization error
64    #[error("Serialization error: {0}")]
65    Serialization(String),
66
67    /// UTF-8 conversion error
68    #[error("UTF-8 conversion failed: {0}")]
69    Utf8(String),
70
71    /// Generic error for other cases
72    #[error("{0}")]
73    Other(String),
74}
75
76impl Error {
77    /// Create an invalid JSON error
78    pub fn invalid_json(position: usize, message: impl Into<String>) -> Self {
79        Self::InvalidJson {
80            position,
81            message: message.into(),
82        }
83    }
84
85    /// Create an invalid frame error
86    pub fn invalid_frame(message: impl Into<String>) -> Self {
87        Self::InvalidFrame(message.into())
88    }
89
90    /// Create a buffer error
91    pub fn buffer(message: impl Into<String>) -> Self {
92        Self::Buffer(message.into())
93    }
94
95    /// Create a memory error
96    pub fn memory(message: impl Into<String>) -> Self {
97        Self::Memory(message.into())
98    }
99
100    /// Create a generic error
101    pub fn other(message: impl Into<String>) -> Self {
102        Self::Other(message.into())
103    }
104}
105
106impl From<std::io::Error> for Error {
107    fn from(err: std::io::Error) -> Self {
108        Error::Io(err.to_string())
109    }
110}
111
112impl From<std::str::Utf8Error> for Error {
113    fn from(err: std::str::Utf8Error) -> Self {
114        Error::Utf8(err.to_string())
115    }
116}