Coverage Report

Created: 2026-03-23 07:13

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