bbox_core/
service_utils.rs1use std::collections::HashMap;
2
3pub struct NamedObjectStore<T> {
5 lookup: HashMap<String, T>,
7 default: Option<String>,
9}
10
11impl<T> Default for NamedObjectStore<T> {
12 fn default() -> Self {
13 NamedObjectStore {
14 lookup: HashMap::new(),
15 default: None,
16 }
17 }
18}
19
20impl<T> NamedObjectStore<T> {
21 pub fn add(&mut self, name: &str, ds: T) {
25 self.lookup.insert(name.to_string(), ds);
26 self.default.get_or_insert(name.to_string());
28 }
29
30 pub fn get(&self, name: &str) -> Option<&T> {
31 self.lookup.get(name)
32 }
33
34 pub fn get_mut(&mut self, name: &str) -> Option<&mut T> {
35 self.lookup.get_mut(name)
36 }
37
38 pub fn get_default(&self) -> Option<&T> {
39 let no_default = "".to_string();
40 let name = self.default.as_ref().unwrap_or(&no_default);
41 self.get(name)
42 }
43
44 pub fn get_default_mut(&mut self) -> Option<&mut T> {
45 let no_default = "".to_string();
46 let name = self.default.as_ref().unwrap_or(&no_default).clone();
47 self.get_mut(&name)
48 }
49
50 pub fn get_or_default(&self, name: Option<&str>) -> Option<&T> {
51 if let Some(name) = name {
52 self.get(name)
53 } else {
54 self.get_default()
55 }
56 }
57
58 pub fn get_or_default_mut(&mut self, name: Option<&str>) -> Option<&mut T> {
59 if let Some(name) = name {
60 self.get_mut(name)
61 } else {
62 self.get_default_mut()
63 }
64 }
65}