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 { fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result32 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 ch, pos, 42 ) 43 } 44 ErrorKind::UnexpectedCharAfter(pos, ch) => { 45 write!(formatter, "unexpected character {:?} after {}", ch, pos) 46 } 47 ErrorKind::ExpectedCommaFound(pos, ch) => { 48 write!(formatter, "expected comma after {}, found {:?}", pos, ch) 49 } 50 ErrorKind::LeadingZero(pos) => { 51 write!(formatter, "invalid leading zero in {}", pos) 52 } 53 ErrorKind::Overflow(pos) => { 54 write!(formatter, "value of {} exceeds u64::MAX", pos) 55 } 56 ErrorKind::EmptySegment(pos) => { 57 write!(formatter, "empty identifier segment in {}", pos) 58 } 59 ErrorKind::IllegalCharacter(pos) => { 60 write!(formatter, "unexpected character in {}", pos) 61 } 62 ErrorKind::WildcardNotTheOnlyComparator(ch) => { 63 write!( 64 formatter, 65 "wildcard req ({}) must be the only comparator in the version req", 66 ch, 67 ) 68 } 69 ErrorKind::UnexpectedAfterWildcard => { 70 formatter.write_str("unexpected character after wildcard in version req") 71 } 72 ErrorKind::ExcessiveComparators => { 73 formatter.write_str("excessive number of version comparators") 74 } 75 } 76 } 77 } 78 79 impl Display for Position { fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result80 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { 81 formatter.write_str(match self { 82 Position::Major => "major version number", 83 Position::Minor => "minor version number", 84 Position::Patch => "patch version number", 85 Position::Pre => "pre-release identifier", 86 Position::Build => "build metadata", 87 }) 88 } 89 } 90 91 impl Debug for Error { fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result92 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { 93 formatter.write_str("Error(\"")?; 94 Display::fmt(self, formatter)?; 95 formatter.write_str("\")")?; 96 Ok(()) 97 } 98 } 99