Skip to main content

libvirt_xdr/
error.rs

1//! Error types for XDR serialization/deserialization.
2
3use std::fmt;
4
5/// Result type for XDR operations.
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Errors that can occur during XDR serialization/deserialization.
9#[derive(Debug, thiserror::Error)]
10pub enum Error {
11    /// Custom error message from serde.
12    #[error("{0}")]
13    Message(String),
14
15    /// Unexpected end of input.
16    #[error("unexpected end of input")]
17    Eof,
18
19    /// Invalid boolean value (must be 0 or 1).
20    #[error("invalid boolean value: {0}")]
21    InvalidBool(u32),
22
23    /// Invalid enum discriminant.
24    #[error("invalid enum discriminant: {0}")]
25    InvalidEnumDiscriminant(i32),
26
27    /// String is not valid UTF-8.
28    #[error("invalid UTF-8 string")]
29    InvalidUtf8,
30
31    /// String exceeds maximum length.
32    #[error("string length {0} exceeds maximum {1}")]
33    StringTooLong(usize, usize),
34
35    /// Array exceeds maximum length.
36    #[error("array length {0} exceeds maximum {1}")]
37    ArrayTooLong(usize, usize),
38
39    /// Trailing data after deserialization.
40    #[error("trailing data: {0} bytes remaining")]
41    TrailingData(usize),
42}
43
44impl serde::ser::Error for Error {
45    fn custom<T: fmt::Display>(msg: T) -> Self {
46        Error::Message(msg.to_string())
47    }
48}
49
50impl serde::de::Error for Error {
51    fn custom<T: fmt::Display>(msg: T) -> Self {
52        Error::Message(msg.to_string())
53    }
54}