skip parsed line if invalid key:value

This commit is contained in:
MaxOhn
2023-02-09 07:24:28 +01:00
parent d15f3313d9
commit 9067759dc2
3 changed files with 15 additions and 9 deletions
-4
View File
@@ -11,8 +11,6 @@ pub enum ParseError {
IoError(IoError),
/// The initial data of an `.osu` file was incorrect.
IncorrectFileHeader,
/// Line in `.osu` was unexpectedly not of the form `key:value`.
BadLine,
/// Line in `.osu` that contains a slider was not in the proper format.
InvalidCurvePoints,
/// Expected a decimal number, got something else.
@@ -30,7 +28,6 @@ impl fmt::Display for ParseError {
Self::IncorrectFileHeader => {
write!(f, "expected `osu file format v` at file begin")
}
Self::BadLine => f.write_str("line not in `Key:Value` pattern"),
Self::InvalidCurvePoints => f.write_str("invalid curve point"),
Self::InvalidDecimalNumber => f.write_str("invalid float number"),
Self::InvalidMode => f.write_str("invalid mode"),
@@ -44,7 +41,6 @@ impl StdError for ParseError {
match self {
Self::IoError(inner) => Some(inner),
Self::IncorrectFileHeader => None,
Self::BadLine => None,
Self::InvalidCurvePoints => None,
Self::InvalidDecimalNumber => None,
Self::InvalidMode => None,
+8 -2
View File
@@ -115,7 +115,10 @@ macro_rules! parse_general_body {
break;
}
let (key, value) = $reader.split_colon().ok_or(ParseError::BadLine)?;
let (key, value) = match $reader.split_colon() {
Some(tuple) => tuple,
None => continue,
};
if key == b"Mode" {
mode = match value {
@@ -159,7 +162,10 @@ macro_rules! parse_difficulty_body {
break;
}
let (key, value) = $reader.split_colon().ok_or(ParseError::BadLine)?;
let (key, value) = match $reader.split_colon() {
Some(tuple) => tuple,
None => continue,
};
match key {
b"ApproachRate" => {
+7 -3
View File
@@ -186,11 +186,15 @@ impl<R> FileReader<R> {
/// Split the buffer at the first ':', then parse the second half into a string.
///
/// Returns `None` if there is no ':' or if the second half is invalid UTF-8.
/// Returns `None` if the second half is invalid UTF-8.
pub(crate) fn split_colon(&self) -> Option<(&[u8], &str)> {
let idx = self.buf.iter().position(|&byte| byte == b':')?;
let front = &self.buf[..idx];
let idx = match self.buf.iter().position(|&byte| byte == b':') {
Some(idx) => idx,
None => return Some((&self.buf, "")),
};
let back = std::str::from_utf8(&self.buf[idx + 1..]).ok()?;
let front = &self.buf[..idx];
Some((front, back.trim_start()))
}