1use std::io;
2
3#[derive(Debug)]
5pub enum Error {
6 Io(io::Error),
8 Nom(nom::Err<nom::error::Error<String>>),
10 TrailingGarbage(String),
12 NoProto,
14 InputFile(String),
16 OutputFile(String),
18 OutputDirectory(String),
20 OutputMultipleInputs,
22 InvalidMessage(String),
24 InvalidImport(String),
26 EmptyRead,
28 MessageOrEnumNotFound(String),
30 InvalidDefaultEnum(String),
32 ReadFnMap,
34 Cycle(Vec<String>),
36 OutputAndOutputDir,
38}
39
40pub 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}