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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// Take a look at the license at the top of the repository in the LICENSE file.

use std::{borrow::Cow, default::Default};

use crate::{translate::*, variant::*, variant_type::*};

wrapper! {
    // rustdoc-stripper-ignore-next
    /// `VariantDict` is a mutable key/value store where the keys are always
    /// strings and the values are [`Variant`s](variant/struct.Variant.html).
    ///
    /// Variant dictionaries can easily be converted to/from `Variant`s of the
    /// appropriate type.  In `glib` terms, this is a variant of the form `"a{sv}"`.
    ///
    /// # Panics
    ///
    /// Note, pretty much all methods on this struct will panic if the
    /// [`end_unsafe()`](#method.end_unsafe) method was called on the instance.
    #[doc(alias = "GVariantDict")]
    pub struct VariantDict(Shared<ffi::GVariantDict>);

    match fn {
        ref => |ptr| ffi::g_variant_dict_ref(ptr),
        unref => |ptr| ffi::g_variant_dict_unref(ptr),
        type_ => || ffi::g_variant_dict_get_type(),
    }
}

impl VariantDict {
    // rustdoc-stripper-ignore-next
    /// Create a new `VariantDict` optionally populating it with the given `Variant`
    ///
    /// Since `Variant`s are immutable, this does not couple the `VariantDict` with
    /// the input `Variant`, instead the contents are copied into the `VariantDict`.
    ///
    /// # Panics
    ///
    /// This function will panic if the given `Variant` is not of the correct type.
    #[doc(alias = "g_variant_dict_new")]
    pub fn new(from_asv: Option<&Variant>) -> Self {
        if let Some(var) = from_asv {
            assert_eq!(var.type_(), VariantDict::static_variant_type());
        }
        unsafe { from_glib_full(ffi::g_variant_dict_new(from_asv.to_glib_none().0)) }
    }

    // rustdoc-stripper-ignore-next
    /// Check if this `VariantDict` contains the given key.
    ///
    /// Look up whether or not the given key is present, returning `true` if it
    /// is present in `self`.
    #[doc(alias = "g_variant_dict_contains")]
    pub fn contains(&self, key: &str) -> bool {
        unsafe {
            from_glib(ffi::g_variant_dict_contains(
                self.to_glib_none().0,
                key.to_glib_none().0,
            ))
        }
    }

    // rustdoc-stripper-ignore-next
    /// Look up a typed value from this `VariantDict`.
    ///
    /// The given `key` is looked up in `self`.
    ///
    /// This will return `None` if the `key` is not present in the dictionary,
    /// and an error if the key is present but with the wrong type.
    #[doc(alias = "g_variant_dict_lookup")]
    pub fn lookup<T: FromVariant>(&self, key: &str) -> Result<Option<T>, VariantTypeMismatchError> {
        self.lookup_value(key, None)
            .map(|v| Variant::try_get(&v))
            .transpose()
    }

    // rustdoc-stripper-ignore-next
    /// Look up and return a value from this `VariantDict`.
    ///
    /// The given `key` is looked up in `self`.  If `expected_type` is not
    /// `None` then it will be matched against the type of any found value.
    ///
    /// This will return `None` if the `key` is not present in the dictionary
    /// or if it is present but the type of the value does not match a given
    /// `expected_type`.  Otherwise, `Some(value)` will be returned where
    /// the `value` is an instance of [`Variant`](variant/struct.Variant.html).
    #[doc(alias = "g_variant_dict_lookup_value")]
    pub fn lookup_value(&self, key: &str, expected_type: Option<&VariantTy>) -> Option<Variant> {
        unsafe {
            from_glib_full(ffi::g_variant_dict_lookup_value(
                self.to_glib_none().0,
                key.to_glib_none().0,
                expected_type.to_glib_none().0,
            ))
        }
    }

    // rustdoc-stripper-ignore-next
    /// Insert a variant into the dictionary.
    ///
    /// The given `key`/`value` pair is inserted into `self`.  If a value
    /// was previously associated with `key` then it is overwritten.
    ///
    /// For convenience, you may use the [`insert()`](#method.insert) if
    /// you have a value which implements [`ToVariant`](variant/trait.ToVariant.html).
    #[doc(alias = "g_variant_dict_insert_value")]
    pub fn insert_value(&self, key: &str, value: &Variant) {
        unsafe {
            ffi::g_variant_dict_insert_value(
                self.to_glib_none().0,
                key.to_glib_none().0,
                value.to_glib_none().0,
            )
        }
    }

    // rustdoc-stripper-ignore-next
    /// Insert a value into the dictionary
    ///
    /// The given `key`/`value` pair is inserted into `self`.  If a value
    /// was previously associated with `key` then it is overwritten.
    ///
    /// This is a convenience method which automatically calls
    /// [`to_variant()`](variant/trait.ToVariant.html#method.to_variant) for you
    /// on the given value.
    ///
    /// If, on the other hand, you have a [`Variant`](variant/struct.Variant.html)
    /// instance already, you should use the [`insert_value()`](#method.insert_value)
    /// method instead.
    #[doc(alias = "g_variant_dict_insert_value")]
    pub fn insert(&self, key: &str, value: impl Into<Variant>) {
        unsafe {
            ffi::g_variant_dict_insert_value(
                self.to_glib_none().0,
                key.to_glib_none().0,
                value.into().to_glib_none().0,
            )
        }
    }

