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

ondisk: Add compat deserializer for FchConsoleOutMode. #133

Merged
merged 1 commit into from
May 6, 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
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(_) => {}
};
}
Loading