Skip to main content

zlink_names/
error.rs

1use core::fmt;
2
3/// The error of converting a string that isn't a valid Varlink name into one.
4///
5/// It says which kind of name the string was meant to be.
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7#[non_exhaustive]
8pub enum Error {
9    /// The string is not a valid Varlink field name.
10    InvalidFieldName,
11    /// The string is not a valid Varlink type name.
12    InvalidTypeName,
13    /// The string is not a valid Varlink interface name.
14    InvalidInterfaceName,
15}
16
17impl fmt::Display for Error {
18    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19        let msg = match self {
20            Self::InvalidFieldName => "Invalid Varlink field name",
21            Self::InvalidTypeName => "Invalid Varlink type name",
22            Self::InvalidInterfaceName => "Invalid Varlink interface name",
23        };
24
25        f.write_str(msg)
26    }
27}
28
29impl core::error::Error for Error {}
30
31#[cfg(test)]
32mod tests {
33    use super::*;
34    use alloc::string::ToString;
35
36    #[test]
37    fn display() {
38        assert_eq!(
39            Error::InvalidFieldName.to_string(),
40            "Invalid Varlink field name"
41        );
42        assert_eq!(
43            Error::InvalidTypeName.to_string(),
44            "Invalid Varlink type name"
45        );
46        assert_eq!(
47            Error::InvalidInterfaceName.to_string(),
48            "Invalid Varlink interface name"
49        );
50    }
51}