[pve-devel] [PATCH installer v4 04/30] common: utils: add deserializer for CidrAddress

Aaron Lauterer a.lauterer at proxmox.com
Thu Apr 4 16:48:36 CEST 2024


so it can be deserialized from a string

Signed-off-by: Aaron Lauterer <a.lauterer at proxmox.com>
---
 proxmox-installer-common/src/utils.rs | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/proxmox-installer-common/src/utils.rs b/proxmox-installer-common/src/utils.rs
index 36b1d53..f6521eb 100644
--- a/proxmox-installer-common/src/utils.rs
+++ b/proxmox-installer-common/src/utils.rs
@@ -103,6 +103,17 @@ impl fmt::Display for CidrAddress {
     }
 }
 
+impl<'de> Deserialize<'de> for CidrAddress {
+    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+    where
+        D: serde::Deserializer<'de>,
+    {
+        let s: String = Deserialize::deserialize(deserializer)?;
+        s.parse()
+            .map_err(|_| serde::de::Error::custom("invalid CIDR"))
+    }
+}
+
 fn mask_limit(addr: &IpAddr) -> usize {
     if addr.is_ipv4() {
         32
-- 
2.39.2





More information about the pve-devel mailing list