[pbs-devel] [PATCH backup] tree-wide: replace serde_json::from_value(a_value.clone())

Wolfgang Bumiller w.bumiller at proxmox.com
Tue Apr 12 12:39:50 CEST 2022


`&Value` itself implements `Deserializer` and can therefore
be passed directly to `T::deserialize` without requiring an
intermediate `clone()`. (This also enables optionally
borrowing strings if the result has a short enough lifetime)

Signed-off-by: Wolfgang Bumiller <w.bumiller at proxmox.com>
---
 pbs-client/Cargo.toml              | 1 +
 pbs-client/src/tools/key_source.rs | 2 +-
 pbs-datastore/src/manifest.rs      | 4 ++--
 proxmox-file-restore/src/main.rs   | 4 ++--
 src/acme/plugin.rs                 | 2 +-
 src/api2/config/acme.rs            | 2 +-
 src/api2/node/network.rs           | 4 ++--
 src/bin/proxmox_tape/media.rs      | 3 ++-
 8 files changed, 12 insertions(+), 10 deletions(-)

diff --git a/pbs-client/Cargo.toml b/pbs-client/Cargo.toml
index d713a3ca..767285d4 100644
--- a/pbs-client/Cargo.toml
+++ b/pbs-client/Cargo.toml
@@ -22,6 +22,7 @@ percent-encoding = "2.1"
 pin-project-lite = "0.2"
 regex = "1.5"
 rustyline = "7"
+serde = "1.0"
 serde_json = "1.0"
 tokio = { version = "1.6", features = [ "fs", "signal" ] }
 tokio-stream = "0.1.0"
diff --git a/pbs-client/src/tools/key_source.rs b/pbs-client/src/tools/key_source.rs
index f68b1e41..53a9ab96 100644
--- a/pbs-client/src/tools/key_source.rs
+++ b/pbs-client/src/tools/key_source.rs
@@ -131,7 +131,7 @@ fn do_crypto_parameters(param: &Value, keep_keyfd_open: bool) -> Result<CryptoPa
     };
 
     let mode: Option<CryptMode> = match param.get("crypt-mode") {
-        Some(mode) => Some(serde_json::from_value(mode.clone())?),
+        Some(mode) => Some(serde::Deserialize::deserialize(mode)?),
         None => None,
     };
 
diff --git a/pbs-datastore/src/manifest.rs b/pbs-datastore/src/manifest.rs
index 42b4bf29..d55ce792 100644
--- a/pbs-datastore/src/manifest.rs
+++ b/pbs-datastore/src/manifest.rs
@@ -178,7 +178,7 @@ impl BackupManifest {
     pub fn fingerprint(&self) -> Result<Option<Fingerprint>, Error> {
         match &self.unprotected["key-fingerprint"] {
             Value::Null => Ok(None),
-            value => Ok(Some(serde_json::from_value(value.clone())?))
+            value => Ok(Some(Deserialize::deserialize(value)?))
         }
     }
 
@@ -220,7 +220,7 @@ impl BackupManifest {
 
                 let fingerprint = &json["unprotected"]["key-fingerprint"];
                 if fingerprint != &Value::Null {
-                    let fingerprint = serde_json::from_value(fingerprint.clone())?;
+                    let fingerprint = Fingerprint::deserialize(fingerprint)?;
                     let config_fp = Fingerprint::new(crypt_config.fingerprint());
                     if config_fp != fingerprint {
                         bail!(
diff --git a/proxmox-file-restore/src/main.rs b/proxmox-file-restore/src/main.rs
index 8876ecd0..b6b0a13e 100644
--- a/proxmox-file-restore/src/main.rs
+++ b/proxmox-file-restore/src/main.rs
@@ -230,7 +230,7 @@ async fn list(snapshot: String, path: String, base64: bool, param: Value) -> Res
                 keyfile,
             };
             let driver: Option<BlockDriverType> = match param.get("driver") {
-                Some(drv) => Some(serde_json::from_value(drv.clone())?),
+                Some(drv) => Some(serde::Deserialize::deserialize(drv)?),
                 None => None,
             };
             data_list(driver, details, file, path).await
@@ -382,7 +382,7 @@ async fn extract(
                 keyfile,
             };
             let driver: Option<BlockDriverType> = match param.get("driver") {
-                Some(drv) => Some(serde_json::from_value(drv.clone())?),
+                Some(drv) => Some(serde::Deserialize::deserialize(drv)?),
                 None => None,
             };
 
diff --git a/src/acme/plugin.rs b/src/acme/plugin.rs
index a993c8d7..335e6709 100644
--- a/src/acme/plugin.rs
+++ b/src/acme/plugin.rs
@@ -30,7 +30,7 @@ pub(crate) fn get_acme_plugin(
 
     Ok(Some(match ty.as_str() {
         "dns" => {
-            let plugin: DnsPlugin = serde_json::from_value(data.clone())?;
+            let plugin: DnsPlugin = serde::Deserialize::deserialize(data)?;
             Box::new(plugin)
         }
         "standalone" => {
diff --git a/src/api2/config/acme.rs b/src/api2/config/acme.rs
index 5e3b4b3f..9b586e00 100644
--- a/src/api2/config/acme.rs
+++ b/src/api2/config/acme.rs
@@ -706,7 +706,7 @@ pub fn update_plugin(
                 bail!("cannot update plugin of type {:?}", ty);
             }
 
-            let mut plugin: DnsPlugin = serde_json::from_value(entry.clone())?;
+            let mut plugin = DnsPlugin::deserialize(&*entry)?;
 
             if let Some(delete) = delete {
                 for delete_prop in delete {
diff --git a/src/api2/node/network.rs b/src/api2/node/network.rs
index 77821fde..4cb0d7b9 100644
--- a/src/api2/node/network.rs
+++ b/src/api2/node/network.rs
@@ -1,6 +1,6 @@
 use anyhow::{Error, bail};
+use serde::{Deserialize, Serialize};
 use serde_json::{Value, to_value};
-use ::serde::{Deserialize, Serialize};
 use hex::FromHex;
 
 use proxmox_router::{ApiMethod, Router, RpcEnvironment, Permission};
@@ -539,7 +539,7 @@ pub fn update_interface(
     let interface = config.lookup_mut(&iface)?;
 
     if let Some(interface_type) = param.get("type") {
-        let interface_type: NetworkInterfaceType = serde_json::from_value(interface_type.clone())?;
+        let interface_type = NetworkInterfaceType::deserialize(interface_type)?;
         if  interface_type != interface.interface_type {
             bail!("got unexpected interface type ({:?} != {:?})", interface_type, interface.interface_type);
         }
diff --git a/src/bin/proxmox_tape/media.rs b/src/bin/proxmox_tape/media.rs
index b73c4b0b..7c0db26d 100644
--- a/src/bin/proxmox_tape/media.rs
+++ b/src/bin/proxmox_tape/media.rs
@@ -1,4 +1,5 @@
 use anyhow::Error;
+use serde::Deserialize;
 use serde_json::Value;
 
 use proxmox_router::{cli::*, ApiHandler, RpcEnvironment};
@@ -77,7 +78,7 @@ async fn list_media(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<(),
     };
 
     fn render_status(_value: &Value, record: &Value) -> Result<String, Error> {
-        let record: MediaListEntry = serde_json::from_value(record.clone())?;
+        let record = MediaListEntry::deserialize(record)?;
         Ok(match record.status {
             MediaStatus::Damaged | MediaStatus::Retired => serde_json::to_value(&record.status)?
                 .as_str()
-- 
2.30.2






More information about the pbs-devel mailing list