    // rustdoc-stripper-ignore-next
    /// Remove the given `key` from the dictionary.
    ///
    /// This removes the given `key` from the dictionary, releasing the reference
    /// on the associated value if one is present.
    ///
    /// If a `key`/`value` pair was removed from the dictionary, `true` is
    /// returned.  If `key` was not present then `false` is returned instead.
    #[doc(alias = "g_variant_dict_remove")]
    pub fn remove(&self, key: &str) -> bool {
        unsafe {
            from_glib(ffi::g_variant_dict_remove(
                self.to_glib_none().0,
                key.to_glib_none().0,
            ))
        }
    }

    // rustdoc-stripper-ignore-next
    /// Convert this dictionary to a [`Variant`](variant/struct.Variant.html)
    ///
    /// This method converts `self` into an instance of [`Variant`](variant/struct.Variant.html)
    /// but in doing so renders it very unsafe to use.
    ///
    /// # Safety
    ///
    /// After calling this, the underlying `GVariantDict` is in a state where
    /// the only valid operations to perform as reference ones.  As such
    /// any attempt to read/update the dictionary *will* fail and emit warnings
    /// of such.
    ///
    /// You should only use this function if the extra cost of the safe function
    /// is too much for your performance critical codepaths
    pub unsafe fn end_unsafe(&self) -> Variant {
        from_glib_none(ffi::g_variant_dict_end(self.to_glib_none().0))
    }

    // rustdoc-stripper-ignore-next
    /// Convert this dictionary to a [`Variant`](variant/struct.Variant.html)
    ///
    /// This method converts `self` into an instance of [`Variant`](variant/struct.Variant.html)
    /// and then reinitialises itself in order to be safe for further use.
    ///
    /// If you are certain that nothing other than disposing of references will
    /// be done after ending the instance, you can call the
    /// [`end_unsafe()`](#method.end_unsafe) method instead to avoid the unnecessary
    /// reinitialisation of the dictionary.
    pub fn end(&self) -> Variant {
        unsafe {
            let ret = self.end_unsafe();
            // Reinitialise the dict so that we can continue safely
            ffi::g_variant_dict_init(self.to_glib_none().0, None::<Variant>.to_glib_none().0);
            ret
        }
    }
}

impl Default for VariantDict {
    fn default() -> Self {
        Self::new(None)
    }
}

impl StaticVariantType for VariantDict {
    fn static_variant_type() -> Cow<'static, VariantTy> {
        Cow::Borrowed(VariantTy::VARDICT)
    }
}

impl ToVariant for VariantDict {
    fn to_variant(&self) -> Variant {
        self.end()
    }
}

impl From<VariantDict> for Variant {
    #[inline]
    fn from(d: VariantDict) -> Self {
        unsafe { d.end_unsafe() }
    }
}

impl FromVariant for VariantDict {
    fn from_variant(variant: &Variant) -> Option<Self> {
        if variant.type_() == VariantDict::static_variant_type() {
            Some(Self::new(Some(variant)))
        } else {
            None
        }
    }
}

impl From<Variant> for VariantDict {
    fn from(other: Variant) -> Self {
        Self::new(Some(&other))
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn create_destroy() {
        let _dict = VariantDict::new(None);
    }

    #[test]
    fn create_roundtrip() {
        let dict = VariantDict::default();
        let var: Variant = dict.to_variant();
        let _dict2: VariantDict = var.into();
    }

    #[test]
    fn create_populate_destroy() {
        let dict = VariantDict::default();
        dict.insert_value("one", &(1u8.to_variant()));
        assert_eq!(dict.lookup_value("one", None), Some(1u8.to_variant()));
    }

    #[test]
    fn create_populate_roundtrip() {
        let dict = VariantDict::default();
        dict.insert_value("one", &(1u8.to_variant()));
        let var: Variant = dict.to_variant();
        let dict = VariantDict::from_variant(&var).expect("Not a dict?");
        assert_eq!(dict.lookup_value("one", None), Some(1u8.to_variant()));
    }

    #[test]
    fn lookup() -> Result<(), Box<dyn std::error::Error>> {
        let dict = VariantDict::default();
        dict.insert_value("one", &(1u8.to_variant()));
        assert_eq!(dict.lookup::<u8>("one")?.unwrap(), 1u8);
        assert_eq!(
            dict.lookup::<String>("one").err().unwrap().actual,
            u8::static_variant_type()
        );
        assert!(dict.lookup::<u8>("two")?.is_none());
        Ok(())
    }

    #[test]
    fn create_populate_remove() {
        let dict = VariantDict::default();
        let empty_var = dict.to_variant();
        dict.insert("one", 1u64);
        assert!(dict.remove("one"));
        assert!(!dict.remove("one"));
        let var2 = dict.to_variant();
        assert_eq!(empty_var, var2);
    }
}