public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Christian Ebner <c.ebner@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH v2 proxmox-backup 3/5] api-types: client: add wrapper api type for TimeSpan
Date: Wed, 23 Oct 2024 11:11:01 +0200	[thread overview]
Message-ID: <20241023091103.80792-4-c.ebner@proxmox.com> (raw)
In-Reply-To: <20241023091103.80792-1-c.ebner@proxmox.com>

Implementing the `ApiType` for `TimeSpan` directly does not work
since `proxmox-schema` depends on `proxmox-time`.

Add a wrapper type `TimeInterval` and implement the api type on this
as a workaround.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
changes since version 1:
- Move `LogInterval` to client, it is not an api type anymore
- Implement `TimeInterval` wrapper for `TimeSpan`, to avoid cyclic
  create dependencies for `proxmox-schema` and `proxmox-time` if
  `TimeSpan` would be implemented as api type

 pbs-api-types/src/client.rs | 49 +++++++++++++++++++++++++++++++++++++
 pbs-api-types/src/lib.rs    |  3 +++
 2 files changed, 52 insertions(+)
 create mode 100644 pbs-api-types/src/client.rs

diff --git a/pbs-api-types/src/client.rs b/pbs-api-types/src/client.rs
new file mode 100644
index 000000000..2d2c864dd
--- /dev/null
+++ b/pbs-api-types/src/client.rs
@@ -0,0 +1,49 @@
+use std::str::FromStr;
+
+use anyhow::Error;
+
+use proxmox_schema::{ApiStringFormat, ApiType, Schema, StringSchema};
+use proxmox_time::TimeSpan;
+
+// Wrapper type for `TimeSpan` to avoid cyclic create dependency for `proxmox-time` and `proxmox-schema`
+pub struct TimeInterval(TimeSpan);
+
+impl FromStr for TimeInterval {
+    type Err = Error;
+
+    fn from_str(value: &str) -> Result<Self, Self::Err> {
+        Ok(Self(TimeSpan::from_str(value)?))
+    }
+}
+
+impl From<TimeInterval> for TimeSpan {
+    fn from(time_interval: TimeInterval) -> TimeSpan {
+        time_interval.0
+    }
+}
+
+impl std::fmt::Display for TimeInterval {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        TimeSpan::fmt(&self.0, f)
+    }
+}
+
+impl ApiType for TimeInterval {
+    const API_SCHEMA: Schema = StringSchema::new("Time interval")
+        .format(&ApiStringFormat::VerifyFn(verify_time_interval))
+        .schema();
+}
+
+impl TimeInterval {
+    pub fn as_f64(&self) -> f64 {
+        std::primitive::f64::from(&self.0)
+    }
+}
+
+pub fn verify_time_interval(value: &str) -> Result<(), Error> {
+    let _: TimeSpan = value.parse()?;
+    Ok(())
+}
+
+proxmox_serde::forward_serialize_to_display!(TimeInterval);
+proxmox_serde::forward_deserialize_to_from_str!(TimeInterval);
diff --git a/pbs-api-types/src/lib.rs b/pbs-api-types/src/lib.rs
index 460c7da7c..bd16ab16c 100644
--- a/pbs-api-types/src/lib.rs
+++ b/pbs-api-types/src/lib.rs
@@ -93,6 +93,9 @@ pub const GROUP_OR_SNAPSHOT_PATH_REGEX_STR: &str =
 mod acl;
 pub use acl::*;
 
+mod client;
+pub use client::*;
+
 mod datastore;
 pub use datastore::*;
 
-- 
2.39.5



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


  parent reply	other threads:[~2024-10-23  9:10 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-23  9:10 [pbs-devel] [PATCH v2 proxmox proxmox-backup 0/5] backup client progress log interval Christian Ebner
2024-10-23  9:10 ` [pbs-devel] [PATCH v2 proxmox 1/5] time: fix typos in `TimeSpan` related docstring Christian Ebner
2024-10-23  9:11 ` [pbs-devel] [PATCH v2 proxmox 2/5] time: also implement `From<&TimeSpan> for f64` Christian Ebner
2024-10-23  9:11 ` Christian Ebner [this message]
2024-10-23  9:11 ` [pbs-devel] [PATCH v2 proxmox-backup 4/5] client: progress log: factor out log message generation Christian Ebner
2024-10-23  9:11 ` [pbs-devel] [PATCH v2 proxmox-backup 5/5] client: progress log: allow to specify backup log interval Christian Ebner

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=20241023091103.80792-4-c.ebner@proxmox.com \
    --to=c.ebner@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal