Skip to main content

ytsaurus_yson/
error.rs

1use std::fmt::Display;
2
3use serde::de;
4use thiserror::Error;
5
6/// Errors that can occur during YSON serialization or deserialization.
7#[derive(Error, Clone, Debug, PartialEq)]
8pub enum YsonError {
9    /// Reached the end of the input stream gracefully.
10    #[error("End of input")]
11    Eof,
12
13    /// Reached the end of the input unexpectedly (e.g., in the middle of a string).
14    /// Contains the byte position where the EOF was encountered.
15    #[error("Unexpected end of input at position {0}")]
16    UnexpectedEof(usize),
17
18    /// Encountered a byte that is not a valid YSON marker.
19    /// Contains the invalid byte and its position.
20    #[error("Invalid binary marker 0x{0:x} at position {1}")]
21    InvalidMarker(u8, usize),
22
23    /// Failed to parse a variable-length integer (varint).
24    /// Contains the starting position of the malformed varint.
25    #[error("Malformed varint at position {0}")]
26    MalformedVarint(usize),
27
28    /// Encountered a string that is not valid UTF-8.
29    /// Contains the starting position of the invalid string.
30    #[error("Invalid UTF-8 string at position {0}")]
31    InvalidUtf8(usize),
32
33    /// Found a token that does not match the expected YSON structure.
34    #[error("Expected {expected}, found {found} at position {pos}")]
35    UnexpectedToken {
36        /// A description of what the parser was looking for.
37        expected: &'static str,
38        /// A string representation of the token that was actually found.
39        found: String,
40        /// The byte position of the unexpected token.
41        pos: usize,
42    },
43
44    /// A catch-all for custom errors produced by `serde` or the user's data types.
45    #[error("Custom error from serde: {0}")]
46    Custom(String),
47}
48
49impl de::Error for YsonError {
50    fn custom<T: Display>(msg: T) -> Self {
51        YsonError::Custom(msg.to_string())
52    }
53}
54
55impl serde::ser::Error for YsonError {
56    fn custom<T: Display>(msg: T) -> Self {
57        YsonError::Custom(msg.to_string())
58    }
59}