Skip to main content

zlink_codegen/
error.rs

1/// Error types that can occur during Varlink interface code generation.
2#[derive(Debug)]
3pub enum Error {
4    /// An invalid argument was provided.
5    InvalidArgument,
6
7    /// An I/O error occurred during file operations.
8    Io(std::io::Error),
9
10    /// An error from the zlink-core library.
11    Zlink(zlink::Error),
12
13    /// Writing to the internal output buffer failed during code generation.
14    Fmt(std::fmt::Error),
15
16    /// `rustfmt` produced output that was not valid UTF-8.
17    InvalidUtf8(std::string::FromUtf8Error),
18}
19
20impl std::fmt::Display for Error {
21    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22        match self {
23            Self::InvalidArgument => write!(f, "Invalid argument provided"),
24            Self::Io(e) => write!(f, "I/O error: {e}"),
25            Self::Zlink(e) => write!(f, "Zlink error: {e}"),
26            Self::Fmt(e) => write!(f, "Formatting error: {e}"),
27            Self::InvalidUtf8(e) => write!(f, "Invalid UTF-8 from rustfmt: {e}"),
28        }
29    }
30}
31
32impl std::error::Error for Error {
33    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
34        match self {
35            Self::Io(e) => Some(e),
36            Self::Zlink(e) => Some(e),
37            Self::Fmt(e) => Some(e),
38            Self::InvalidUtf8(e) => Some(e),
39            Self::InvalidArgument => None,
40        }
41    }
42}
43
44impl From<std::io::Error> for Error {
45    fn from(e: std::io::Error) -> Self {
46        Self::Io(e)
47    }
48}
49
50impl From<zlink::Error> for Error {
51    fn from(e: zlink::Error) -> Self {
52        Self::Zlink(e)
53    }
54}
55
56impl From<std::fmt::Error> for Error {
57    fn from(e: std::fmt::Error) -> Self {
58        Self::Fmt(e)
59    }
60}
61
62impl From<std::string::FromUtf8Error> for Error {
63    fn from(e: std::string::FromUtf8Error) -> Self {
64        Self::InvalidUtf8(e)
65    }
66}