Skip to main content

datum/
attributes.rs

1use std::sync::Arc;
2
3#[derive(Clone, Debug, PartialEq, Eq)]
4pub enum Attribute {
5    Name(Arc<str>),
6    InputBuffer { initial: usize, max: usize },
7    Dispatcher(Arc<str>),
8}
9
10#[derive(Clone, Debug, Default, PartialEq, Eq)]
11pub struct Attributes {
12    attribute_list: Vec<Attribute>,
13}
14
15impl Attributes {
16    #[must_use]
17    pub fn none() -> Self {
18        Self::default()
19    }
20
21    #[must_use]
22    pub fn named(name: impl Into<String>) -> Self {
23        Self {
24            attribute_list: vec![Attribute::Name(Arc::from(name.into()))],
25        }
26    }
27
28    #[must_use]
29    pub fn input_buffer(initial: usize, max: usize) -> Self {
30        Self {
31            attribute_list: vec![Attribute::InputBuffer { initial, max }],
32        }
33    }
34
35    #[must_use]
36    pub fn dispatcher(name: impl Into<String>) -> Self {
37        Self {
38            attribute_list: vec![Attribute::Dispatcher(Arc::from(name.into()))],
39        }
40    }
41
42    #[must_use]
43    pub fn and(mut self, other: Self) -> Self {
44        if other.attribute_list.is_empty() {
45            return self;
46        }
47        if self.attribute_list.is_empty() {
48            return other;
49        }
50        let mut combined = other.attribute_list;
51        combined.extend(self.attribute_list);
52        self.attribute_list = combined;
53        self
54    }
55
56    #[must_use]
57    pub fn attribute_list(&self) -> &[Attribute] {
58        &self.attribute_list
59    }
60
61    #[must_use]
62    pub fn name(&self) -> Option<&str> {
63        self.attribute_list
64            .iter()
65            .find_map(|attribute| match attribute {
66                Attribute::Name(name) => Some(name.as_ref()),
67                _ => None,
68            })
69    }
70
71    #[must_use]
72    pub fn input_buffer_hint(&self) -> Option<(usize, usize)> {
73        self.attribute_list
74            .iter()
75            .find_map(|attribute| match attribute {
76                Attribute::InputBuffer { initial, max } => Some((*initial, *max)),
77                _ => None,
78            })
79    }
80
81    #[must_use]
82    pub fn dispatcher_hint(&self) -> Option<&str> {
83        self.attribute_list
84            .iter()
85            .find_map(|attribute| match attribute {
86                Attribute::Dispatcher(name) => Some(name.as_ref()),
87                _ => None,
88            })
89    }
90
91    #[must_use]
92    pub fn with_name(mut self, name: impl Into<String>) -> Self {
93        self.attribute_list
94            .insert(0, Attribute::Name(Arc::from(name.into())));
95        self
96    }
97}