quack_builder/
errors.rs

1use std::io;
2
3/// An error enum
4#[derive(Debug)]
5pub enum Error {
6    /// IO error
7    Io(io::Error),
8    /// Nom Error
9    Nom(nom::Err<nom::error::Error<String>>),
10    /// Nom's other failure case; giving up in the middle of a file
11    TrailingGarbage(String),
12    /// No .proto file provided
13    NoProto,
14    /// Cannot read input file
15    InputFile(String),
16    /// Cannot read output file
17    OutputFile(String),
18    /// Cannot read output directory
19    OutputDirectory(String),
20    /// Multiple input files with `--output` argument
21    OutputMultipleInputs,
22    /// Invalid message
23    InvalidMessage(String),
24    /// Varint decoding error
25    InvalidImport(String),
26    /// Empty read
27    EmptyRead,
28    /// Enum or message not found
29    MessageOrEnumNotFound(String),
30    /// Invalid default enum
31    InvalidDefaultEnum(String),
32    /// Missing `read_fn` implementation for maps
33    ReadFnMap,
34    /// Cycle detected
35    Cycle(Vec<String>),
36    /// `--output` and `--output_directory` both used
37    OutputAndOutputDir,
38}
39
40/// A wrapper for `Result<T, Error>`
41pub type Result<T> = ::std::result::Result<T, Error>;
42
43impl From<io::Error> for Error {
44    fn from(e: io::Error) -> Error {
45        Error::Io(e)
46    }
47}
48
49impl std::error::Error for Error {
50    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
51        match self {
52            Error::Io(e) => Some(e),
53            Error::Nom(e) => Some(e),
54            _ => None,
55        }
56    }
57}
58
59impl std::fmt::Display for Error {
60    fn fmt(
61        &self,
62        f: &mut std::fmt::Formatter,
63    ) -> std::result::Result<(), std::fmt::Error> {
64        match self {
65            Error::Io(e) => write!(f, "{}", e),
66            Error::Nom(e) => write!(f, "{}", e),
67            Error::TrailingGarbage(s) => write!(f, "parsing abandoned near: {:?}", s),
68            Error::NoProto => write!(f, "No .proto file provided"),
69            Error::InputFile(file) => write!(f, "Cannot read input file '{}'", file),
70            Error::OutputFile(file) => write!(f, "Cannot read output file '{}'", file),
71            Error::OutputDirectory(dir) => write!(f, "Cannot read output directory '{}'", dir),
72            Error::OutputMultipleInputs => write!(f, "--output only allowed for single input file"),
73            Error::InvalidMessage(msg) => write!(
74                f,
75                "Message checks errored: {}\r\n\
76                Proto definition might be invalid or something got wrong in the parsing",
77                msg
78            ),
79            Error::InvalidImport(imp) => write!(
80                f,
81                "Cannot convert protobuf import into module import:: {}\r\n\
82                Import definition might be invalid, some characters may not be supported",
83                imp
84            ),
85            Error::EmptyRead => write!(
86                f,
87                "No message or enum were read;\
88                either definition might be invalid or there were only unsupported structures"
89            ),
90            Error::MessageOrEnumNotFound(me) => write!(f, "Could not find message or enum {}", me),
91            Error::InvalidDefaultEnum(en) => write!(
92                f,
93                "Enum field cannot be set to '{}', this variant does not exist",
94                en
95            ),
96            Error::ReadFnMap => write!(f, "There should be a special case for maps"),
97            Error::Cycle(msgs) => write!(
98                f,
99                "Messages {:?} are cyclic (missing an optional field)",
100                msgs
101            ),
102            Error::OutputAndOutputDir => {
103                write!(f, "only one of --output or --output_directory allowed")
104            }
105        }
106    }
107}