use alloc::string::String;
use zerodds_cdr::CorbaAny;
use crate::event::{Property, PropertySeq};
pub const EVENT_RELIABILITY: &str = "EventReliability";
pub const CONNECTION_RELIABILITY: &str = "ConnectionReliability";
pub const PRIORITY: &str = "Priority";
pub const ORDER_POLICY: &str = "OrderPolicy";
pub const DISCARD_POLICY: &str = "DiscardPolicy";
pub const MAX_EVENTS_PER_CONSUMER: &str = "MaxEventsPerConsumer";
pub const MAX_QUEUE_LENGTH: &str = "MaxQueueLength";
pub const MAX_CONSUMERS: &str = "MaxConsumers";
pub const MAX_SUPPLIERS: &str = "MaxSuppliers";
#[derive(Debug, Clone, Default, PartialEq)]
pub struct QoSProperties(pub PropertySeq);
impl QoSProperties {
#[must_use]
pub fn new() -> Self {
Self(PropertySeq::new())
}
pub fn set(&mut self, name: impl Into<String>, value: CorbaAny) {
let name = name.into();
if let Some(p) = self.0.iter_mut().find(|p| p.name == name) {
p.value = value;
} else {
self.0.push(Property::new(name, value));
}
}
#[must_use]
pub fn get(&self, name: &str) -> Option<&CorbaAny> {
self.0.iter().find(|p| p.name == name).map(|p| &p.value)
}
pub fn apply(&mut self, props: &PropertySeq) {
for p in props {
self.set(p.name.clone(), p.value.clone());
}
}
}
#[cfg(test)]
#[allow(clippy::expect_used, clippy::unwrap_used)]
mod tests {
use super::*;
use zerodds_cdr::AnyValue;
#[test]
fn qos_set_get_overwrite() {
let mut q = QoSProperties::new();
q.set(PRIORITY, CorbaAny(AnyValue::Short(5)));
assert_eq!(q.get(PRIORITY), Some(&CorbaAny(AnyValue::Short(5))));
q.set(PRIORITY, CorbaAny(AnyValue::Short(9)));
assert_eq!(q.get(PRIORITY), Some(&CorbaAny(AnyValue::Short(9))));
assert_eq!(q.0.len(), 1, "overwrite, no duplicate");
assert_eq!(q.get(EVENT_RELIABILITY), None);
}
}