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

Christoph Heiss c.heiss at proxmox.com
Mon Aug 14 11:40:53 CEST 2023


Thanks for the review & insights (on this and the other patches too)!

On Fri, Aug 11, 2023 at 12:58:21PM +0200, Wolfgang Bumiller wrote:
>
> 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.
I'll change it so that these two implementations match.

>
> 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!()`.
`serde_plain` does look interesting, didn't know about this crate!

Although I guess it would make more sense to instead just use
`serde_plain::derive_display_from_serialize()` and its `FromStr`
counterpart for the below, seeing as `Serialize` + `Deserialize` are
already implemented anyway (instead of switching things around).

>
> >
> > +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