Skip to main content

hocon_rs/
object.rs

1use crate::join_format;
2use crate::value::Value;
3use std::collections::HashMap;
4use std::fmt::{Display, Formatter};
5use std::ops::{Deref, DerefMut};
6
7#[derive(Debug, Clone, PartialEq, Default)]
8pub struct Object(HashMap<String, Value>);
9
10impl Object {
11    pub fn new() -> Self {
12        Default::default()
13    }
14}
15
16impl Deref for Object {
17    type Target = HashMap<String, Value>;
18
19    fn deref(&self) -> &Self::Target {
20        &self.0
21    }
22}
23
24impl DerefMut for Object {
25    fn deref_mut(&mut self) -> &mut Self::Target {
26        &mut self.0
27    }
28}
29
30impl Display for Object {
31    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
32        join_format(
33            self.iter(),
34            f,
35            |f| write!(f, ", "),
36            |f, (k, v)| write!(f, "{k}: {v}"),
37        )
38    }
39}
40
41impl From<Object> for HashMap<String, Value> {
42    fn from(val: Object) -> Self {
43        val.0
44    }
45}
46
47impl FromIterator<(String, Value)> for Object {
48    fn from_iter<T: IntoIterator<Item = (String, Value)>>(iter: T) -> Self {
49        Self(HashMap::from_iter(iter))
50    }
51}