Skip to content

Commit

Permalink
fix(backend/repartcfg): Implement deserialization for ini_enum! macro
Browse files Browse the repository at this point in the history
  • Loading branch information
korewaChino committed Feb 8, 2025
1 parent debdc32 commit 37758fe
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/backend/repartcfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,8 @@ impl<'de> serde::Deserialize<'de> for PartTypeIdent {
}

ini_enum! {
#[derive(Debug, serde::Deserialize)]
#[serde(rename_all = "lowercase")]
#[derive(Debug)]
// #[serde(rename_all = "lowercase")]
pub enum FileSystem {
Ext4,
Btrfs,
Expand All @@ -328,8 +328,8 @@ ini_enum! {
Swap,
}

#[derive(Debug, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
#[derive(Debug, Default)]
// #[serde(rename_all = "lowercase")]
pub enum EncryptOption {
#[default]
Off,
Expand All @@ -338,8 +338,8 @@ ini_enum! {
KeyFileTpm2 => "key-file+tpm2",
}

#[derive(Debug, Default, Deserialize)]
#[serde(rename_all = "lowercase")]
#[derive(Debug, Default)]
// #[serde(rename_all = "lowercase")]
pub enum Verity {
#[default]
Off,
Expand Down
1 change: 1 addition & 0 deletions src/backend/test/root.conf
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ FactoryReset=yes
# This path is actually relative; see man repart.d at --copy-source option
CopyFiles=/:/
ExcludeFiles=/boot/
Encrypt=key-file
19 changes: 19 additions & 0 deletions src/util/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,25 @@ macro_rules! ini_enum {
)*})
}
}
impl<'de> serde::Deserialize<'de> for $name {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
match s.as_str() {
$(
s if s == ini_enum!(@match $field $(=> $s)?) => Ok(Self::$field),
)*
other => {
let variants: &[&str] = &[$(
stringify!($field),
)*];
Err(serde::de::Error::unknown_variant(other, variants))
}
}
}
}
};
(
$(#[$outmeta1:meta])*
Expand Down

0 comments on commit 37758fe

Please sign in to comment.