1 | // Take a look at the license at the top of the repository in the LICENSE file. |
2 | |
3 | use std::{borrow::Cow, default::Default}; |
4 | |
5 | use crate::{translate::*, variant::*, variant_type::*}; |
6 | |
7 | wrapper! { |
8 | // rustdoc-stripper-ignore-next |
9 | /// `VariantDict` is a mutable key/value store where the keys are always |
10 | /// strings and the values are [`Variant`s](variant/struct.Variant.html). |
11 | /// |
12 | /// Variant dictionaries can easily be converted to/from `Variant`s of the |
13 | /// appropriate type. In `glib` terms, this is a variant of the form `"a{sv}"`. |
14 | /// |
15 | /// # Panics |
16 | /// |
17 | /// Note, pretty much all methods on this struct will panic if the |
18 | /// [`end_unsafe()`](#method.end_unsafe) method was called on the instance. |
19 | #[doc (alias = "GVariantDict" )] |
20 | pub struct VariantDict(Shared<ffi::GVariantDict>); |
21 | |
22 | match fn { |
23 | ref => |ptr| ffi::g_variant_dict_ref(ptr), |
24 | unref => |ptr| ffi::g_variant_dict_unref(ptr), |
25 | type_ => || ffi::g_variant_dict_get_type(), |
26 | } |
27 | } |
28 | |
29 | impl VariantDict { |
30 | // rustdoc-stripper-ignore-next |
31 | /// Create a new `VariantDict` optionally populating it with the given `Variant` |
32 | /// |
33 | /// Since `Variant`s are immutable, this does not couple the `VariantDict` with |
34 | /// the input `Variant`, instead the contents are copied into the `VariantDict`. |
35 | /// |
36 | /// # Panics |
37 | /// |
38 | /// This function will panic if the given `Variant` is not of the correct type. |
39 | #[doc (alias = "g_variant_dict_new" )] |
40 | pub fn new(from_asv: Option<&Variant>) -> Self { |
41 | if let Some(var) = from_asv { |
42 | assert_eq!(var.type_(), VariantDict::static_variant_type()); |
43 | } |
44 | unsafe { from_glib_full(ffi::g_variant_dict_new(from_asv.to_glib_none().0)) } |
45 | } |
46 | |
47 | // rustdoc-stripper-ignore-next |
48 | /// Check if this `VariantDict` contains the given key. |
49 | /// |
50 | /// Look up whether or not the given key is present, returning `true` if it |
51 | /// is present in `self`. |
52 | #[doc (alias = "g_variant_dict_contains" )] |
53 | pub fn contains(&self, key: &str) -> bool { |
54 | unsafe { |
55 | from_glib(ffi::g_variant_dict_contains( |
56 | self.to_glib_none().0, |
57 | key.to_glib_none().0, |
58 | )) |
59 | } |
60 | } |
61 | |
62 | // rustdoc-stripper-ignore-next |
63 | /// Look up a typed value from this `VariantDict`. |
64 | /// |
65 | /// The given `key` is looked up in `self`. |
66 | /// |
67 | /// This will return `None` if the `key` is not present in the dictionary, |
68 | /// and an error if the key is present but with the wrong type. |
69 | #[doc (alias = "g_variant_dict_lookup" )] |
70 | pub fn lookup<T: FromVariant>(&self, key: &str) -> Result<Option<T>, VariantTypeMismatchError> { |
71 | self.lookup_value(key, None) |
72 | .map(|v| Variant::try_get(&v)) |
73 | .transpose() |
74 | } |
75 | |
76 | // rustdoc-stripper-ignore-next |
77 | /// Look up and return a value from this `VariantDict`. |
78 | /// |
79 | /// The given `key` is looked up in `self`. If `expected_type` is not |
80 | /// `None` then it will be matched against the type of any found value. |
81 | /// |
82 | /// This will return `None` if the `key` is not present in the dictionary |
83 | /// or if it is present but the type of the value does not match a given |
84 | /// `expected_type`. Otherwise, `Some(value)` will be returned where |
85 | /// the `value` is an instance of [`Variant`](variant/struct.Variant.html). |
86 | #[doc (alias = "g_variant_dict_lookup_value" )] |
87 | pub fn lookup_value(&self, key: &str, expected_type: Option<&VariantTy>) -> Option<Variant> { |
88 | unsafe { |
89 | from_glib_full(ffi::g_variant_dict_lookup_value( |
90 | self.to_glib_none().0, |
91 | key.to_glib_none().0, |
92 | expected_type.to_glib_none().0, |
93 | )) |
94 | } |
95 | } |
96 | |
97 | // rustdoc-stripper-ignore-next |
98 | /// Insert a variant into the dictionary. |
99 | /// |
100 | /// The given `key`/`value` pair is inserted into `self`. If a value |
101 | /// was previously associated with `key` then it is overwritten. |
102 | /// |
103 | /// For convenience, you may use the [`insert()`](#method.insert) if |
104 | /// you have a value which implements [`ToVariant`](variant/trait.ToVariant.html). |
105 | #[doc (alias = "g_variant_dict_insert_value" )] |
106 | pub fn insert_value(&self, key: &str, value: &Variant) { |
107 | unsafe { |
108 | ffi::g_variant_dict_insert_value( |
109 | self.to_glib_none().0, |
110 | key.to_glib_none().0, |
111 | value.to_glib_none().0, |
112 | ) |
113 | } |
114 | } |
115 | |
116 | // rustdoc-stripper-ignore-next |
117 | /// Insert a value into the dictionary |
118 | /// |
119 | /// The given `key`/`value` pair is inserted into `self`. If a value |
120 | /// was previously associated with `key` then it is overwritten. |
121 | /// |
122 | /// This is a convenience method which automatically calls |
123 | /// [`to_variant()`](variant/trait.ToVariant.html#method.to_variant) for you |
124 | /// on the given value. |
125 | /// |
126 | /// If, on the other hand, you have a [`Variant`](variant/struct.Variant.html) |
127 | /// instance already, you should use the [`insert_value()`](#method.insert_value) |
128 | /// method instead. |
129 | #[doc (alias = "g_variant_dict_insert_value" )] |
130 | pub fn insert(&self, key: &str, value: impl Into<Variant>) { |
131 | unsafe { |
132 | ffi::g_variant_dict_insert_value( |
133 | self.to_glib_none().0, |
134 | key.to_glib_none().0, |
135 | value.into().to_glib_none().0, |
136 | ) |
137 | } |
138 | } |
139 | |
140 | // rustdoc-stripper-ignore-next |
141 | /// Remove the given `key` from the dictionary. |
142 | /// |
143 | /// This removes the given `key` from the dictionary, releasing the reference |
144 | /// on the associated value if one is present. |
145 | /// |
146 | /// If a `key`/`value` pair was removed from the dictionary, `true` is |
147 | /// returned. If `key` was not present then `false` is returned instead. |
148 | #[doc (alias = "g_variant_dict_remove" )] |
149 | pub fn remove(&self, key: &str) -> bool { |
150 | unsafe { |
151 | from_glib(ffi::g_variant_dict_remove( |
152 | self.to_glib_none().0, |
153 | key.to_glib_none().0, |
154 | )) |
155 | } |
156 | } |
157 | |
158 | // rustdoc-stripper-ignore-next |
159 | /// Convert this dictionary to a [`Variant`](variant/struct.Variant.html) |
160 | /// |
161 | /// This method converts `self` into an instance of [`Variant`](variant/struct.Variant.html) |
162 | /// but in doing so renders it very unsafe to use. |
163 | /// |
164 | /// # Safety |
165 | /// |
166 | /// After calling this, the underlying `GVariantDict` is in a state where |
167 | /// the only valid operations to perform as reference ones. As such |
168 | /// any attempt to read/update the dictionary *will* fail and emit warnings |
169 | /// of such. |
170 | /// |
171 | /// You should only use this function if the extra cost of the safe function |
172 | /// is too much for your performance critical codepaths |
173 | pub unsafe fn end_unsafe(&self) -> Variant { |
174 | from_glib_none(ffi::g_variant_dict_end(self.to_glib_none().0)) |
175 | } |
176 | |
177 | // rustdoc-stripper-ignore-next |
178 | /// Convert this dictionary to a [`Variant`](variant/struct.Variant.html) |
179 | /// |
180 | /// This method converts `self` into an instance of [`Variant`](variant/struct.Variant.html) |
181 | /// and then reinitialises itself in order to be safe for further use. |
182 | /// |
183 | /// If you are certain that nothing other than disposing of references will |
184 | /// be done after ending the instance, you can call the |
185 | /// [`end_unsafe()`](#method.end_unsafe) method instead to avoid the unnecessary |
186 | /// reinitialisation of the dictionary. |
187 | pub fn end(&self) -> Variant { |
188 | unsafe { |
189 | let ret = self.end_unsafe(); |
190 | // Reinitialise the dict so that we can continue safely |
191 | ffi::g_variant_dict_init(self.to_glib_none().0, None::<Variant>.to_glib_none().0); |
192 | ret |
193 | } |
194 | } |
195 | } |
196 | |
197 | impl Default for VariantDict { |
198 | fn default() -> Self { |
199 | Self::new(from_asv:None) |
200 | } |
201 | } |
202 | |
203 | impl StaticVariantType for VariantDict { |
204 | fn static_variant_type() -> Cow<'static, VariantTy> { |
205 | Cow::Borrowed(VariantTy::VARDICT) |
206 | } |
207 | } |
208 | |
209 | impl ToVariant for VariantDict { |
210 | fn to_variant(&self) -> Variant { |
211 | self.end() |
212 | } |
213 | } |
214 | |
215 | impl From<VariantDict> for Variant { |
216 | #[inline ] |
217 | fn from(d: VariantDict) -> Self { |
218 | unsafe { d.end_unsafe() } |
219 | } |
220 | } |
221 | |
222 | impl FromVariant for VariantDict { |
223 | fn from_variant(variant: &Variant) -> Option<Self> { |
224 | if variant.type_() == VariantDict::static_variant_type() { |
225 | Some(Self::new(from_asv:Some(variant))) |
226 | } else { |
227 | None |
228 | } |
229 | } |
230 | } |
231 | |
232 | impl From<Variant> for VariantDict { |
233 | fn from(other: Variant) -> Self { |
234 | Self::new(from_asv:Some(&other)) |
235 | } |
236 | } |
237 | |
238 | #[cfg (test)] |
239 | mod test { |
240 | use super::*; |
241 | |
242 | #[test ] |
243 | fn create_destroy() { |
244 | let _dict = VariantDict::new(None); |
245 | } |
246 | |
247 | #[test ] |
248 | fn create_roundtrip() { |
249 | let dict = VariantDict::default(); |
250 | let var: Variant = dict.to_variant(); |
251 | let _dict2: VariantDict = var.into(); |
252 | } |
253 | |
254 | #[test ] |
255 | fn create_populate_destroy() { |
256 | let dict = VariantDict::default(); |
257 | dict.insert_value("one" , &(1u8.to_variant())); |
258 | assert_eq!(dict.lookup_value("one" , None), Some(1u8.to_variant())); |
259 | } |
260 | |
261 | #[test ] |
262 | fn create_populate_roundtrip() { |
263 | let dict = VariantDict::default(); |
264 | dict.insert_value("one" , &(1u8.to_variant())); |
265 | let var: Variant = dict.to_variant(); |
266 | let dict = VariantDict::from_variant(&var).expect("Not a dict?" ); |
267 | assert_eq!(dict.lookup_value("one" , None), Some(1u8.to_variant())); |
268 | } |
269 | |
270 | #[test ] |
271 | fn lookup() -> Result<(), Box<dyn std::error::Error>> { |
272 | let dict = VariantDict::default(); |
273 | dict.insert_value("one" , &(1u8.to_variant())); |
274 | assert_eq!(dict.lookup::<u8>("one" )?.unwrap(), 1u8); |
275 | assert_eq!( |
276 | dict.lookup::<String>("one" ).err().unwrap().actual, |
277 | u8::static_variant_type() |
278 | ); |
279 | assert!(dict.lookup::<u8>("two" )?.is_none()); |
280 | Ok(()) |
281 | } |
282 | |
283 | #[test ] |
284 | fn create_populate_remove() { |
285 | let dict = VariantDict::default(); |
286 | let empty_var = dict.to_variant(); |
287 | dict.insert("one" , 1u64); |
288 | assert!(dict.remove("one" )); |
289 | assert!(!dict.remove("one" )); |
290 | let var2 = dict.to_variant(); |
291 | assert_eq!(empty_var, var2); |
292 | } |
293 | } |
294 | |