Skip to content

Commit

Permalink
Merge pull request #79 from ohadravid/add-support-for-enum-field
Browse files Browse the repository at this point in the history
Add support for enum fields
  • Loading branch information
ohadravid authored Jun 2, 2023
2 parents 86f8cf8 + 7bd5f15 commit 15c64b4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wmi"
version = "0.13.0"
version = "0.13.1"
authors = ["Ohad Ravid <[email protected]>"]
edition = "2021"
license = "MIT OR Apache-2.0"
Expand Down
19 changes: 18 additions & 1 deletion src/de/variant_de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,27 @@ impl<'de> serde::Deserializer<'de> for Variant {
}
}

fn deserialize_enum<V>(
self,
name: &'static str,
fields: &'static [&'static str],
visitor: V,
) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
match self {
Variant::Object(o) => {
Deserializer::from_wbem_class_obj(o).deserialize_enum(name, fields, visitor)
}
_ => self.deserialize_any(visitor),
}
}

forward_to_deserialize_any! {
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
bytes byte_buf unit unit_struct newtype_struct seq tuple
tuple_struct map enum identifier ignored_any
tuple_struct map identifier ignored_any
}
}

Expand Down
57 changes: 39 additions & 18 deletions src/de/wbem_class_de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,10 @@ mod tests {

use crate::duration::WMIDuration;
use crate::variant::Variant;
use crate::FilterValue;
use serde::Deserialize;
use std::collections::HashMap;
use std::time::Duration;

use crate::tests::fixtures::*;
use std::process;
Expand Down Expand Up @@ -435,35 +437,54 @@ mod tests {
}

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

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

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

#[derive(Deserialize, Debug)]
enum Instance {
#[serde(rename = "Win32_Process")]
Process(Win32_Process),
#[serde(rename = "Win32_Thread")]
Thread(Win32_Thread),
}

#[derive(Deserialize, Debug)]
enum User {
#[serde(rename = "Win32_SystemAccount")]
System(Win32_SystemAccount),
#[serde(rename = "Win32_UserAccount")]
User(Win32_UserAccount),
struct __InstanceCreationEvent {
TargetInstance: Instance,
}

let user: Win32_UserAccount = wmi_con.get().unwrap();
let mut filters_process = HashMap::new();

filters_process.insert(
"TargetInstance".to_owned(),
FilterValue::is_a::<Win32_Process>().unwrap(),
);

filters_process.insert(
"TargetInstance.Name".to_owned(),
FilterValue::String("ping.exe".to_owned()),
);

let mut instances_iter = wmi_con
.filtered_notification::<__InstanceCreationEvent>(
&filters_process,
Some(Duration::from_secs(1)),
)
.unwrap();

std::process::Command::new("ping.exe")
.arg("127.0.0.1")
.status()
.unwrap();

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

match user_enum {
User::System(_) => assert!(false),
User::User(_) => assert!(true),
};
assert!(matches!(proc.TargetInstance, Instance::Process(..)))
}
}

0 comments on commit 15c64b4

Please sign in to comment.