haystack_types/
h_dict.rs

1use num::Float;
2use std::collections::HashMap;
3use crate::io::HBox;
4use crate::{HType, HVal, NumTrait};
5use std::fmt::{self,Write,Display};
6use std::str::FromStr;
7
8pub struct HDict<'a,T> {
9    inner: HashMap<String, HBox<'a,T>>
10}
11
12pub type Dict<'a,T> = HDict<'a,T>;
13
14const THIS_TYPE: HType = HType::Dict;
15
16impl <'a,T: NumTrait + 'a>HDict<'a,T> {
17    pub fn new() -> HDict<'a,T> {
18        HDict { inner: HashMap::new() }
19    }
20
21    pub fn from_map(map: HashMap<String, HBox<'a,T>>) -> HDict<'a,T> {
22        HDict { inner: map }
23    }
24
25    pub fn get(&self, key: &str) -> Option<&HBox<'a,T>> {
26        self.inner.get(key)
27    }
28}
29
30impl <'a,T: NumTrait + 'a>HVal<'a,T> for HDict<'a,T> {
31    fn to_zinc(&self, buf: &mut String) -> fmt::Result {
32        write!(buf,"{{")?;
33        let inner = &self.inner;
34        for (k,v) in inner.into_iter() {
35            match v.haystack_type() {
36                HType::Remove => write!(buf,"-{}",k),
37                HType::Marker => write!(buf,"{}",k),
38                _ => {
39                    write!(buf,"{}:",k)?;
40                    v.to_zinc(buf)?;
41                    write!(buf,",")
42                }
43            }?;
44        }
45        write!(buf,"}}")
46    }
47    fn to_json(&self, _buf: &mut String) -> fmt::Result {
48        unimplemented!()
49    }
50    fn haystack_type(&self) -> HType { THIS_TYPE }
51
52    fn _eq(&self, other: &dyn HVal<'a,T>) -> bool { false }
53    set_get_method!(get_dict_val, HDict<'a,T>);
54}