modewise_pipeline/
registry.rs

1use std::collections::BTreeMap;
2use std::sync::Arc;
3
4use crate::Operation;
5
6/// Operations available to pipelines, by name.
7#[derive(Default, Clone)]
8pub struct Registry {
9    operations: BTreeMap<&'static str, Arc<dyn Operation>>,
10}
11
12impl Registry {
13    pub fn new() -> Self {
14        Self::default()
15    }
16
17    /// Replaces any operation already registered under the same name.
18    pub fn register(&mut self, operation: Arc<dyn Operation>) {
19        self.operations
20            .insert(operation.descriptor().name, operation);
21    }
22
23    pub fn get(&self, name: &str) -> Option<Arc<dyn Operation>> {
24        self.operations.get(name).cloned()
25    }
26
27    pub fn names(&self) -> impl Iterator<Item = &'static str> + '_ {
28        self.operations.keys().copied()
29    }
30}
31
32#[cfg(test)]
33mod tests {
34    use crate::test_support::ops;
35
36    #[test]
37    fn lists_registered_names_in_order() {
38        let names: Vec<_> = ops().registry.names().collect();
39        assert_eq!(names, ["liar", "needs_modes", "pair", "scale", "split"]);
40    }
41}