• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

loot / loot-condition-interpreter / 12956314244

24 Jan 2025 07:31PM UTC coverage: 89.302% (+0.4%) from 88.934%
12956314244

push

github

Ortham
Update versions and changelog for v5.0.0

3823 of 4281 relevant lines covered (89.3%)

14.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

59.8
/src/error.rs
1
use std::error;
2
use std::fmt;
3
use std::io;
4
use std::num::NonZeroUsize;
5
use std::num::ParseIntError;
6
use std::path::PathBuf;
7

8
use nom::error::ErrorKind;
9
use nom::Err;
10

11
#[derive(Debug)]
12
#[non_exhaustive]
13
pub enum Error {
14
    ParsingIncomplete(MoreDataNeeded),
15
    // The string is the input that was not parsed.
16
    UnconsumedInput(String),
17
    /// The string is the input at which the error was encountered.
18
    ParsingError(String, ParsingErrorKind),
19
    PeParsingError(PathBuf, Box<dyn error::Error>),
20
    IoError(PathBuf, io::Error),
21
}
22

23
fn escape<I: fmt::Display>(input: I) -> String {
4✔
24
    input.to_string().replace('"', "\\\"")
4✔
25
}
4✔
26

27
impl<I: fmt::Debug + fmt::Display> From<Err<ParsingError<I>>> for Error {
28
    fn from(error: Err<ParsingError<I>>) -> Self {
4✔
29
        match error {
×
30
            Err::Incomplete(nom::Needed::Unknown) => {
31
                Error::ParsingIncomplete(MoreDataNeeded::UnknownSize)
×
32
            }
33
            Err::Incomplete(nom::Needed::Size(size)) => {
×
34
                Error::ParsingIncomplete(MoreDataNeeded::Size(size))
×
35
            }
36
            Err::Error(e) | Err::Failure(e) => Error::ParsingError(escape(e.input), e.kind),
4✔
37
        }
38
    }
4✔
39
}
40

41
impl fmt::Display for Error {
42
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
10✔
43
        match self {
×
44
            Error::ParsingIncomplete(MoreDataNeeded::UnknownSize) => write!(
×
45
                f,
×
46
                "An unknown number of bytes of additional input was expected by the parser"
×
47
            ),
×
48
            Error::ParsingIncomplete(MoreDataNeeded::Size(size)) => write!(
×
49
                f,
×
50
                "{size} bytes of additional input was expected by the parser"
×
51
            ),
×
52
            Error::UnconsumedInput(i) => write!(
2✔
53
                f,
2✔
54
                "The parser did not consume the following input: \"{}\"",
2✔
55
                i
2✔
56
            ),
2✔
57
            Error::ParsingError(i, e) => write!(
4✔
58
                f,
4✔
59
                "An error was encountered while parsing the expression \"{}\": {}",
4✔
60
                i, e
4✔
61
            ),
4✔
62
            Error::PeParsingError(p, e) => write!(
2✔
63
                f,
2✔
64
                "An error was encountered while reading the version fields of \"{}\": {}",
2✔
65
                p.display(),
2✔
66
                e
2✔
67
            ),
2✔
68
            Error::IoError(p, e) => write!(
2✔
69
                f,
2✔
70
                "An error was encountered while accessing the path \"{}\": {}",
2✔
71
                p.display(),
2✔
72
                e
2✔
73
            ),
2✔
74
        }
75
    }
10✔
76
}
77

78
impl error::Error for Error {
79
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
×
80
        match self {
×
81
            Error::ParsingError(_, e) => Some(e),
×
82
            Error::PeParsingError(_, e) => Some(e.as_ref()),
×
83
            Error::IoError(_, e) => Some(e),
×
84
            _ => None,
×
85
        }
86
    }
×
87
}
88

89
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
90
pub enum MoreDataNeeded {
91
    /// It's not known how much more data are needed
92
    UnknownSize,
93
    /// Contains the number of bytes of data that are needed
94
    Size(NonZeroUsize),
95
}
96

97
#[derive(Debug)]
98
pub struct ParsingError<I: fmt::Debug + fmt::Display> {
99
    input: I,
100
    kind: ParsingErrorKind,
101
}
102

103
impl<I: fmt::Debug + fmt::Display> From<(I, ErrorKind)> for ParsingError<I> {
104
    fn from((input, kind): (I, ErrorKind)) -> Self {
×
105
        use nom::error::ParseError;
106
        ParsingError::from_error_kind(input, kind)
×
107
    }
×
108
}
109

110
impl<I: fmt::Debug + fmt::Display> From<nom::error::Error<I>> for ParsingError<I> {
111
    fn from(error: nom::error::Error<I>) -> Self {
546✔
112
        use nom::error::ParseError;
113
        ParsingError::from_error_kind(error.input, error.code)
546✔
114
    }
546✔
115
}
116

117
impl<I: fmt::Debug + fmt::Display> fmt::Display for ParsingError<I> {
118
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
×
119
        write!(
×
120
            f,
×
121
            "An error was encountered while parsing the expression \"{}\": {}",
×
122
            self.input, self.kind
×
123
        )
×
124
    }
×
125
}
126

127
impl<I: fmt::Debug + fmt::Display> error::Error for ParsingError<I> {
128
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
×
129
        self.kind.source()
×
130
    }
×
131
}
132

133
impl<I: fmt::Debug + fmt::Display> nom::error::ParseError<I> for ParsingError<I> {
134
    fn from_error_kind(input: I, kind: ErrorKind) -> Self {
546✔
135
        ParsingError {
546✔
136
            input,
546✔
137
            kind: ParsingErrorKind::GenericParserError(kind.description().to_string()),
546✔
138
        }
546✔
139
    }
546✔
140

141
    fn append(_: I, _: ErrorKind, other: Self) -> Self {
17✔
142
        other
17✔
143
    }
17✔
144
}
145

146
#[derive(Debug)]
147
pub enum ParsingErrorKind {
148
    InvalidRegexSyntax(String),
149
    InvalidRegexUnknown,
150
    InvalidCrc(ParseIntError),
151
    PathEndsInADirectorySeparator(PathBuf),
152
    PathIsNotInGameDirectory(PathBuf),
153
    GenericParserError(String),
154
}
155

156
impl ParsingErrorKind {
157
    pub fn at<I: fmt::Debug + fmt::Display>(self, input: I) -> ParsingError<I> {
15✔
158
        ParsingError { input, kind: self }
15✔
159
    }
15✔
160
}
161

162
impl From<regex::Error> for ParsingErrorKind {
163
    fn from(error: regex::Error) -> Self {
1✔
164
        match error {
1✔
165
            regex::Error::Syntax(s) => ParsingErrorKind::InvalidRegexSyntax(s),
1✔
166
            _ => ParsingErrorKind::InvalidRegexUnknown,
×
167
        }
168
    }
1✔
169
}
170

171
impl From<ParseIntError> for ParsingErrorKind {
172
    fn from(error: ParseIntError) -> Self {
1✔
173
        ParsingErrorKind::InvalidCrc(error)
1✔
174
    }
1✔
175
}
176

177
impl error::Error for ParsingErrorKind {
178
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
×
179
        match self {
×
180
            ParsingErrorKind::InvalidCrc(e) => Some(e),
×
181
            _ => None,
×
182
        }
183
    }
×
184
}
185

186
impl fmt::Display for ParsingErrorKind {
187
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4✔
188
        match self {
4✔
189
            ParsingErrorKind::InvalidRegexSyntax(s) => write!(f, "{}", s),
1✔
190
            ParsingErrorKind::InvalidRegexUnknown => write!(f, "Unknown regex parsing error"),
×
191
            ParsingErrorKind::InvalidCrc(e) => e.fmt(f),
1✔
192
            ParsingErrorKind::PathEndsInADirectorySeparator(p) => {
1✔
193
                write!(f, "\"{}\" ends in a directory separator", p.display())
1✔
194
            }
195
            ParsingErrorKind::PathIsNotInGameDirectory(p) => {
1✔
196
                write!(f, "\"{}\" is not in the game directory", p.display())
1✔
197
            }
198
            ParsingErrorKind::GenericParserError(e) => write!(f, "Error in parser: {}", e),
×
199
        }
200
    }
4✔
201
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc