wmi 0.4.1

WMI crate for rust.
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
use failure::format_err;
use serde::de::{
    self, DeserializeOwned, DeserializeSeed, EnumAccess, IntoDeserializer, MapAccess, Unexpected,
    VariantAccess, Visitor,
};
use serde::forward_to_deserialize_any;

use std::{iter::Peekable, ptr};
use winapi::um::oleauto::VariantClear;

use crate::error::Error;
use crate::result_enumerator::IWbemClassWrapper;

pub struct Deserializer<'a> {
    // This string starts with the input data and characters are truncated off
    // the beginning as data is parsed.
    pub wbem_class_obj: &'a IWbemClassWrapper,
}

impl<'a> Deserializer<'a> {
    pub fn from_wbem_class_obj(wbem_class_obj: &'a IWbemClassWrapper) -> Self {
        Deserializer { wbem_class_obj }
    }
}

pub fn from_wbem_class_obj<T>(wbem_class_obj: &IWbemClassWrapper) -> Result<T, Error>
where
    T: DeserializeOwned,
{
    let mut deserializer = Deserializer::from_wbem_class_obj(wbem_class_obj);
    let t = T::deserialize(&mut deserializer)?;

    Ok(t)
}

struct WMIEnum<'a, 'de: 'a> {
    de: &'a mut Deserializer<'de>,
}

impl<'a, 'de> WMIEnum<'a, 'de> {
    pub fn new(de: &'a mut Deserializer<'de>) -> Self {
        Self { de }
    }
}

impl<'de, 'a> EnumAccess<'de> for WMIEnum<'a, 'de> {
    type Error = Error;
    type Variant = Self;

    fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error>
    where
        V: DeserializeSeed<'de>,
    {
        let val = seed.deserialize(&mut *self.de)?;
        Ok((val, self))
    }
}

impl<'de, 'a> VariantAccess<'de> for WMIEnum<'a, 'de> {
    type Error = Error;

    // Newtype variants can be deserialized directly.
    fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, Self::Error>
    where
        T: DeserializeSeed<'de>,
    {
        seed.deserialize(self.de)
    }

    // All other possible enum variants are not supported.
    fn unit_variant(self) -> Result<(), Self::Error> {
        let unexp = Unexpected::UnitVariant;
        Err(de::Error::invalid_type(unexp, &"newtype variant"))
    }

    fn tuple_variant<V>(self, _len: usize, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: Visitor<'de>,
    {
        let unexp = Unexpected::TupleVariant;
        Err(de::Error::invalid_type(unexp, &"newtype variant"))
    }

    fn struct_variant<V>(
        self,
        _fields: &'static [&'static str],
        visitor: V,
    ) -> Result<V::Value, Self::Error>
    where
        V: Visitor<'de>,
    {
        let unexp = Unexpected::StructVariant;
        Err(de::Error::invalid_type(unexp, &"newtype variant"))
    }
}

struct WMIMapAccess<'a, 'de, S, I>
where
    S: AsRef<str>,
    I: Iterator<Item = S>,
{
    fields: Peekable<I>,
    de: &'a Deserializer<'de>,
}

impl<'a, 'de, S, I> WMIMapAccess<'a, 'de, S, I>
where
    S: AsRef<str>,
    I: Iterator<Item = S>,
{
    pub fn new(fields: I, de: &'a Deserializer<'de>) -> Self {
        Self {
            fields: fields.peekable(),
            de,
        }
    }
}

impl<'de, 'a, S, I> MapAccess<'de> for WMIMapAccess<'a, 'de, S, I>
where
    S: AsRef<str>,
    I: Iterator<Item = S>,
{
    type Error = Error;

    fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Self::Error>
    where
        K: DeserializeSeed<'de>,
    {
        if let Some(field) = self.fields.peek() {
            seed.deserialize(field.as_ref().into_deserializer())
                .map(Some)
        } else {
            Ok(None)
        }
    }

    fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Self::Error>
    where
        V: DeserializeSeed<'de>,
    {
        let current_field = self
            .fields
            .next()
            .ok_or(format_err!("Expected current field to not be None"))?;

        let property_value = self
            .de
            .wbem_class_obj
            .get_property(current_field.as_ref())
            .map_err(Error::from_err)?;

        seed.deserialize(property_value)
    }
}

impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
    type Error = Error;

    fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: Visitor<'de>,
    {
        Err(Error::from_err(format_err!(
            "Only structs and maps can be deserialized from WMI objects"
        )))
    }

    fn deserialize_enum<V>(
        mut self,
        _name: &'static str,
        _variants: &'static [&'static str],
        visitor: V,
    ) -> Result<V::Value, Self::Error>
    where
        V: Visitor<'de>,
    {
        visitor.visit_enum(WMIEnum::new(&mut self))
    }

    // When deserializing enums, return the object's class name as the expected enum variant.
    fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: Visitor<'de>,
    {
        let class_name = self.wbem_class_obj.class()?;
        visitor.visit_string(class_name)
    }

    // Support for deserializing `Wrapper(Win32_OperatingSystem)`.
    fn deserialize_newtype_struct<V>(
        self,
        _name: &'static str,
        visitor: V,
    ) -> Result<V::Value, Self::Error>
    where
        V: Visitor<'de>,
    {
        visitor.visit_newtype_struct(self)
    }

    fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: Visitor<'de>,
    {
        let fields = self.wbem_class_obj.list_properties()?;

        visitor.visit_map(WMIMapAccess::new(fields.iter(), &self))
    }

    fn deserialize_struct<V>(
        self,
        name: &'static str,
        fields: &'static [&'static str],
        visitor: V,
    ) -> Result<V::Value, Self::Error>
    where
        V: Visitor<'de>,
    {
        visitor.visit_map(WMIMapAccess::new(fields.iter(), &self))
    }

    forward_to_deserialize_any! {
        bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes
        byte_buf option unit unit_struct seq tuple
        tuple_struct ignored_any
    }
}

#[allow(non_snake_case)]
#[allow(non_camel_case_types)]
#[cfg(test)]
mod tests {
    use super::*;
    use crate::datetime::WMIDateTime;
    use crate::variant::Variant;
    use serde::Deserialize;
    use std::collections::HashMap;

    use crate::tests::fixtures::*;
    use std::process;

    #[test]
    fn it_works() {
        let wmi_con = wmi_con();

        #[derive(Deserialize, Debug)]
        struct Win32_OperatingSystem {
            Caption: String,
            Name: String,
            CurrentTimeZone: i16,
            Debug: bool,

            // This actually returns as an i32 from COM.
            EncryptionLevel: u32,
            ForegroundApplicationBoost: u8,

            LastBootUpTime: WMIDateTime,
        }

        let enumerator = wmi_con
            .exec_query_native_wrapper("SELECT * FROM Win32_OperatingSystem")
            .unwrap();

        for res in enumerator {
            let w = res.unwrap();

            let w: Win32_OperatingSystem = from_wbem_class_obj(&w).unwrap();

            assert!(w.Caption.contains("Microsoft "));
            assert!(w.Name.contains("Microsoft ") && w.Name.contains("Partition"));
            assert_eq!(w.Debug, false);
            assert_eq!(w.EncryptionLevel, 256);
            assert_eq!(w.ForegroundApplicationBoost, 2);
            assert_eq!(
                w.LastBootUpTime.0.timezone().local_minus_utc() / 60,
                w.CurrentTimeZone as i32
            );
        }
    }

    #[test]
    fn it_desr_into_map() {
        let wmi_con = wmi_con();

        let enumerator = wmi_con
            .exec_query_native_wrapper("SELECT * FROM Win32_OperatingSystem")
            .unwrap();

        for res in enumerator {
            let w = res.unwrap();

            let w: HashMap<String, Variant> = from_wbem_class_obj(&w).unwrap();

            match w.get("Caption").unwrap() {
                Variant::String(s) => assert!(s.starts_with("Microsoft Windows")),
                _ => assert!(false),
            }

            assert_eq!(*w.get("Debug").unwrap(), Variant::Bool(false));

            let langs = w.get("MUILanguages").unwrap();

            match langs {
                Variant::Array(langs) => {
                    assert!(langs.contains(&Variant::String("en-US".into())));
                }
                _ => assert!(false),
            }
        }
    }

