[pve-devel] [PATCH installer 04/14] common: simplify filesystem type serializing & Display trait impl

Stefan Hanreich s.hanreich at proxmox.com
Thu Jul 11 16:32:07 CEST 2024


On 7/10/24 15:27, Christoph Heiss wrote:
> +impl<'de> Deserialize<'de> for FsType {
> +    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
> +    where
> +        D: serde::Deserializer<'de>,
> +    {
> +        let fs: String = Deserialize::deserialize(deserializer)?;
> +
> +        match fs.as_str() {
> +            "ext4" => Ok(FsType::Ext4),
> +            "xfs" => Ok(FsType::Xfs),
> +            "zfs (RAID0)" => Ok(FsType::Zfs(ZfsRaidLevel::Raid0)),
> +            "zfs (RAID1)" => Ok(FsType::Zfs(ZfsRaidLevel::Raid1)),
> +            "zfs (RAID10)" => Ok(FsType::Zfs(ZfsRaidLevel::Raid10)),
> +            "zfs (RAIDZ-1)" => Ok(FsType::Zfs(ZfsRaidLevel::RaidZ)),
> +            "zfs (RAIDZ-2)" => Ok(FsType::Zfs(ZfsRaidLevel::RaidZ2)),
> +            "zfs (RAIDZ-3)" => Ok(FsType::Zfs(ZfsRaidLevel::RaidZ3)),
> +            "btrfs (RAID0)" => Ok(FsType::Btrfs(BtrfsRaidLevel::Raid0)),
> +            "btrfs (RAID1)" => Ok(FsType::Btrfs(BtrfsRaidLevel::Raid1)),
> +            "btrfs (RAID10)" => Ok(FsType::Btrfs(BtrfsRaidLevel::Raid10)),
> +            _ => Err(serde::de::Error::custom("could not find file system: {fs}")),
> +        }
> +    }
> +}

Maybe we could implement FromStr here and use
serde_plain::derive_deserialize_from_fromstr ?

We could even implement FromStr for BtrfsRaidLevel and ZfsRaidLevel and
then use that here, but it might be a bit overkill for just this....

> +impl Serialize for FsType {
> +    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
> +    where
> +        S: serde::Serializer,
> +    {
> +        let value = match self {
> +            // proxinstall::$fssetup
> +            FsType::Ext4 => "ext4",
> +            FsType::Xfs => "xfs",
> +            // proxinstall::get_zfs_raid_setup()
> +            FsType::Zfs(level) => &format!("zfs ({level})"),
> +            // proxinstall::get_btrfs_raid_setup()
> +            FsType::Btrfs(level) => &format!("btrfs ({level})"),
> +        };
> +
> +        serializer.collect_str(value)
> +    }
> +}

Same as above but with Display and
serde_plain::derive_display_from_serialize




More information about the pve-devel mailing list