use alloc::string::String;
use alloc::vec::Vec;
pub type FeatureName = String;
pub type RepositoryId = String;
pub type FailureReason = u32;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Cookie {
pub cookie_value: Vec<u8>,
}
impl Cookie {
#[must_use]
pub fn new(cookie_value: Vec<u8>) -> Self {
Self { cookie_value }
}
#[must_use]
pub fn truncate_to_base(&self) -> Self {
Self {
cookie_value: self.cookie_value.clone(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PortDescription {
pub name: FeatureName,
pub type_id: RepositoryId,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FacetDescription {
pub base: PortDescription,
pub facet_ref: RepositoryId,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ConnectionDescription {
pub cookie: Cookie,
pub objref: RepositoryId,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ReceptacleDescription {
pub base: PortDescription,
pub is_multiple: bool,
pub connections: Vec<ConnectionDescription>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ConsumerDescription {
pub base: PortDescription,
pub consumer: RepositoryId,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EmitterDescription {
pub base: PortDescription,
pub consumer: RepositoryId,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SubscriberDescription {
pub cookie: Cookie,
pub consumer: RepositoryId,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PublisherDescription {
pub base: PortDescription,
pub consumers: Vec<SubscriberDescription>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ConfigValue {
pub name: FeatureName,
pub value: Vec<u8>,
}
#[cfg(test)]
#[allow(clippy::expect_used)]
mod tests {
use super::*;
#[test]
fn cookie_new_stores_octet_seq() {
let c = Cookie::new(alloc::vec![1, 2, 3]);
assert_eq!(c.cookie_value, alloc::vec![1, 2, 3]);
}
#[test]
fn cookie_truncate_preserves_octet_seq() {
let c = Cookie::new(alloc::vec![0xDE, 0xAD]);
let t = c.truncate_to_base();
assert_eq!(t.cookie_value, c.cookie_value);
}
#[test]
fn port_description_carries_name_and_type_id() {
let p = PortDescription {
name: String::from("foo"),
type_id: String::from("IDL:M/I:1.0"),
};
assert_eq!(p.name, "foo");
assert_eq!(p.type_id, "IDL:M/I:1.0");
}
#[test]
fn receptacle_description_supports_simplex_and_multiplex() {
let simplex = ReceptacleDescription {
base: PortDescription {
name: String::from("manager"),
type_id: String::from("IDL:Stock/StockManager:1.0"),
},
is_multiple: false,
connections: alloc::vec![],
};
assert!(!simplex.is_multiple);
assert!(simplex.connections.is_empty());
let multiplex = ReceptacleDescription {
base: PortDescription {
name: String::from("managers"),
type_id: String::from("IDL:Stock/StockManager:1.0"),
},
is_multiple: true,
connections: alloc::vec![
ConnectionDescription {
cookie: Cookie::new(alloc::vec![1]),
objref: String::from("ref-A"),
},
ConnectionDescription {
cookie: Cookie::new(alloc::vec![2]),
objref: String::from("ref-B"),
}
],
};
assert!(multiplex.is_multiple);
assert_eq!(multiplex.connections.len(), 2);
}
#[test]
fn publisher_description_can_have_multiple_subscribers() {
let p = PublisherDescription {
base: PortDescription {
name: String::from("ticker"),
type_id: String::from("IDL:Stock/Tick:1.0"),
},
consumers: alloc::vec![
SubscriberDescription {
cookie: Cookie::new(alloc::vec![10]),
consumer: String::from("sub-A"),
},
SubscriberDescription {
cookie: Cookie::new(alloc::vec![11]),
consumer: String::from("sub-B"),
}
],
};
assert_eq!(p.consumers.len(), 2);
}
#[test]
fn config_value_carries_name_and_marshaled_value() {
let cv = ConfigValue {
name: String::from("rate_hz"),
value: alloc::vec![0, 0, 0, 100],
};
assert_eq!(cv.name, "rate_hz");
assert_eq!(cv.value.len(), 4);
}
}