[pbs-devel] [PATCH proxmox 2/6] apt-api-types: use serde-plain to display/parse enums

Wolfgang Bumiller w.bumiller at proxmox.com
Tue Jul 9 08:18:28 CEST 2024


From: Dietmar Maurer <dietmar at proxmox.com>

Signed-off-by: Dietmar Maurer <dietmar at proxmox.com>
---
 proxmox-apt-api-types/Cargo.toml     |  2 +-
 proxmox-apt-api-types/debian/control |  8 ++---
 proxmox-apt-api-types/src/lib.rs     | 48 ++++------------------------
 3 files changed, 12 insertions(+), 46 deletions(-)

diff --git a/proxmox-apt-api-types/Cargo.toml b/proxmox-apt-api-types/Cargo.toml
index e2ab46ad..e6482146 100644
--- a/proxmox-apt-api-types/Cargo.toml
+++ b/proxmox-apt-api-types/Cargo.toml
@@ -9,7 +9,7 @@ exclude.workspace = true
 description = "APT API type definitions."
 
 [dependencies]
-anyhow.workspace = true
 serde = { workspace = true, features = ["derive"] }
+serde_plain.workspace = true
 proxmox-schema = { workspace = true, features = ["api-macro"] }
 proxmox-config-digest.workspace = true
diff --git a/proxmox-apt-api-types/debian/control b/proxmox-apt-api-types/debian/control
index 708e8c4b..5b50e114 100644
--- a/proxmox-apt-api-types/debian/control
+++ b/proxmox-apt-api-types/debian/control
@@ -6,12 +6,12 @@ Build-Depends: debhelper (>= 12),
  cargo:native <!nocheck>,
  rustc:native <!nocheck>,
  libstd-rust-dev <!nocheck>,
- librust-anyhow-1+default-dev <!nocheck>,
  librust-proxmox-config-digest-0.1+default-dev <!nocheck>,
  librust-proxmox-schema-3+api-macro-dev (>= 3.1.1-~~) <!nocheck>,
  librust-proxmox-schema-3+default-dev (>= 3.1.1-~~) <!nocheck>,
  librust-serde-1+default-dev <!nocheck>,
- librust-serde-1+derive-dev <!nocheck>
+ librust-serde-1+derive-dev <!nocheck>,
+ librust-serde-plain-1+default-dev <!nocheck>
 Maintainer: Proxmox Support Team <support at proxmox.com>
 Standards-Version: 4.6.2
 Vcs-Git: git://git.proxmox.com/git/proxmox-apt.git
@@ -24,12 +24,12 @@ Architecture: any
 Multi-Arch: same
 Depends:
  ${misc:Depends},
- librust-anyhow-1+default-dev,
  librust-proxmox-config-digest-0.1+default-dev,
  librust-proxmox-schema-3+api-macro-dev (>= 3.1.1-~~),
  librust-proxmox-schema-3+default-dev (>= 3.1.1-~~),
  librust-serde-1+default-dev,
- librust-serde-1+derive-dev
+ librust-serde-1+derive-dev,
+ librust-serde-plain-1+default-dev
 Provides:
  librust-proxmox-apt-api-types+default-dev (= ${binary:Version}),
  librust-proxmox-apt-api-types-1-dev (= ${binary:Version}),
diff --git a/proxmox-apt-api-types/src/lib.rs b/proxmox-apt-api-types/src/lib.rs
index 3b6ac9e4..80d5ec4b 100644
--- a/proxmox-apt-api-types/src/lib.rs
+++ b/proxmox-apt-api-types/src/lib.rs
@@ -1,6 +1,5 @@
 use std::fmt::Display;
 
-use anyhow::{bail, Error};
 use serde::{Deserialize, Serialize};
 
 use proxmox_config_digest::ConfigDigest;
@@ -16,26 +15,8 @@ pub enum APTRepositoryFileType {
     Sources,
 }
 
-impl TryFrom<&str> for APTRepositoryFileType {
-    type Error = Error;
-
-    fn try_from(file_type: &str) -> Result<Self, Error> {
-        match file_type {
-            "list" => Ok(APTRepositoryFileType::List),
-            "sources" => Ok(APTRepositoryFileType::Sources),
-            _ => bail!("invalid file type '{file_type}'"),
-        }
-    }
-}
-
-impl Display for APTRepositoryFileType {
-    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        match self {
-            APTRepositoryFileType::List => write!(f, "list"),
-            APTRepositoryFileType::Sources => write!(f, "sources"),
-        }
-    }
-}
+serde_plain::derive_display_from_serialize!(APTRepositoryFileType);
+serde_plain::derive_fromstr_from_deserialize!(APTRepositoryFileType);
 
 #[api]
 #[derive(Debug, Copy, Clone, Serialize, Deserialize, PartialEq, Eq)]
@@ -47,26 +28,8 @@ pub enum APTRepositoryPackageType {
     DebSrc,
 }
 
-impl TryFrom<&str> for APTRepositoryPackageType {
-    type Error = Error;
-
-    fn try_from(package_type: &str) -> Result<Self, Error> {
-        match package_type {
-            "deb" => Ok(APTRepositoryPackageType::Deb),
-            "deb-src" => Ok(APTRepositoryPackageType::DebSrc),
-            _ => bail!("invalid package type '{package_type}'"),
-        }
-    }
-}
-
-impl Display for APTRepositoryPackageType {
-    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        match self {
-            APTRepositoryPackageType::Deb => write!(f, "deb"),
-            APTRepositoryPackageType::DebSrc => write!(f, "deb-src"),
-        }
-    }
-}
+serde_plain::derive_display_from_serialize!(APTRepositoryPackageType);
+serde_plain::derive_fromstr_from_deserialize!(APTRepositoryPackageType);
 
 #[api(
     properties: {
@@ -327,6 +290,9 @@ pub enum APTRepositoryHandle {
     CephReefTest,
 }
 
+serde_plain::derive_display_from_serialize!(APTRepositoryHandle);
+serde_plain::derive_fromstr_from_deserialize!(APTRepositoryHandle);
+
 #[api()]
 #[derive(Debug, Clone, Serialize, Deserialize)]
 #[serde(rename_all = "PascalCase")]
-- 
2.39.2





More information about the pbs-devel mailing list