1 use crate::parse::Error;
2 use core::fmt::{self, Debug, Display};
3 
4 pub(crate) enum ErrorKind {
5     UnexpectedEnd(Position),
6     UnexpectedChar(Position, char),
7     UnexpectedCharAfter(Position, char),
8     ExpectedCommaFound(Position, char),
9     LeadingZero(Position),
10     Overflow(Position),
11     EmptySegment(Position),
12     IllegalCharacter(Position),
13     UnexpectedAfterWildcard,
14     ExcessiveComparators,
15 }
16 
17 #[derive(Copy, Clone, Eq, PartialEq)]
18 pub(crate) enum Position {
19     Major,
20     Minor,
21     Patch,
22     Pre,
23     Build,
24 }
25 
26 #[cfg(feature = "std")]
27 #[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
28 impl std::error::Error for Error {}
29 
30 impl Display for Error {
fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result31     fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
32         match &self.kind {
33             ErrorKind::UnexpectedEnd(pos) => {
34                 write!(formatter, "unexpected end of input while parsing {}", pos)
35             }
36             ErrorKind::UnexpectedChar(pos, ch) => {
37                 write!(
38                     formatter,
39                     "unexpected character {:?} while parsing {}",
40                     ch, pos,
41                 )
42             }
43             ErrorKind::UnexpectedCharAfter(pos, ch) => {
44                 write!(formatter, "unexpected character {:?} after {}", ch, pos)
45             }
46             ErrorKind::ExpectedCommaFound(pos, ch) => {
47                 write!(formatter, "expected comma after {}, found {:?}", pos, ch)
48             }
49             ErrorKind::LeadingZero(pos) => {
50                 write!(formatter, "invalid leading zero in {}", pos)
51             }
52             ErrorKind::Overflow(pos) => {
53                 write!(formatter, "value of {} exceeds u64::MAX", pos)
54             }
55             ErrorKind::EmptySegment(pos) => {
56                 write!(formatter, "empty identifier segment in {}", pos)
57             }
58             ErrorKind::IllegalCharacter(pos) => {
59                 write!(formatter, "unexpected character in {}", pos)
60             }
61             ErrorKind::UnexpectedAfterWildcard => {
62                 formatter.write_str("unexpected character after wildcard in version req")
63             }
64             ErrorKind::ExcessiveComparators => {
65                 formatter.write_str("excessive number of version comparators")
66             }
67         }
68     }
69 }
70 
71 impl Display for Position {
fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result72     fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
73         formatter.write_str(match self {
74             Position::Major => "major version number",
75             Position::Minor => "minor version number",
76             Position::Patch => "patch version number",
77             Position::Pre => "pre-release identifier",
78             Position::Build => "build metadata",
79         })
80     }
81 }
82 
83 impl Debug for Error {
fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result84     fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
85         formatter.write_str("Error(\"")?;
86         Display::fmt(self, formatter)?;
87         formatter.write_str("\")")?;
88         Ok(())
89     }
90 }
91