Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parser: Allow connection line to be specified multiple times #18

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
/// A second URI line was found at the given line.
MultipleUris(usize),
/// A second connection line was found at the given line.
#[deprecated(note = "This is no longer considered an error.")]
MultipleConnections(usize),
/// A second time zone line was found at the given line.
MultipleTimeZones(usize),
Expand Down Expand Up @@ -100,6 +101,7 @@
write!(f, "Multiple session-information in line {}", line)
}
ParserError::MultipleUris(line) => write!(f, "Multiple URIs in line {}", line),
#[allow(deprecated)]
ParserError::MultipleConnections(line) => {
write!(f, "Multiple connections in line {}", line)
}
Expand Down Expand Up @@ -520,12 +522,7 @@

// Parse connection line:
// - Can exist not at all or exactly once per session
b'c' => parse_rejecting_duplicates(
&mut connection,
&line,
ParserError::MultipleConnections,
Connection::parse,
)?,
b'c' => parse_skipping_duplicates(&mut connection, &line, Connection::parse)?,

// Parse bandwidth lines:
// - Can exist not at all, once or multiple times
Expand Down Expand Up @@ -623,6 +620,19 @@
Ok(())
}

fn parse_skipping_duplicates<T, P: Fn(&Line) -> Result<T, ParserError>>(
value: &mut Option<T>,
line: &Line<'_>,
parser: P,
) -> Result<(), ParserError> {
if value.is_some() {
let _ = parser(line)?;
return Ok(());
}
*value = Some(parser(line)?);
Ok(())
}

// Field parser helpers on byte slice iterators
fn parse_str<'a>(
it: &mut impl Iterator<Item = &'a [u8]>,
Expand Down Expand Up @@ -704,7 +714,7 @@
"Line not in key=value format",
))
}
Some(i) if i == 1 => line[0],

Check failure on line 717 in src/parser.rs

View workflow job for this annotation

GitHub Actions / rustfmt-clippy

redundant guard

Check failure on line 717 in src/parser.rs

View workflow job for this annotation

GitHub Actions / rustfmt-clippy

redundant guard
_ => {
return Err(ParserError::InvalidLineFormat(
n,
Expand Down
Loading