mech_core/types/
atom.rs

1use crate::*;
2use super::*;
3
4use std::cmp::Ordering;
5
6#[cfg(feature = "atom")]
7#[derive(Clone, Debug)]
8pub struct MechAtom(pub (u64, Ref<Dictionary>));
9
10impl PartialEq<MechAtom> for MechAtom {
11  fn eq(&self, other: &MechAtom) -> bool {
12    &self.id() == &other.id()
13  }
14}
15
16impl Eq for MechAtom {}
17
18impl PartialOrd for MechAtom {
19  fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
20    self.id().partial_cmp(&other.id())
21  }
22}
23
24impl Ord for MechAtom {
25  fn cmp(&self, other: &Self) -> Ordering {
26    self.id().cmp(&other.id())
27  }
28}
29
30impl MechAtom {
31  pub fn id(&self) -> u64 {
32    self.0.0
33  }
34  pub fn name(&self) -> String {
35    let names_brrw = self.0.1.borrow();
36    names_brrw.get(&self.0.0).cloned().unwrap_or_else(|| format!("{}", emojify(&(self.0.0 as u16))))
37  }
38  pub fn dictionary(&self) -> Ref<Dictionary> {
39    self.0.1.clone()
40  }
41  pub fn new(id: u64) -> MechAtom {
42    let dict = Ref::new(Dictionary::new());
43    MechAtom((id, dict))
44  }
45  pub fn to_html(&self) -> String {
46    format!("<span class=\"mech-atom\"><span class=\"mech-atom-name\">{}</span></span>",self.name())
47  }
48
49}
50
51impl Hash for MechAtom {
52  fn hash<H: Hasher>(&self, state: &mut H) {
53    self.0 .0.hash(state);
54  }
55}
56
57impl fmt::Display for MechAtom {
58  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59    write!(f, "{}", self.pretty_print())
60  }
61}
62
63impl PrettyPrint for MechAtom {
64  fn pretty_print(&self) -> String {
65    let name = self.name();
66    format!(":{}", name)
67  }
68}