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(
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}