diff --git a/src/de/variant_de.rs b/src/de/variant_de.rs index 8af128c..da5e1d4 100644 --- a/src/de/variant_de.rs +++ b/src/de/variant_de.rs @@ -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)] @@ -84,7 +87,7 @@ impl<'de> serde::Deserializer<'de> for Variant { fn deserialize_enum( self, name: &'static str, - fields: &'static [&'static str], + variants: &'static [&'static str], visitor: V, ) -> Result where @@ -92,8 +95,11 @@ impl<'de> serde::Deserializer<'de> for Variant { { 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), } } diff --git a/src/de/wbem_class_de.rs b/src/de/wbem_class_de.rs index 651fd1b..0334317 100644 --- a/src/de/wbem_class_de.rs +++ b/src/de/wbem_class_de.rs @@ -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 = wmi_con.get(); + + let err = res.err().unwrap(); + + assert_eq!( + format!("{}", err), + "unknown variant `OK`, expected `Potato`" + ) + } }