From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id DC4D560B3D for ; Thu, 17 Feb 2022 10:41:14 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id D6E601D5AA for ; Thu, 17 Feb 2022 10:40:44 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id C21241D570 for ; Thu, 17 Feb 2022 10:40:42 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 9444746DDF for ; Thu, 17 Feb 2022 10:40:42 +0100 (CET) From: Dominik Csapak To: pbs-devel@lists.proxmox.com Date: Thu, 17 Feb 2022 10:40:36 +0100 Message-Id: <20220217094041.1632033-3-d.csapak@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220217094041.1632033-1-d.csapak@proxmox.com> References: <20220217094041.1632033-1-d.csapak@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.158 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record T_SCC_BODY_TEXT_LINE -0.01 - URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [lib.rs] Subject: [pbs-devel] [RFC PATCH proxmox 2/4] promxox-router: add SerializableReturn Trait X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 17 Feb 2022 09:41:14 -0000 this will be useful as a generic return type for api calls which must implement Serialize. Signed-off-by: Dominik Csapak --- proxmox-router/Cargo.toml | 2 + proxmox-router/src/lib.rs | 2 + proxmox-router/src/serializable_return.rs | 62 +++++++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 proxmox-router/src/serializable_return.rs diff --git a/proxmox-router/Cargo.toml b/proxmox-router/Cargo.toml index 28616d8..d460772 100644 --- a/proxmox-router/Cargo.toml +++ b/proxmox-router/Cargo.toml @@ -15,6 +15,7 @@ hyper = { version = "0.14", features = [ "full" ] } nix = "0.19.1" percent-encoding = "2.1" serde_json = "1.0" +serde = "1.0" unicode-width ="0.1.8" # cli: @@ -24,6 +25,7 @@ libc = { version = "0.2", optional = true } proxmox-lang = { path = "../proxmox-lang", version = "1.0" } proxmox-schema = { path = "../proxmox-schema", version = "1.1" } +proxmox-async = { path = "../proxmox-async", version = "0.3" } [features] default = [ "cli" ] diff --git a/proxmox-router/src/lib.rs b/proxmox-router/src/lib.rs index dadb917..84f39f8 100644 --- a/proxmox-router/src/lib.rs +++ b/proxmox-router/src/lib.rs @@ -12,6 +12,7 @@ pub mod error; mod permission; mod router; mod rpc_environment; +mod serializable_return; #[doc(inline)] pub use error::HttpError; @@ -19,6 +20,7 @@ pub use error::HttpError; pub use permission::*; pub use router::*; pub use rpc_environment::{RpcEnvironment, RpcEnvironmentType}; +pub use serializable_return::SerializableReturn; // make list_subdirs_api_method! work without an explicit proxmox-schema dependency: #[doc(hidden)] diff --git a/proxmox-router/src/serializable_return.rs b/proxmox-router/src/serializable_return.rs new file mode 100644 index 0000000..51f6c3c --- /dev/null +++ b/proxmox-router/src/serializable_return.rs @@ -0,0 +1,62 @@ +use serde::Serializer; +use serde_json::Value; + +/// This defines a *fixed* serializer (iow. also where/how to write out the data). +/// +/// (Note that `serde::Serializer` is implemented for `__&mut__ serde_json::Serializer`. +type SenderSerializer<'a> = &'a mut serde_json::Serializer< + &'a mut std::io::BufWriter, +>; + +/// This is an object-safe trait which requires the ability to serialize into particular +/// Serializer instances. +pub trait SerializableReturn { + /// Serializes self into a [`proxmox_async::blocking::SenderWriter`] wrapped + /// into a [`std::io::BufWriter`] + /// + /// If `value` is an Object/Map, serializes that first and puts the value of + /// `self` into the `data` property. + fn sender_serialize( + &self, + serializer: SenderSerializer, + value: Value, + ) -> Result< + ::Ok, + ::Error, + >; + + /// Returns a value again from self + fn to_value(&self) -> Result; +} + +impl SerializableReturn for T +where + T: serde::Serialize, +{ + fn sender_serialize( + &self, + serializer: SenderSerializer, + value: Value, + ) -> Result< + ::Ok, + ::Error, + > { + use serde::ser::SerializeMap; + if let Some(original) = value.as_object() { + let mut map = serializer.serialize_map(None)?; + for (k, v) in original { + map.serialize_entry(k, v)?; + } + + map.serialize_key("data")?; + map.serialize_value(&self)?; + map.end() + } else { + self.serialize(serializer) + } + } + + fn to_value(&self) -> Result { + serde_json::to_value(self) + } +} -- 2.30.2