Skip to main content

floria_plugin_sdk/data/expression/
map.rs

1use super::super::super::dispatch_bindings::*;
2
3use std::{
4    cmp::*,
5    collections::*,
6    fmt::{self, Write},
7    hash::*,
8};
9
10impl MapResource {
11    /// Into map.
12    pub fn into_map(self) -> Map {
13        self.into_inner()
14    }
15
16    /// Get map.
17    pub fn map(&self) -> &Map {
18        self.get()
19    }
20
21    /// Get map.
22    pub fn map_mut(&mut self) -> &mut Map {
23        self.get_mut()
24    }
25}
26
27//
28// Map
29//
30
31/// Map.
32#[derive(Clone, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
33pub struct Map {
34    /// Inner.
35    pub inner: BTreeMap<Expression, Expression>,
36}
37
38impl Map {
39    /// Get.
40    pub fn into_get<KeyT>(&self, key: KeyT) -> Option<&Expression>
41    where
42        KeyT: Into<Expression>,
43    {
44        self.inner.get(&key.into())
45    }
46}
47
48impl From<BTreeMap<Expression, Expression>> for Map {
49    fn from(inner: BTreeMap<Expression, Expression>) -> Self {
50        Self { inner }
51    }
52}
53
54impl GuestMapResource for Map {
55    fn new(key_value_pairs: Vec<(Expression, Expression)>) -> Self {
56        Self::from_iter(key_value_pairs)
57    }
58
59    fn inner(&self) -> Vec<(Expression, Expression)> {
60        self.inner.clone().into_iter().collect()
61    }
62
63    fn length(&self) -> u64 {
64        self.inner.len() as u64
65    }
66}
67
68impl FromIterator<(Expression, Expression)> for Map {
69    fn from_iter<IntoIteratorT>(iter: IntoIteratorT) -> Self
70    where
71        IntoIteratorT: IntoIterator<Item = (Expression, Expression)>,
72    {
73        Self::from(BTreeMap::from_iter(iter))
74    }
75}
76
77impl fmt::Display for Map {
78    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
79        formatter.write_char('{')?;
80
81        let mut iterator = self.inner.iter().peekable();
82        while let Some((key, value)) = iterator.next() {
83            fmt::Display::fmt(key, formatter)?;
84            formatter.write_char(':')?;
85            fmt::Display::fmt(value, formatter)?;
86            if iterator.peek().is_some() {
87                formatter.write_char(',')?;
88            }
89        }
90
91        formatter.write_char('}')
92    }
93}