From a1d788e482a593d63b22ef0b6128c57a26a0975b Mon Sep 17 00:00:00 2001 From: Ohad Ravid Date: Sat, 17 Feb 2024 21:14:42 +0200 Subject: [PATCH 1/3] 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); + } + } From 0540beef3bf02ed3efa00db8694440b62bdf981c Mon Sep 17 00:00:00 2001 From: Omer Lubin Date: Fri, 23 Feb 2024 22:27:51 +0200 Subject: [PATCH 2/3] Fix rustfmt for variant_de.rs --- src/de/variant_de.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/de/variant_de.rs b/src/de/variant_de.rs index 4c67283..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::{self, IntoDeserializer}, forward_to_deserialize_any, Deserialize}; +use serde::{ + de::{self, IntoDeserializer}, + forward_to_deserialize_any, Deserialize, +}; use std::{fmt, vec::IntoIter}; #[derive(Debug)] @@ -94,9 +97,9 @@ impl<'de> serde::Deserializer<'de> for Variant { Variant::Object(o) => { Deserializer::from_wbem_class_obj(o).deserialize_enum(name, variants, visitor) } - Variant::String(str) => { - str.into_deserializer().deserialize_enum(name, variants, visitor) - } + Variant::String(str) => str + .into_deserializer() + .deserialize_enum(name, variants, visitor), _ => self.deserialize_any(visitor), } } From 928d0823abcec9680f67c1f0542a598df0cbfdcf Mon Sep 17 00:00:00 2001 From: Omer Lubin Date: Fri, 23 Feb 2024 22:28:18 +0200 Subject: [PATCH 3/3] ADD fail-to-desr test for unit string enum desr --- src/de/wbem_class_de.rs | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/src/de/wbem_class_de.rs b/src/de/wbem_class_de.rs index 2c979f3..0334317 100644 --- a/src/de/wbem_class_de.rs +++ b/src/de/wbem_class_de.rs @@ -494,19 +494,42 @@ mod tests { #[derive(Deserialize, Debug, PartialEq, Eq)] enum Status { + SomeValue, OK, + SomeOtherValue, } - - #[derive(Deserialize, Debug)] + + #[derive(Deserialize, Debug)] struct Win32_OperatingSystem { Status: Status, } - let os: Win32_OperatingSystem = wmi_con - .get() - .unwrap(); + 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`" + ) + } }