From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9]) by lore.proxmox.com (Postfix) with ESMTPS id 828FC1FF16B for ; Thu, 28 Nov 2024 17:07:41 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id BD735A771; Thu, 28 Nov 2024 17:07:40 +0100 (CET) From: Christian Ebner To: pbs-devel@lists.proxmox.com Date: Thu, 28 Nov 2024 17:07:20 +0100 Message-Id: <20241128160721.583578-2-c.ebner@proxmox.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20241128160721.583578-1-c.ebner@proxmox.com> References: <20241128160721.583578-1-c.ebner@proxmox.com> MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.029 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 RCVD_IN_VALIDITY_CERTIFIED_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_RPBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_SAFE_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. 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 2/3] api types: version: implement traits to allow for version comparison 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: , Reply-To: Proxmox Backup Server development discussion Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pbs-devel-bounces@lists.proxmox.com Sender: "pbs-devel" Derive and implement the traits to allow comparison of two `ApiVersion` instances for more direct and easy api version comparisons. Further, add some basic test cases to reduce risk of regressions. This is useful for e.g. feature compatibility checks by comparing api versions of remote instances. Example comparison: ``` api_version >= ApiVersion::new(3, 3, 0) ``` Signed-off-by: Christian Ebner --- changes since version 1: - implement traits for operator based version comparison - add basic regression tests pbs-api-types/src/version.rs | 122 +++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) diff --git a/pbs-api-types/src/version.rs b/pbs-api-types/src/version.rs index bd4c517da..09e725eb6 100644 --- a/pbs-api-types/src/version.rs +++ b/pbs-api-types/src/version.rs @@ -1,4 +1,5 @@ //! Defines the types for the api version info endpoint +use std::cmp::Ordering; use std::convert::TryFrom; use anyhow::{format_err, Context}; @@ -33,6 +34,7 @@ pub type ApiVersionMajor = u64; pub type ApiVersionMinor = u64; pub type ApiVersionRelease = u64; +#[derive(PartialEq, Eq)] pub struct ApiVersion { pub major: ApiVersionMajor, pub minor: ApiVersionMinor, @@ -66,3 +68,123 @@ impl TryFrom for ApiVersion { }) } } + +impl PartialOrd for ApiVersion { + fn partial_cmp(&self, other: &Self) -> Option { + let ordering = match ( + self.major.cmp(&other.major), + self.minor.cmp(&other.minor), + self.release.cmp(&other.release), + ) { + (Ordering::Equal, Ordering::Equal, ordering) => ordering, + (Ordering::Equal, ordering, _) => ordering, + (ordering, _, _) => ordering, + }; + + Some(ordering) + } +} + +impl ApiVersion { + pub fn new(major: ApiVersionMajor, minor: ApiVersionMinor, release: ApiVersionRelease) -> Self { + Self { + major, + minor, + release, + } + } +} + +#[test] +fn same_level_version_comarison() { + let major_base = ApiVersion::new(2, 0, 0); + let major_less = ApiVersion::new(1, 0, 0); + let major_greater = ApiVersion::new(3, 0, 0); + + let minor_base = ApiVersion::new(2, 2, 0); + let minor_less = ApiVersion::new(2, 1, 0); + let minor_greater = ApiVersion::new(2, 3, 0); + + let release_base = ApiVersion::new(2, 2, 2); + let release_less = ApiVersion::new(2, 2, 1); + let release_greater = ApiVersion::new(2, 2, 3); + + assert!(major_base == major_base); + assert!(minor_base == minor_base); + assert!(release_base == release_base); + + assert!(major_base > major_less); + assert!(major_base >= major_less); + assert!(major_base != major_less); + + assert!(major_base < major_greater); + assert!(major_base <= major_greater); + assert!(major_base != major_greater); + + assert!(minor_base > minor_less); + assert!(minor_base >= minor_less); + assert!(minor_base != minor_less); + + assert!(minor_base < minor_greater); + assert!(minor_base <= minor_greater); + assert!(minor_base != minor_greater); + + assert!(release_base > release_less); + assert!(release_base >= release_less); + assert!(release_base != release_less); + + assert!(release_base < release_greater); + assert!(release_base <= release_greater); + assert!(release_base != release_greater); +} + +#[test] +fn mixed_level_version_comarison() { + let major_base = ApiVersion::new(2, 0, 0); + let major_less = ApiVersion::new(1, 0, 0); + let major_greater = ApiVersion::new(3, 0, 0); + + let minor_base = ApiVersion::new(2, 2, 0); + let minor_less = ApiVersion::new(2, 1, 0); + let minor_greater = ApiVersion::new(2, 3, 0); + + let release_base = ApiVersion::new(2, 2, 2); + let release_less = ApiVersion::new(2, 2, 1); + let release_greater = ApiVersion::new(2, 2, 3); + + assert!(major_base < minor_base); + assert!(major_base < minor_less); + assert!(major_base < minor_greater); + + assert!(major_base < release_base); + assert!(major_base < release_less); + assert!(major_base < release_greater); + + assert!(major_less < minor_base); + assert!(major_less < minor_less); + assert!(major_less < minor_greater); + + assert!(major_less < release_base); + assert!(major_less < release_less); + assert!(major_less < release_greater); + + assert!(major_greater > minor_base); + assert!(major_greater > minor_less); + assert!(major_greater > minor_greater); + + assert!(major_greater > release_base); + assert!(major_greater > release_less); + assert!(major_greater > release_greater); + + assert!(minor_base < release_base); + assert!(minor_base < release_less); + assert!(minor_base < release_greater); + + assert!(minor_greater > release_base); + assert!(minor_greater > release_less); + assert!(minor_greater > release_greater); + + assert!(minor_less < release_base); + assert!(minor_less < release_less); + assert!(minor_less < release_greater); +} -- 2.39.5 _______________________________________________ pbs-devel mailing list pbs-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel