From a1d788e482a593d63b22ef0b6128c57a26a0975b Mon Sep 17 00:00:00 2001 From: Ohad Ravid Date: Sat, 17 Feb 2024 21:14:42 +0200 Subject: [PATCH] Added support for unit enum desr --- src/de/variant_de.rs | 9 ++++++--- src/de/wbem_class_de.rs | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/de/variant_de.rs b/src/de/variant_de.rs index 8af128c..4c67283 100644 --- a/src/de/variant_de.rs +++ b/src/de/variant_de.rs @@ -1,5 +1,5 @@ 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 +84,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,7 +92,10 @@ 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..2c979f3 100644 --- a/src/de/wbem_class_de.rs +++ b/src/de/wbem_class_de.rs @@ -487,4 +487,26 @@ 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 { + OK, + } + + #[derive(Deserialize, Debug)] + struct Win32_OperatingSystem { + Status: Status, + } + + let os: Win32_OperatingSystem = wmi_con + .get() + .unwrap(); + + assert_eq!(os.Status, Status::OK); + } + }