Skip to content

Commit

Permalink
ondisk: Add compat deserializer for FchConsoleOutMode.
Browse files Browse the repository at this point in the history
Fixes <#132>.
  • Loading branch information
daym committed May 6, 2024
1 parent d9de1bf commit d835923
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 1 deletion.
70 changes: 69 additions & 1 deletion src/ondisk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7218,14 +7218,82 @@ pub enum CcxSmtControl {
}

#[derive(Debug, PartialEq, FromPrimitive, ToPrimitive, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub enum FchConsoleOutMode {
Disabled = 0,
Enabled = 1,
}

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for FchConsoleOutMode {
fn deserialize<D>(
deserializer: D,
) -> std::result::Result<Self, <D as serde::Deserializer<'de>>::Error>
where
D: serde::Deserializer<'de>,
{
struct ModeVisitor;
impl<'de> serde::de::Visitor<'de> for ModeVisitor {
type Value = FchConsoleOutMode;
fn expecting(
&self,
formatter: &mut core::fmt::Formatter<'_>,
) -> core::fmt::Result {
formatter.write_str(
"an enum variant of FchConsoleOutMode or a number",
)
}
fn visit_str<E>(
self,
v: &str,
) -> core::result::Result<Self::Value, E>
where
E: serde::de::Error,
{
match v {
"Disabled" => Ok(FchConsoleOutMode::Disabled),
"Enabled" => Ok(FchConsoleOutMode::Enabled),
_ => {
Err(serde::de::Error::custom("an enum variant of FchConsoleOutMode or a number was expected"))
}
}
}

fn visit_i64<E>(
self,
value: i64,
) -> core::result::Result<Self::Value, E>
where
E: serde::de::Error,
{
match value {
0 => Ok(FchConsoleOutMode::Disabled),
1 => Ok(FchConsoleOutMode::Enabled),
_ => Err(serde::de::Error::custom("an enum variant of FchConsoleOutMode or a number was expected")),
}
}

fn visit_u64<E>(
self,
value: u64,
) -> core::result::Result<Self::Value, E>
where
E: serde::de::Error,
{
match value {
0 => Ok(FchConsoleOutMode::Disabled),
1 => Ok(FchConsoleOutMode::Enabled),
_ => Err(serde::de::Error::custom("an enum variant of FchConsoleOutMode or a number was expected")),
}
}
}

deserializer.deserialize_any(ModeVisitor)
}
}

#[derive(Debug, PartialEq, FromPrimitive, ToPrimitive, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
Expand Down
42 changes: 42 additions & 0 deletions tests/compat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#[cfg(feature = "serde")]
const CURRENT_CONFIG_STR: &str = "\"Disabled\"";

#[cfg(feature = "serde")]
const COMPAT_CONFIG_STR: &str = r#"0x00"#;

#[cfg(feature = "serde")]
const INVALID_CONFIG_STR: &str = "\"Disabledx\"";

#[cfg(feature = "serde")]
#[test]
#[allow(non_snake_case)]
fn test_current_FchConsoleOutMode() {
let mode: amd_apcb::FchConsoleOutMode =
serde_yaml::from_str(&CURRENT_CONFIG_STR)
.expect("configuration be valid JSON");
assert_eq!(mode, amd_apcb::FchConsoleOutMode::Disabled);
}

#[cfg(feature = "serde")]
#[test]
#[allow(non_snake_case)]
fn test_compat_FchConsoleOutMode() {
let mode: amd_apcb::FchConsoleOutMode =
serde_yaml::from_str(&COMPAT_CONFIG_STR)
.expect("configuration be valid JSON");
assert_eq!(mode, amd_apcb::FchConsoleOutMode::Disabled);
}

#[cfg(feature = "serde")]
#[test]
#[allow(non_snake_case)]
fn test_invalid_FchConsoleOutMode() {
match serde_yaml::from_str::<amd_apcb::FchConsoleOutMode>(
&INVALID_CONFIG_STR,
) {
Ok(_) => {
panic!("unexpected success");
}
Err(_) => {}
};
}

0 comments on commit d835923

Please sign in to comment.