1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use serde_json::{Map, Value};
use std::{
    borrow::Borrow,
    convert::{AsMut, AsRef},
    hash::Hash,
    iter::{FromIterator, IntoIterator},
};

#[derive(Clone, Debug)]
/// 通用类型,表示字符串 => 字符串的映射结构
pub struct StringMap(Value);

impl StringMap {
    #[allow(dead_code)]
    pub(crate) fn new(value: Value) -> Self {
        Self(value)
    }
}

impl Default for StringMap {
    #[inline]
    fn default() -> Self {
        Self(Value::Object(Default::default()))
    }
}

impl From<StringMap> for Value {
    #[inline]
    fn from(val: StringMap) -> Self {
        val.0
    }
}

impl AsRef<Value> for StringMap {
    #[inline]
    fn as_ref(&self) -> &Value {
        &self.0
    }
}

impl AsMut<Value> for StringMap {
    #[inline]
    fn as_mut(&mut self) -> &mut Value {
        &mut self.0
    }
}

impl StringMap {
    #[doc = "根据 Key 获取相应的不可变 String 引用"]
    pub fn get<Q: ?Sized>(&self, key: &Q) -> Option<&str>
    where
        String: Borrow<Q>,
        Q: Ord + Eq + Hash,
    {
        self.0
            .as_object()
            .and_then(|object| object.get(key))
            .and_then(|val| val.as_str())
    }

    #[doc = "根据 Key 设置 String 值"]
    pub fn insert(&mut self, key: String, new: String) -> Option<String> {
        self.0.as_object_mut().and_then(|object| {
            object.insert(key, new.into()).and_then(|old| match old {
                Value::String(s) => Some(s),
                _ => None,
            })
        })
    }

    #[doc = "根据 Key 获取相应的不可变 String 引用"]
    pub fn remove<Q: ?Sized>(&mut self, key: &Q) -> Option<String>
    where
        String: Borrow<Q>,
        Q: Ord + Eq + Hash,
    {
        self.0
            .as_object_mut()
            .and_then(|object| object.remove(key))
            .and_then(|val| match val {
                Value::String(s) => Some(s),
                _ => None,
            })
    }

    #[doc = "获取映射结构的元素数量"]
    pub fn len(&self) -> usize {
        self.0.as_object().unwrap().len()
    }

    #[doc = "映射结构是否为空"]
    pub fn is_empty(&self) -> bool {
        self.0.as_object().unwrap().is_empty()
    }
}

impl<T> From<T> for StringMap
where
    T: IntoIterator<Item = (String, String)>,
{
    #[inline]
    fn from(m: T) -> Self {
        Self(Value::Object(Map::from_iter(
            m.into_iter().map(|(key, value)| (key, Value::String(value))),
        )))
    }
}