Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for unit enum desr #91

Merged
merged 4 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`"
)
}
}
Loading