From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <pbs-devel-bounces@lists.proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
	by lore.proxmox.com (Postfix) with ESMTPS id 8825B1FF164
	for <inbox@lore.proxmox.com>; Wed, 23 Oct 2024 11:10:50 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id 9CE191A71A;
	Wed, 23 Oct 2024 11:11:28 +0200 (CEST)
From: Christian Ebner <c.ebner@proxmox.com>
To: pbs-devel@lists.proxmox.com
Date: Wed, 23 Oct 2024 11:11:01 +0200
Message-Id: <20241023091103.80792-4-c.ebner@proxmox.com>
X-Mailer: git-send-email 2.39.5
In-Reply-To: <20241023091103.80792-1-c.ebner@proxmox.com>
References: <20241023091103.80792-1-c.ebner@proxmox.com>
MIME-Version: 1.0
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.028 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 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
Subject: [pbs-devel] [PATCH v2 proxmox-backup 3/5] api-types: client: add
 wrapper api type for TimeSpan
X-BeenThere: pbs-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox Backup Server development discussion
 <pbs-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pbs-devel/>
List-Post: <mailto:pbs-devel@lists.proxmox.com>
List-Help: <mailto:pbs-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=subscribe>
Reply-To: Proxmox Backup Server development discussion
 <pbs-devel@lists.proxmox.com>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: pbs-devel-bounces@lists.proxmox.com
Sender: "pbs-devel" <pbs-devel-bounces@lists.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