1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use std::fmt;

/// Error type for serde-kdl2 serialization and deserialization.
#[derive(Debug, thiserror::Error)]
pub enum Error {
    /// A KDL parsing error.
    #[error("KDL parse error: {0}")]
    Parse(#[from] kdl::KdlError),

    /// A serde (de)serialization error.
    #[error("{0}")]
    Message(String),

    /// The top-level type must be a struct or map.
    #[error("top-level type must be a struct or map")]
    TopLevelNotStruct,

    /// Expected a specific type but got something else.
    #[error("type mismatch: expected {expected}, got {got}")]
    TypeMismatch { expected: &'static str, got: String },

    /// A required field was missing.
    #[error("missing field: {0}")]
    MissingField(String),

    /// Integer overflow during conversion.
    #[error("integer {0} out of range for target type")]
    IntegerOutOfRange(i128),

    /// Enum variant not found or ambiguous.
    #[error("unknown enum variant: {0}")]
    UnknownVariant(String),

    /// Unsupported operation.
    #[error("unsupported: {0}")]
    Unsupported(String),
}

impl serde::de::Error for Error {
    fn custom<T: fmt::Display>(msg: T) -> Self {
        Error::Message(msg.to_string())
    }
}

impl serde::ser::Error for Error {
    fn custom<T: fmt::Display>(msg: T) -> Self {
        Error::Message(msg.to_string())
    }
}

pub type Result<T> = std::result::Result<T, Error>;