all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Dominik Csapak <d.csapak@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox 2/4] promxox-router: add SerializableReturn Trait
Date: Fri,  8 Apr 2022 11:56:01 +0200	[thread overview]
Message-ID: <20220408095606.2767234-3-d.csapak@proxmox.com> (raw)
In-Reply-To: <20220408095606.2767234-1-d.csapak@proxmox.com>

this will be useful as a generic return type for api calls which
must implement Serialize.

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 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 d9f47ea..f042f31 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.1" }
 proxmox-schema = { path = "../proxmox-schema", version = "1.1" }
+proxmox-async = { path = "../proxmox-async", version = "0.4" }
 
 [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<proxmox_async::blocking::SenderWriter>,
+>;
+
+/// 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<
+        <SenderSerializer as serde::Serializer>::Ok,
+        <SenderSerializer as serde::Serializer>::Error,
+    >;
+
+    /// Returns a value again from self
+    fn to_value(&self) -> Result<Value, serde_json::error::Error>;
+}
+
+impl<T> SerializableReturn for T
+where
+    T: serde::Serialize,
+{
+    fn sender_serialize(
+        &self,
+        serializer: SenderSerializer,
+        value: Value,
+    ) -> Result<
+        <SenderSerializer as serde::Serializer>::Ok,
+        <SenderSerializer as serde::Serializer>::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<Value, serde_json::error::Error> {
+        serde_json::to_value(self)
+    }
+}
-- 
2.30.2





  parent reply	other threads:[~2022-04-08  9:56 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-08  9:55 [pbs-devel] [PATCH proxmox/proxmox-backup] implement streaming serialization for api calls Dominik Csapak
2022-04-08  9:56 ` [pbs-devel] [PATCH proxmox 1/4] proxmox-async: add SenderWriter helper Dominik Csapak
2022-04-12 12:29   ` [pbs-devel] applied-series: " Wolfgang Bumiller
2022-04-08  9:56 ` Dominik Csapak [this message]
2022-04-08  9:56 ` [pbs-devel] [PATCH proxmox 3/4] proxmox-router: add new ApiHandler variants for streaming serialization Dominik Csapak
2022-04-08  9:56 ` [pbs-devel] [PATCH proxmox 4/4] proxmox-api-macro: add 'streaming' option Dominik Csapak
2022-04-08  9:56 ` [pbs-devel] [PATCH proxmox-backup 1/3] proxmox-rest-server: OutputFormatter: add new format_data_streaming method Dominik Csapak
2022-04-08  9:56 ` [pbs-devel] [PATCH proxmox-backup 2/3] adapt to the new ApiHandler variants Dominik Csapak
2022-04-08  9:56 ` [pbs-devel] [PATCH proxmox-backup 3/3] api: admin/datastore: enable streaming for some api calls Dominik Csapak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220408095606.2767234-3-d.csapak@proxmox.com \
    --to=d.csapak@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal