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

loot / loot-condition-interpreter / 8929667851

02 May 2024 07:54PM UTC coverage: 88.969% (-0.2%) from 89.211%
8929667851

push

github

Ortham
Update version numbers and changelog for v4.0.0

3694 of 4152 relevant lines covered (88.97%)

16.09 hits per line

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

59.62
/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
pub enum Error {
13
    ParsingIncomplete(MoreDataNeeded),
14
    // The string is the input that was not parsed.
15
    UnconsumedInput(String),
16
    /// The string is the input at which the error was encountered.
17
    ParsingError(String, ParsingErrorKind),
18
    PeParsingError(PathBuf, Box<dyn error::Error>),
19
    IoError(PathBuf, io::Error),
20
}
21

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

185
impl fmt::Display for ParsingErrorKind {
186
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4✔
187
        match self {
4✔
188
            ParsingErrorKind::InvalidRegexSyntax(s) => write!(f, "{}", s),
1✔
189
            ParsingErrorKind::InvalidRegexUnknown => write!(f, "Unknown regex parsing error"),
×
190
            ParsingErrorKind::InvalidCrc(e) => e.fmt(f),
1✔
191
            ParsingErrorKind::PathEndsInADirectorySeparator(p) => {
1✔
192
                write!(f, "\"{}\" ends in a directory separator", p.display())
1✔
193
            }
194
            ParsingErrorKind::PathIsNotInGameDirectory(p) => {
1✔
195
                write!(f, "\"{}\" is not in the game directory", p.display())
1✔
196
            }
197
            ParsingErrorKind::GenericParserError(e) => write!(f, "Error in parser: {}", e),
×
198
        }
199
    }
4✔
200
}
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