[pbs-devel] [PATCH proxmox-backup 05/12] api-types: implement `Display`, `FromStr` for `RealmType`

Christoph Heiss c.heiss at proxmox.com
Tue Aug 8 14:22:07 CEST 2023


Signed-off-by: Christoph Heiss <c.heiss at proxmox.com>
---
 pbs-api-types/src/lib.rs | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/pbs-api-types/src/lib.rs b/pbs-api-types/src/lib.rs
index 4764c51a..6ebbe514 100644
--- a/pbs-api-types/src/lib.rs
+++ b/pbs-api-types/src/lib.rs
@@ -1,5 +1,9 @@
 //! Basic API types used by most of the PBS code.

+use core::fmt;
+
+use anyhow::{format_err, Error};
+
 use serde::{Deserialize, Serialize};

 use proxmox_auth_api::{APITOKEN_ID_REGEX_STR, USER_ID_REGEX_STR};
@@ -508,6 +512,33 @@ pub enum RealmType {
     Ldap,
 }

+impl fmt::Display for RealmType {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        use RealmType::*;
+        match self {
+            Pam => write!(f, "PAM"),
+            Pbs => write!(f, "PBS"),
+            OpenId => write!(f, "OpenID"),
+            Ldap => write!(f, "LDAP"),
+        }
+    }
+}
+
+impl std::str::FromStr for RealmType {
+    type Err = Error;
+
+    fn from_str(realm_type: &str) -> Result<Self, Error> {
+        use RealmType::*;
+        match realm_type.to_lowercase().as_str() {
+            "pam" => Ok(Pam),
+            "pbs" => Ok(Pbs),
+            "openid" => Ok(OpenId),
+            "ldap" => Ok(Ldap),
+            _ => Err(format_err!("unknown realm type {realm_type}")),
+        }
+    }
+}
+
 #[api(
     properties: {
         realm: {
--
2.41.0






More information about the pbs-devel mailing list