]> git.proxmox.com Git - proxmox.git/commitdiff
type: move `ProductType` type to `proxmox-subscription` from pom
authorStefan Sterz <s.sterz@proxmox.com>
Thu, 9 Nov 2023 15:33:58 +0000 (16:33 +0100)
committerFabian Grünbichler <f.gruenbichler@proxmox.com>
Mon, 27 Nov 2023 12:31:59 +0000 (13:31 +0100)
previously this type lived inside of pom. this made it harder to
access the product type from a `SubscriptionInfo` trait in other
products. move the type here so we can check product types more
consistently across products (e. g. in pom and pbs)

Signed-off-by: Stefan Sterz <s.sterz@proxmox.com>
proxmox-subscription/src/lib.rs
proxmox-subscription/src/subscription_info.rs

index 6c30a916d8835acd16b9a10152329d962a201822..8a298f233c06b3f4f385d41cd0a809998a4c75f1 100644 (file)
@@ -1,5 +1,7 @@
 mod subscription_info;
-pub use subscription_info::{get_hardware_address, SubscriptionInfo, SubscriptionStatus};
+pub use subscription_info::{
+    get_hardware_address, ProductType, SubscriptionInfo, SubscriptionStatus,
+};
 
 pub mod check;
 pub mod files;
index f45539245797c353068047b733c17430ecf1fad2..dc10a2a8d065fab297dc04f2163f92fa4a4383e1 100644 (file)
@@ -1,4 +1,4 @@
-use std::path::Path;
+use std::{fmt::Display, path::Path, str::FromStr};
 
 use anyhow::{bail, format_err, Error};
 use openssl::hash::{hash, DigestBytes, MessageDigest};
@@ -59,6 +59,48 @@ impl std::fmt::Display for SubscriptionStatus {
     }
 }
 
+#[cfg_attr(feature = "api-types", api())]
+#[cfg_attr(feature = "api-types", derive(Updater))]
+#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
+#[serde(rename_all = "lowercase")]
+/// Product type
+pub enum ProductType {
+    /// Proxmox Virtual Environment
+    Pve,
+    /// Proxmox Backup Server
+    Pbs,
+    /// Proxmox Mail Gateway
+    Pmg,
+    /// Proxmox Offline Mirror
+    Pom,
+}
+
+impl Display for ProductType {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        let txt = match self {
+            ProductType::Pve => "pve",
+            ProductType::Pbs => "pbs",
+            ProductType::Pmg => "pmg",
+            ProductType::Pom => "pom",
+        };
+        f.write_str(txt)
+    }
+}
+
+impl FromStr for ProductType {
+    type Err = Error;
+
+    fn from_str(s: &str) -> Result<Self, Self::Err> {
+        match s {
+            "pve" => Ok(ProductType::Pve),
+            "pmg" => Ok(ProductType::Pmg),
+            "pbs" => Ok(ProductType::Pbs),
+            "pom" => Ok(ProductType::Pom),
+            _ => bail!("unknown product type '{s}'"),
+        }
+    }
+}
+
 #[cfg_attr(feature = "api-types", api(
     properties: {
         status: {
@@ -237,6 +279,13 @@ impl SubscriptionInfo {
             }
         }
     }
+
+    pub fn get_product_type(&self) -> Result<ProductType, Error> {
+        self.key
+            .as_ref()
+            .ok_or_else(|| format_err!("no product key set"))
+            .map(|key| key[..3].parse::<ProductType>())?
+    }
 }
 
 /// Shortcut for md5 sums.