[pbs-devel] [PATCH proxmox-backup 05/12] api-types: implement `Display`, `FromStr` for `RealmType`
Wolfgang Bumiller
w.bumiller at proxmox.com
Fri Aug 11 12:58:21 CEST 2023
On Tue, Aug 08, 2023 at 02:22:07PM +0200, Christoph Heiss wrote:
> 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,
> }
RealmType implements Serialize and Deserialize, but uses lowercase for
its serialized values.
To my knowledge we don't yet have any types where Serialize+Deserialize
vs Display+FromStr differ like this.
If we really want this it should be commented + described in the commit
message. I do wonder though, if there's any harm in just also changing
the deserializer to use the case-insensitive `FromStr` by replacing the
derive with `serde_plain::derive_deserialize_from_fromstr!()`.
>
> +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