1use thiserror::Error;
4
5pub type Result<T> = std::result::Result<T, JustcodeError>;
7
8#[derive(Error, Debug, Clone, PartialEq, Eq)]
10pub enum JustcodeError {
11 #[error("unexpected end of input: expected {expected} bytes, got {got}")]
13 UnexpectedEndOfInput { expected: usize, got: usize },
14
15 #[error("size limit exceeded: limit is {limit}, but {requested} bytes were requested")]
17 SizeLimitExceeded { limit: usize, requested: usize },
18
19 #[error("invalid varint encoding")]
21 InvalidVarint,
22
23 #[error("{0}")]
25 Custom(String),
26}
27
28impl JustcodeError {
29 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