Skip to content

Commit

Permalink
Extend priority range syntax (#1731)
Browse files Browse the repository at this point in the history
  • Loading branch information
fuzzypixelz authored Jan 20, 2025
1 parent deab78e commit da1b0d3
Showing 1 changed file with 52 additions and 24 deletions.
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());
}
}

0 comments on commit da1b0d3

Please sign in to comment.