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 authored and Danny Milosavljevic committed May 6, 2024
1 parent d9de1bf commit 8c774bd
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "amd-apcb"
version = "0.3.1"
version = "0.3.2"
authors = ["Oxide Computer"]
edition = "2021"

Expand Down
57 changes: 56 additions & 1 deletion src/ondisk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7218,14 +7218,69 @@ 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: serde::Deserializer<'de>>(
deserializer: D,
) -> std::result::Result<Self, D::Error> {
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("'Disabled', 'Enabled', 0 or 1")
}
fn visit_str<E: serde::de::Error>(
self,
v: &str,
) -> core::result::Result<Self::Value, E> {
match v {
"Disabled" => Ok(FchConsoleOutMode::Disabled),
"Enabled" => Ok(FchConsoleOutMode::Enabled),
_ => Err(serde::de::Error::custom(
"'Disabled', 'Enabled', 0 or 1 was expected",
)),
}
}
fn visit_i64<E: serde::de::Error>(
self,
value: i64,
) -> core::result::Result<Self::Value, E> {
match value {
0 => Ok(FchConsoleOutMode::Disabled),
1 => Ok(FchConsoleOutMode::Enabled),
_ => Err(serde::de::Error::custom(
"'Disabled', 'Enabled', 0 or 1 was expected",
)),
}
}
fn visit_u64<E: serde::de::Error>(
self,
value: u64,
) -> core::result::Result<Self::Value, E> {
match value {
0 => Ok(FchConsoleOutMode::Disabled),
1 => Ok(FchConsoleOutMode::Enabled),
_ => Err(serde::de::Error::custom(
"'Disabled', 'Enabled', 0 or 1 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
60 changes: 60 additions & 0 deletions tests/compat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#[cfg(feature = "serde")]
#[test]
#[allow(non_snake_case)]
fn test_current_FchConsoleOutMode_Disabled() {
let mode: amd_apcb::FchConsoleOutMode =
serde_yaml::from_str("\"Disabled\"")
.expect("configuration be valid JSON");
assert_eq!(mode, amd_apcb::FchConsoleOutMode::Disabled);
}

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

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

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

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

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

0 comments on commit 8c774bd

Please sign in to comment.