Skip to content

Commit

Permalink
Merge pull request #91 from ohadravid/unit-enum-support
Browse files Browse the repository at this point in the history
Support for unit enum desr
  • Loading branch information
ohadravid authored Feb 27, 2024
2 parents fa4ef68 + f4ea5b1 commit 0c11c43
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/de/variant_de.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::{de::wbem_class_de::Deserializer, variant::Variant, WMIError};
use serde::{de, forward_to_deserialize_any, Deserialize};
use serde::{
de::{self, IntoDeserializer},
forward_to_deserialize_any, Deserialize,
};
use std::{fmt, vec::IntoIter};

#[derive(Debug)]
Expand Down Expand Up @@ -84,16 +87,19 @@ impl<'de> serde::Deserializer<'de> for Variant {
fn deserialize_enum<V>(
self,
name: &'static str,
fields: &'static [&'static str],
variants: &'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)
Deserializer::from_wbem_class_obj(o).deserialize_enum(name, variants, visitor)
}
Variant::String(str) => str
.into_deserializer()
.deserialize_enum(name, variants, visitor),
_ => self.deserialize_any(visitor),
}
}
Expand Down
45 changes: 45 additions & 0 deletions src/de/wbem_class_de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,4 +487,49 @@ mod tests {

assert!(matches!(proc.TargetInstance, Instance::Process(..)))
}

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

#[derive(Deserialize, Debug, PartialEq, Eq)]
enum Status {
SomeValue,
OK,
SomeOtherValue,
}

#[derive(Deserialize, Debug)]
struct Win32_OperatingSystem {
Status: Status,
}

let os: Win32_OperatingSystem = wmi_con.get().unwrap();

assert_eq!(os.Status, Status::OK);
}

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

#[derive(Deserialize, Debug, PartialEq, Eq)]
enum Status {
Potato,
}

#[derive(Deserialize, Debug)]
struct Win32_OperatingSystem {
Status: Status,
}

let res: Result<Win32_OperatingSystem, WMIError> = wmi_con.get();

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

assert_eq!(
format!("{}", err),
"unknown variant `OK`, expected `Potato`"
)
}
}

0 comments on commit 0c11c43

Please sign in to comment.