pb_rs/
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(&self, f: &mut std::fmt::Formatter) -> std::result::Result<(), std::fmt::Error> {
61        match self {
62            Error::Io(e) => write!(f, "{}", e),
63            Error::Nom(e) => write!(f, "{}", e),
64            Error::TrailingGarbage(s) => write!(f, "parsing abandoned near: {:?}", s),
65            Error::NoProto => write!(f, "No .proto file provided"),
66            Error::InputFile(file) => write!(f, "Cannot read input file '{}'", file),
67            Error::OutputFile(file) => write!(f, "Cannot read output file '{}'", file),
68            Error::OutputDirectory(dir) => write!(f, "Cannot read output directory '{}'", dir),
69            Error::OutputMultipleInputs => write!(f, "--output only allowed for single input file"),
70            Error::InvalidMessage(msg) => write!(
71                f,
72                "Message checks errored: {}\r\n\
73                Proto definition might be invalid or something got wrong in the parsing",
74                msg
75            ),
76            Error::InvalidImport(imp) => write!(
77                f,
78                "Cannot convert protobuf import into module import:: {}\r\n\
79                Import definition might be invalid, some characters may not be supported",
80                imp
81            ),
82            Error::EmptyRead => write!(
83                f,
84                "No message or enum were read;\
85                either definition might be invalid or there were only unsupported structures"
86            ),
87            Error::MessageOrEnumNotFound(me) => write!(f, "Could not find message or enum {}", me),
88            Error::InvalidDefaultEnum(en) => write!(
89                f,
90                "Enum field cannot be set to '{}', this variant does not exist",
91                en
92            ),
93            Error::ReadFnMap => write!(f, "There should be a special case for maps"),
94            Error::Cycle(msgs) => write!(
95                f,
96                "Messages {:?} are cyclic (missing an optional field)",
97                msgs
98            ),
99            Error::OutputAndOutputDir => {
100                write!(f, "only one of --output or --output_directory allowed")
101            }
102        }
103    }
104}