    #[test]
    fn it_desr_into_map_with_selected_fields() {
        let wmi_con = wmi_con();

        let enumerator = wmi_con
            .exec_query_native_wrapper("SELECT Caption FROM Win32_OperatingSystem")
            .unwrap();

        for res in enumerator {
            let w = res.unwrap();

            let w: HashMap<String, Variant> = from_wbem_class_obj(&w).unwrap();

            match w.get("Caption").unwrap() {
                Variant::String(s) => assert!(s.starts_with("Microsoft Windows")),
                _ => assert!(false),
            }

            assert_eq!(w.get("Debug"), None);
        }
    }

    #[test]
    fn it_desr_array() {
        let wmi_con = wmi_con();

        #[derive(Deserialize, Debug)]
        struct Win32_ComputerSystem {
            BootStatus: Vec<i32>,
            Roles: Vec<String>,
        }

        let results: Vec<Win32_ComputerSystem> = wmi_con.query().unwrap();

        for res in results {
            assert_eq!(res.BootStatus.len(), 10);
            assert!(res.Roles.contains(&"NT".to_owned()));
        }
    }

    #[test]
    fn it_desr_option_string() {
        let wmi_con = wmi_con();

        #[derive(Deserialize, Debug)]
        pub struct Win32_Process {
            pub Name: String,
            pub CommandLine: Option<String>,
            pub ProcessID: u32,
        }

        let mut filters = HashMap::new();
        filters.insert("ProcessID".into(), 0.into());

        let system_proc: Win32_Process = wmi_con.filtered_query(&filters).unwrap().pop().unwrap();

        assert_eq!(system_proc.CommandLine, None);

        let mut filters = HashMap::new();
        filters.insert("ProcessID".into(), i64::from(process::id()).into());

        let current_proc: Win32_Process = wmi_con.filtered_query(&filters).unwrap().pop().unwrap();

        assert!(current_proc.CommandLine.is_some());
    }

    #[test]
    fn it_fail_to_desr_null_to_string() {
        // Values can return as Null / Empty from WMI.
        // It is impossible to `desr` such values to `String` (for example).
        // See `it_desr_option_string` on how to fix this error.
        let wmi_con = wmi_con();

        #[derive(Deserialize, Debug)]
        pub struct Win32_Process {
            pub Name: String,
            pub CommandLine: String,
            pub ProcessID: u32,
        }

        let mut filters = HashMap::new();
        filters.insert("ProcessID".into(), 0.into());

        let res: Result<Vec<Win32_Process>, _> = wmi_con.filtered_query(&filters);

        let err = res.err().unwrap();

        assert_eq!(
            format!("{}", err),
            "invalid type: Option value, expected a string"
        )
    }

    #[test]
    fn it_can_desr_newtype() {
        // Values can return as Null / Empty from WMI.
        // It is impossible to `desr` such values to `String` (for example).
        // See `it_desr_option_string` on how to fix this error.
        let wmi_con = wmi_con();

        #[derive(Deserialize, Debug)]
        pub struct Win32_Service {
            pub Name: String,
            pub PathName: Option<String>,
        }

        #[derive(Deserialize, Debug)]
        struct Wrapper(Win32_Service);

        let wrapped_service: Wrapper = wmi_con.get().unwrap();

        assert_ne!(&wrapped_service.0.Name, "")
    }

    #[test]
    fn it_can_desr_newtype_enum() {
        let wmi_con = wmi_con();

        #[derive(Deserialize, Debug)]
        pub struct Win32_UserAccount {
            pub __Path: String,
            pub Name: String,
        }

        #[derive(Deserialize, Debug)]
        pub struct Win32_SystemAccount {
            pub Name: String,
        }

        #[derive(Deserialize, Debug)]
        enum User {
            #[serde(rename = "Win32_SystemAccount")]
            System(Win32_SystemAccount),
            #[serde(rename = "Win32_UserAccount")]
            User(Win32_UserAccount),
        };

        let user: Win32_UserAccount = wmi_con.get().unwrap();

        let user_enum: User = wmi_con.get_by_path(&user.__Path).unwrap();

        match user_enum {
            User::System(_) => assert!(false),
            User::User(_) => assert!(true),
        };
    }
}