thot_core/runner/resources/
script_groups.rs

1//! [`ScriptAssociations`] grouped by `priority`.
2use super::script_association::ScriptAssociation;
3use std::collections::{HashMap, HashSet};
4
5/// A group of [`Script`]s.
6pub type ScriptGroup = HashSet<ScriptAssociation>;
7
8/// [`ScriptGroup`]s keyed by priority.
9pub struct ScriptGroups(HashMap<i32, ScriptGroup>);
10
11impl ScriptGroups {
12    pub fn new() -> Self {
13        Self(HashMap::new())
14    }
15}
16
17impl From<ScriptGroup> for ScriptGroups {
18    fn from(scripts: ScriptGroup) -> Self {
19        let mut groups = HashMap::new();
20        for assoc in scripts {
21            let p = assoc.priority();
22            if !groups.contains_key(&p) {
23                // create priority group if needed
24                groups.insert(p, HashSet::new());
25            }
26
27            // insert association into priority group
28            let p_group = groups.get_mut(&p).expect("priority group should exist");
29            p_group.insert(assoc);
30        }
31
32        Self(groups)
33    }
34}
35
36impl Into<Vec<(i32, ScriptGroup)>> for ScriptGroups {
37    fn into(self) -> Vec<(i32, ScriptGroup)> {
38        let mut v = self.0.into_iter().collect::<Vec<(i32, ScriptGroup)>>();
39        v.sort_by(|(p1, _g1), (p2, _g2)| p1.cmp(p2));
40
41        v
42    }
43}
44
45// impl<'a> IntoIterator for &'a ScriptGroups {
46//     type Item = (i32, ScriptGroup);
47//     type IntoIter = std::slice::Iter<'a, (i32, ScriptGroup)>;
48//
49//     fn into_iter(self) -> Self::IntoIter {
50//         let v: Vec<(i32, ScriptGroup)> = self.into();
51//         v.into_iter()
52//     }
53// }
54
55#[cfg(test)]
56#[path = "./script_groups_test.rs"]
57mod script_groups_test;