justcode_core/
error.rs

1//! Error types for justcode encoding/decoding.
2
3use thiserror::Error;
4
5/// Result type for justcode operations.
6pub type Result<T> = std::result::Result<T, JustcodeError>;
7
8/// Errors that can occur during encoding or decoding.
9#[derive(Error, Debug, Clone, PartialEq, Eq)]
10pub enum JustcodeError {
11    /// Not enough bytes to decode the value.
12    #[error("unexpected end of input: expected {expected} bytes, got {got}")]
13    UnexpectedEndOfInput { expected: usize, got: usize },
14
15    /// Size limit exceeded during decoding.
16    #[error("size limit exceeded: limit is {limit}, but {requested} bytes were requested")]
17    SizeLimitExceeded { limit: usize, requested: usize },
18
19    /// Invalid varint encoding.
20    #[error("invalid varint encoding")]
21    InvalidVarint,
22
23    /// Custom error message.
24    #[error("{0}")]
25    Custom(String),
26}
27
28impl JustcodeError {
29    /// Create a custom error with a message.
30    pub fn custom(msg: impl Into<String>) -> Self {
31        Self::Custom(msg.into())
32    }
33}
34
35#[cfg(test)]
36mod tests {
37    use super::*;
38
39    #[test]
40    fn test_error_display() {
41        let err = JustcodeError::UnexpectedEndOfInput {
42            expected: 10,
43            got: 5,
44        };
45        assert!(err.to_string().contains("unexpected end of input"));
46    }
47
48    #[test]
49    fn test_custom_error() {
50        let err = JustcodeError::custom("test error");
51        assert_eq!(err.to_string(), "test error");
52    }
53}
54