[pbs-devel] [PATCH proxmox 2/4] promxox-router: add SerializableReturn Trait
Dominik Csapak
d.csapak at proxmox.com
Fri Apr 8 11:56:01 CEST 2022
this will be useful as a generic return type for api calls which
must implement Serialize.
Signed-off-by: Dominik Csapak <d.csapak at 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
More information about the pbs-devel
mailing list