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

Extend priority range syntax #1731

Merged
merged 1 commit into from
Jan 20, 2025
Merged
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
76 changes: 52 additions & 24 deletions commons/zenoh-protocol/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ impl Display for PriorityRange {
}
}

#[derive(Debug)]
#[derive(Debug, PartialEq)]
pub enum InvalidPriorityRange {
InvalidSyntax { found: String },
InvalidBound { message: String },
Expand All @@ -366,8 +366,8 @@ pub enum InvalidPriorityRange {
impl Display for InvalidPriorityRange {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
InvalidPriorityRange::InvalidSyntax { found } => write!(f, "invalid PriorityRange string, expected an range of the form `start-end` but found {found}"),
InvalidPriorityRange::InvalidBound { message } => write!(f, "invalid PriorityRange bound: {message}"),
InvalidPriorityRange::InvalidSyntax { found } => write!(f, "invalid priority range string, expected an range of the form `start-end` but found {found}"),
InvalidPriorityRange::InvalidBound { message } => write!(f, "invalid priority range bound: {message}"),
}
}
}
Expand Down Expand Up @@ -396,27 +396,28 @@ impl FromStr for PriorityRange {
message: err.to_string(),
})?;

let end = metadata
.next()
.ok_or_else(|| InvalidPriorityRange::InvalidSyntax {
found: s.to_string(),
})?
.parse::<u8>()
.map(Priority::try_from)
.map_err(|err| InvalidPriorityRange::InvalidBound {
message: err.to_string(),
})?
.map_err(|err| InvalidPriorityRange::InvalidBound {
message: err.to_string(),
})?;

if metadata.next().is_some() {
return Err(InvalidPriorityRange::InvalidSyntax {
found: s.to_string(),
});
};

Ok(PriorityRange::new(start..=end))
match metadata.next() {
Some(slice) => {
let end = slice
.parse::<u8>()
.map(Priority::try_from)
.map_err(|err| InvalidPriorityRange::InvalidBound {
message: err.to_string(),
})?
.map_err(|err| InvalidPriorityRange::InvalidBound {
message: err.to_string(),
})?;

if metadata.next().is_some() {
return Err(InvalidPriorityRange::InvalidSyntax {
found: s.to_string(),
});
};

Ok(PriorityRange::new(start..=end))
}
None => Ok(PriorityRange::new(start..=start)),
}
}
}

Expand Down Expand Up @@ -574,3 +575,30 @@ pub enum CongestionControl {
impl CongestionControl {
pub const DEFAULT: Self = Self::Drop;
}

#[cfg(test)]
mod tests {
use core::str::FromStr;

use crate::core::{Priority, PriorityRange};

#[test]
fn test_priority_range() {
assert_eq!(
PriorityRange::from_str("2-3"),
Ok(PriorityRange::new(
Priority::InteractiveHigh..=Priority::InteractiveLow
))
);

assert_eq!(
PriorityRange::from_str("7"),
Ok(PriorityRange::new(
Priority::Background..=Priority::Background
))
);

assert!(PriorityRange::from_str("1-").is_err());
assert!(PriorityRange::from_str("-5").is_err());
}
}
Loading