summaryrefslogtreecommitdiffstats
path: root/vendor/toml/src/edit.rs
blob: 90fb284ca01052ed30a592e4b70f56c3fc580271 (plain)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#[cfg(feature = "parse")]
pub(crate) mod de {
    pub(crate) use toml_edit::de::Error;
}

#[cfg(not(feature = "parse"))]
pub(crate) mod de {
    /// Errors that can occur when deserializing a type.
    #[derive(Debug, Clone, PartialEq, Eq)]
    pub struct Error {
        inner: String,
    }

    impl Error {
        /// Add key while unwinding
        pub fn add_key(&mut self, _key: String) {}

        /// What went wrong
        pub fn message(&self) -> &str {
            self.inner.as_str()
        }
    }

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

    impl std::fmt::Display for Error {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            self.inner.fmt(f)
        }
    }

    impl std::error::Error for Error {}
}

#[cfg(feature = "display")]
pub(crate) mod ser {
    pub(crate) use toml_edit::ser::Error;
}

#[cfg(not(feature = "display"))]
pub(crate) mod ser {
    #[derive(Debug, Clone, PartialEq, Eq)]
    #[non_exhaustive]
    pub(crate) enum Error {
        UnsupportedType(Option<&'static str>),
        UnsupportedNone,
        KeyNotString,
        Custom(String),
    }

    impl Error {
        pub(crate) fn custom<T>(msg: T) -> Self
        where
            T: std::fmt::Display,
        {
            Error::Custom(msg.to_string())
        }
    }

    impl serde::ser::Error for Error {
        fn custom<T>(msg: T) -> Self
        where
            T: std::fmt::Display,
        {
            Self::custom(msg)
        }
    }

    impl std::fmt::Display for Error {
        fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            match self {
                Self::UnsupportedType(Some(t)) => write!(formatter, "unsupported {t} type"),
                Self::UnsupportedType(None) => write!(formatter, "unsupported rust type"),
                Self::UnsupportedNone => "unsupported None value".fmt(formatter),
                Self::KeyNotString => "map key was not a string".fmt(formatter),
                Self::Custom(s) => s.fmt(formatter),
            }
        }
    }

    impl std::error::Error for Error {}
}