mech_core/structures/
tuple.rs

1use crate::*;
2
3// Tuple ----------------------------------------------------------------------
4
5#[derive(Clone, Debug, PartialEq, Eq)]
6pub struct MechTuple {
7  pub elements: Vec<Box<Value>>
8}
9
10impl MechTuple {
11
12  #[cfg(feature = "pretty_print")]
13  pub fn to_html(&self) -> String {
14    let mut elements = Vec::new();
15    for element in &self.elements {
16      elements.push(element.to_html());
17    }
18    format!("<span class=\"mech-tuple\"><span class=\"mech-start-brace\">(</span>{}<span class=\"mech-end-brace\">)</span></span>", elements.join(", "))
19  }
20
21  pub fn get(&self, index: usize) -> Option<&Value> {
22    if index < self.elements.len() {
23      Some(self.elements[index].as_ref())
24    } else {
25      None
26    }
27  }
28
29  pub fn from_vec(elements: Vec<Value>) -> Self {
30    MechTuple{elements: elements.iter().map(|m| Box::new(m.clone())).collect::<Vec<Box<Value>>>()}
31  }
32
33  pub fn size(&self) -> usize {
34    self.elements.len()
35  }
36
37  pub fn kind(&self) -> ValueKind {
38    ValueKind::Tuple(self.elements.iter().map(|x| x.kind()).collect())
39  }
40
41  pub fn size_of(&self) -> usize {
42    self.elements.iter().map(|x| x.size_of()).sum()
43  }
44
45}
46
47#[cfg(feature = "pretty_print")]
48impl PrettyPrint for MechTuple {
49  fn pretty_print(&self) -> String {
50    let mut builder = Builder::default();
51    let string_elements: Vec<String> = self.elements.iter().map(|e| e.pretty_print()).collect::<Vec<String>>();
52    builder.push_record(string_elements);
53    let mut table = builder.build();
54    let style = Style::empty()
55      .top(' ')
56      .left('│')
57      .right('│')
58      .bottom(' ')
59      .vertical(' ')
60      .intersection_bottom('ʼ')
61      .corner_top_left('╭')
62      .corner_top_right('╮')
63      .corner_bottom_left('╰')
64      .corner_bottom_right('╯');
65    table.with(style);
66    format!("{table}")
67  }
68}
69
70impl Hash for MechTuple {
71  fn hash<H: Hasher>(&self, state: &mut H) {
72    for x in self.elements.iter() {
73        x.hash(state)
74    }
75  }
76}