[pbs-devel] [PATCH proxmox-backup 1/2] api types: version: add helper for min version checks

Thomas Lamprecht t.lamprecht at proxmox.com
Thu Nov 28 14:10:16 CET 2024


Am 28.11.24 um 13:49 schrieb Christian Ebner:
> Add a helper method to the ApiVersion type to reduce possible errors
> when comparing api versions for feature compatibility checks.
> 
> Signed-off-by: Christian Ebner <c.ebner at proxmox.com>
> ---
>  pbs-api-types/src/version.rs | 33 +++++++++++++++++++++++++++++++++
>  1 file changed, 33 insertions(+)
> 
> diff --git a/pbs-api-types/src/version.rs b/pbs-api-types/src/version.rs
> index 80f87e372..5f8efb663 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};
> @@ -68,3 +69,35 @@ impl TryFrom<ApiVersionInfo> for ApiVersion {
>          })
>      }
>  }
> +
> +impl ApiVersion {
> +    pub fn new(
> +        major: ApiVersionMajor,
> +        minor: ApiVersionMinor,
> +        release: ApiVersionRelease,
> +        repoid: String,
> +    ) -> Self {
> +        Self {
> +            major,
> +            minor,
> +            release,
> +            repoid,
> +        }
> +    }
> +
> +    pub fn is_min_required(&self, version: ApiVersion) -> bool {
> +        match (
> +            version.major.cmp(&self.major),
> +            version.minor.cmp(&self.minor),
> +            version.release.cmp(&self.release),
> +        ) {
> +            (Ordering::Less, _, _) => true,
> +            (Ordering::Greater, _, _) => false,
> +            (Ordering::Equal, Ordering::Less, _) => true,
> +            (Ordering::Equal, Ordering::Greater, _) => false,
> +            (Ordering::Equal, Ordering::Equal, Ordering::Less) => true,
> +            (Ordering::Equal, Ordering::Equal, Ordering::Equal) => true,
> +            (Ordering::Equal, Ordering::Equal, Ordering::Greater) => false,
> +        }
> +    }
> +}


Why not impl the Ord trait here instead?

Then the call-site could be

let supports_prune_delete_stats = api_version >= ApiVersion::new(3, 2, 11, String::new());

And maybe a separate type for the triple without the commit hash on which you
also impl the same and then avoid that slightly confusing String::new() hack.




More information about the pbs-devel mailing list