| 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 |
WildcardNotTheOnlyComparator(char), |
| 14 |
UnexpectedAfterWildcard, |
| 15 |
ExcessiveComparators, |
| 16 |
} |
| 17 |
|
| 18 |
#[derive(Copy, Clone, Eq, PartialEq)] |
| 19 |
pub(crate) enum Position { |
| 20 |
Major, |
| 21 |
Minor, |
| 22 |
Patch, |
| 23 |
Pre, |
| 24 |
Build, |
| 25 |
} |
| 26 |
|
| 27 |
#[cfg(feature = "std")] |
| 28 |
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))] |
| 29 |
impl std::error::Error for Error {} |
| 30 |
|
| 31 |
impl Display for Error { |
| 32 |
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 33 |
match &self.kind { |
| 34 |
ErrorKind::UnexpectedEnd(pos) => { |
| 35 |
write!(formatter, "unexpected end of input while parsing {}", pos) |
| 36 |
} |
| 37 |
ErrorKind::UnexpectedChar(pos, ch) => { |
| 38 |
write!( |
| 39 |
formatter, |
| 40 |
"unexpected character {} while parsing {}", |
| 41 |
QuotedChar(*ch), |
| 42 |
pos, |
| 43 |
) |
| 44 |
} |
| 45 |
ErrorKind::UnexpectedCharAfter(pos, ch) => { |
| 46 |
write!( |
| 47 |
formatter, |
| 48 |
"unexpected character {} after {}", |
| 49 |
QuotedChar(*ch), |
| 50 |
pos, |
| 51 |
) |
| 52 |
} |
| 53 |
ErrorKind::ExpectedCommaFound(pos, ch) => { |
| 54 |
write!( |
| 55 |
formatter, |
| 56 |
"expected comma after {}, found {}", |
| 57 |
pos, |
| 58 |
QuotedChar(*ch), |
| 59 |
) |
| 60 |
} |
| 61 |
ErrorKind::LeadingZero(pos) => { |
| 62 |
write!(formatter, "invalid leading zero in {}", pos) |
| 63 |
} |
| 64 |
ErrorKind::Overflow(pos) => { |
| 65 |
write!(formatter, "value of {} exceeds u64::MAX", pos) |
| 66 |
} |
| 67 |
ErrorKind::EmptySegment(pos) => { |
| 68 |
write!(formatter, "empty identifier segment in {}", pos) |
| 69 |
} |
| 70 |
ErrorKind::IllegalCharacter(pos) => { |
| 71 |
write!(formatter, "unexpected character in {}", pos) |
| 72 |
} |
| 73 |
ErrorKind::WildcardNotTheOnlyComparator(ch) => { |
| 74 |
write!( |
| 75 |
formatter, |
| 76 |
"wildcard req ({}) must be the only comparator in the version req", |
| 77 |
ch, |
| 78 |
) |
| 79 |
} |
| 80 |
ErrorKind::UnexpectedAfterWildcard => { |
| 81 |
formatter.write_str("unexpected character after wildcard in version req") |
| 82 |
} |
| 83 |
ErrorKind::ExcessiveComparators => { |
| 84 |
formatter.write_str("excessive number of version comparators") |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
impl Display for Position { |
| 91 |
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 92 |
formatter.write_str(match self { |
| 93 |
Position::Major => "major version number", |
| 94 |
Position::Minor => "minor version number", |
| 95 |
Position::Patch => "patch version number", |
| 96 |
Position::Pre => "pre-release identifier", |
| 97 |
Position::Build => "build metadata", |
| 98 |
}) |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
impl Debug for Error { |
| 103 |
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 104 |
formatter.write_str("Error(\"")?; |
| 105 |
Display::fmt(self, formatter)?; |
| 106 |
formatter.write_str("\")")?; |
| 107 |
Ok(()) |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
struct QuotedChar(char); |
| 112 |
|
| 113 |
impl Display for QuotedChar { |
| 114 |
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 115 |
// Standard library versions prior to https://github.com/rust-lang/rust/pull/95345 |
| 116 |
// print character 0 as '\u{0}'. We prefer '\0' to keep error messages |
| 117 |
// the same across all supported Rust versions. |
| 118 |
if self.0 == '\0' { |
| 119 |
formatter.write_str("'\\0'") |
| 120 |
} else { |
| 121 |
write!(formatter, "{:?}", self.0) |
| 122 |
} |
| 123 |
} |
| 124 |
} |