use std::sync::Arc;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Attribute {
Name(Arc<str>),
InputBuffer { initial: usize, max: usize },
Dispatcher(Arc<str>),
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Attributes {
attribute_list: Vec<Attribute>,
}
impl Attributes {
#[must_use]
pub fn none() -> Self {
Self::default()
}
#[must_use]
pub fn named(name: impl Into<String>) -> Self {
Self {
attribute_list: vec![Attribute::Name(Arc::from(name.into()))],
}
}
#[must_use]
pub fn input_buffer(initial: usize, max: usize) -> Self {
Self {
attribute_list: vec![Attribute::InputBuffer { initial, max }],
}
}
#[must_use]
pub fn dispatcher(name: impl Into<String>) -> Self {
Self {
attribute_list: vec![Attribute::Dispatcher(Arc::from(name.into()))],
}
}
#[must_use]
pub fn and(mut self, other: Self) -> Self {
if other.attribute_list.is_empty() {
return self;
}
if self.attribute_list.is_empty() {
return other;
}
let mut combined = other.attribute_list;
combined.extend(self.attribute_list);
self.attribute_list = combined;
self
}
#[must_use]
pub fn attribute_list(&self) -> &[Attribute] {
&self.attribute_list
}
#[must_use]
pub fn name(&self) -> Option<&str> {
self.attribute_list
.iter()
.find_map(|attribute| match attribute {
Attribute::Name(name) => Some(name.as_ref()),
_ => None,
})
}
#[must_use]
pub fn input_buffer_hint(&self) -> Option<(usize, usize)> {
self.attribute_list
.iter()
.find_map(|attribute| match attribute {
Attribute::InputBuffer { initial, max } => Some((*initial, *max)),
_ => None,
})
}
#[must_use]
pub fn dispatcher_hint(&self) -> Option<&str> {
self.attribute_list
.iter()
.find_map(|attribute| match attribute {
Attribute::Dispatcher(name) => Some(name.as_ref()),
_ => None,
})
}
#[must_use]
pub fn with_name(mut self, name: impl Into<String>) -> Self {
self.attribute_list
.insert(0, Attribute::Name(Arc::from(name.into())));
self
}
}