[pve-devel] [PATCH v3 27/30] common: add deserializer for FsType

Aaron Lauterer a.lauterer at proxmox.com
Thu Mar 28 14:50:25 CET 2024


Signed-off-by: Aaron Lauterer <a.lauterer at proxmox.com>
---
 proxmox-installer-common/Cargo.toml     |  1 +
 proxmox-installer-common/src/options.rs | 10 ++++--
 proxmox-installer-common/src/setup.rs   | 43 +++++++++++++++++++++++--
 3 files changed, 48 insertions(+), 6 deletions(-)

diff --git a/proxmox-installer-common/Cargo.toml b/proxmox-installer-common/Cargo.toml
index bde5457..70f828a 100644
--- a/proxmox-installer-common/Cargo.toml
+++ b/proxmox-installer-common/Cargo.toml
@@ -8,5 +8,6 @@ exclude = [ "build", "debian" ]
 homepage = "https://www.proxmox.com"
 
 [dependencies]
+regex = "1.7"
 serde = { version = "1.0", features = ["derive"] }
 serde_json = "1.0"
diff --git a/proxmox-installer-common/src/options.rs b/proxmox-installer-common/src/options.rs
index 1efac66..1e782f9 100644
--- a/proxmox-installer-common/src/options.rs
+++ b/proxmox-installer-common/src/options.rs
@@ -1,6 +1,6 @@
+use serde::Deserialize;
 use std::net::{IpAddr, Ipv4Addr};
 use std::{cmp, fmt};
-use serde::Deserialize;
 
 use crate::setup::{
     LocaleInfo, NetworkInfo, ProductConfig, ProxmoxProduct, RuntimeInfo, SetupInfo,
@@ -32,8 +32,11 @@ pub enum ZfsRaidLevel {
     Raid0,
     Raid1,
     Raid10,
+    #[serde(rename = "raidz-1")]
     RaidZ,
+    #[serde(rename = "raidz-2")]
     RaidZ2,
+    #[serde(rename = "raidz-3")]
     RaidZ3,
 }
 
@@ -51,7 +54,8 @@ impl fmt::Display for ZfsRaidLevel {
     }
 }
 
-#[derive(Copy, Clone, Debug, Eq, PartialEq)]
+#[derive(Copy, Clone, Debug, Deserialize, Eq, PartialEq)]
+#[serde(rename_all = "lowercase")]
 pub enum FsType {
     Ext4,
     Xfs,
@@ -226,7 +230,7 @@ pub enum AdvancedBootdiskOptions {
     Btrfs(BtrfsBootdiskOptions),
 }
 
-#[derive(Clone, Debug, PartialEq)]
+#[derive(Clone, Debug, Deserialize, PartialEq)]
 pub struct Disk {
     pub index: String,
     pub path: String,
diff --git a/proxmox-installer-common/src/setup.rs b/proxmox-installer-common/src/setup.rs
index 8432a2c..743e7a9 100644
--- a/proxmox-installer-common/src/setup.rs
+++ b/proxmox-installer-common/src/setup.rs
@@ -8,6 +8,7 @@ use std::{
     path::{Path, PathBuf},
     process::{self, Command, Stdio},
 };
+use regex::Regex;
 
 use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
 
@@ -145,7 +146,7 @@ pub fn installer_setup(in_test_mode: bool) -> Result<(SetupInfo, LocaleInfo, Run
     }
 }
 
-#[derive(Serialize)]
+#[derive(Debug, Deserialize, Serialize)]
 pub struct InstallZfsOption {
     pub ashift: usize,
     #[serde(serialize_with = "serialize_as_display")]
@@ -392,11 +393,11 @@ pub fn spawn_low_level_installer(test_mode: bool) -> io::Result<process::Child>
 }
 
 /// See Proxmox::Install::Config
-#[derive(Serialize)]
+#[derive(Debug, Deserialize, Serialize)]
 pub struct InstallConfig {
     pub autoreboot: usize,
 
-    #[serde(serialize_with = "serialize_fstype")]
+    #[serde(serialize_with = "serialize_fstype", deserialize_with = "deserialize_fs_type")]
     pub filesys: FsType,
     pub hdsize: f64,
     #[serde(skip_serializing_if = "Option::is_none")]
@@ -471,3 +472,39 @@ where
 
     serializer.collect_str(value)
 }
+
+pub fn deserialize_fs_type<'de, D>(deserializer: D) -> Result<FsType, D::Error>
+where
+    D: Deserializer<'de>,
+{
+    let de_fs: String = Deserialize::deserialize(deserializer)?;
+
+    let fs;
+    let mut raid: Option<String> = None;
+    let re_raid = Regex::new(r"^(?P<fs>.*) \((?P<raid>.*)\)$").map_err(de::Error::custom)?;
+    match re_raid.captures(de_fs.as_str()) {
+        Some(caps) => {
+            fs = caps.name("fs").unwrap().as_str().to_lowercase();
+            raid = Some(caps.name("raid").unwrap().as_str().to_lowercase());
+        },
+        None => fs = de_fs,
+    }
+
+    match fs {
+        t if t == "zfs" => {
+            let raidlevel: ZfsRaidLevel = Deserialize::deserialize(serde_json::Value::String(raid.unwrap()))
+                .map_err(de::Error::custom)?;
+            Ok(FsType::Zfs(raidlevel))
+        }
+        t if t == "btrfs" => {
+            let raidlevel: BtrfsRaidLevel = Deserialize::deserialize(serde_json::Value::String(raid.unwrap()))
+                .map_err(de::Error::custom)?;
+            Ok(FsType::Btrfs(raidlevel))
+        }
+        t => {
+            let fstype: FsType = Deserialize::deserialize(serde_json::Value::String(t))
+                .map_err(de::Error::custom)?;
+            Ok(fstype)
+        }
+    }
+}
-- 
2.39.2





More information about the pve-devel mailing list