[pbs-devel] [RFC proxmox-backup 08/15] api: add API token endpoints

Wolfgang Bumiller w.bumiller at proxmox.com
Tue Oct 20 12:15:54 CEST 2020


On Tue, Oct 20, 2020 at 11:42:22AM +0200, Wolfgang Bumiller wrote:
> On Mon, Oct 19, 2020 at 09:39:12AM +0200, Fabian Grünbichler wrote:
> > +    let _lock = open_file_locked(user::USER_CFG_LOCKFILE, std::time::Duration::new(10, 0), true)?;
> > +
> > +    let (mut config, expected_digest) = user::config()?;
> > +
> > +    if let Some(ref digest) = digest {
> > +        let digest = proxmox::tools::hex_to_digest(digest)?;
> > +        crate::tools::detect_modified_configuration_file(&digest, &expected_digest)?;
> > +    }
> > +
> > +    let tokenname = Tokenname::try_from(tokenname)?;
> > +    let tokenid = Userid::from((userid.name(), userid.realm(), tokenname.as_ref()));
> > +
> > +    let mut data: user::ApiToken = config.lookup("token", tokenid.as_str())?;
> > +
> > +    if let Some(comment) = comment {
> > +        let comment = comment.trim().to_string();
> > +        if comment.is_empty() {
> > +            data.comment = None;
> > +        } else {
> > +            data.comment = Some(comment);
> > +        }
> > +    }
> > +
> > +    if let Some(enable) = enable {
> > +        data.enable = if enable { None } else { Some(false) };
> 
> Really not a fan of single line if/else like this. Also with the `if
> let` together this isn't actually "fast" to read.
> How about:
> 
>     data.enabled = match enable {
>         Some(true) => None,
>         other => other,
>     }
> 
> or
> 
>     data.enabled = enable.filter(|&b| !b);

I missed that there was no `else` case which should leave it unchanged,
so only the `match` version really makes sense, but with explicit
`None => data.enabled`, maybe include comments:

     data.enabled = match enable {
         Some(true) => None, // enabled is default
         Some(false) => Some(false),
         None => data.enabled, // unchanged.
     }





More information about the pbs-devel mailing list