diff --git a/commons/zenoh-protocol/src/core/mod.rs b/commons/zenoh-protocol/src/core/mod.rs index d3eeb68fae..92ffdd6c3a 100644 --- a/commons/zenoh-protocol/src/core/mod.rs +++ b/commons/zenoh-protocol/src/core/mod.rs @@ -357,7 +357,7 @@ impl Display for PriorityRange { } } -#[derive(Debug)] +#[derive(Debug, PartialEq)] pub enum InvalidPriorityRange { InvalidSyntax { found: String }, InvalidBound { message: String }, @@ -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}"), } } } @@ -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)), + } } } @@ -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()); + } +}