geop_ops_model/
records.rs1use std::collections::BTreeMap;
7
8use geop_core_math::geop_error::{GeopError, GeopResult};
9use geop_core_program::target::Target;
10
11#[derive(Clone, Debug, PartialEq)]
13pub struct Records<T>(BTreeMap<String, T>);
14
15impl<T: PartialEq + 'static> Target for Records<T> {
17 const CHAIN: bool = true;
18}
19
20impl<T> Default for Records<T> {
21 fn default() -> Self {
22 Self(BTreeMap::new())
23 }
24}
25
26impl<T> Records<T> {
27 pub fn add(&mut self, name: impl Into<String>, record: T) -> GeopResult<()> {
30 let name = name.into();
31 if self.0.contains_key(&name) {
32 return Err(GeopError::new(format!(
33 "the part has a record of {name:?} already"
34 )));
35 }
36 self.0.insert(name, record);
37 Ok(())
38 }
39
40 pub fn insert(&mut self, name: impl Into<String>, record: T) {
42 self.0.insert(name.into(), record);
43 }
44
45 pub fn remove(&mut self, name: &str) -> Option<T> {
48 self.0.remove(name)
49 }
50
51 pub fn get(&self, name: &str) -> Option<&T> {
53 self.0.get(name)
54 }
55
56 pub fn iter(&self) -> impl Iterator<Item = (&str, &T)> {
58 self.0.iter().map(|(name, record)| (name.as_str(), record))
59 }
60}