public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Wolfgang Bumiller <w.bumiller@proxmox.com>
To: Lukas Wagner <l.wagner@proxmox.com>
Cc: pbs-devel@lists.proxmox.com
Subject: Re: [pbs-devel] [PATCH proxmox-ldap 3/6] add helpers for constructing LDAP filters
Date: Wed, 18 Jan 2023 13:21:30 +0100	[thread overview]
Message-ID: <20230118122130.d4gl3ytyv3muacrs@casey.proxmox.com> (raw)
In-Reply-To: <20230117142037.847150-4-l.wagner@proxmox.com>

On Tue, Jan 17, 2023 at 03:20:34PM +0100, Lukas Wagner wrote:
> Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
> ---
>  src/lib.rs | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 70 insertions(+)
> 
> diff --git a/src/lib.rs b/src/lib.rs
> index 48eb863..40c4f6d 100644
> --- a/src/lib.rs
> +++ b/src/lib.rs
> @@ -226,3 +226,73 @@ impl LdapConnection {
>          bail!("user not found")
>      }
>  }
> +
> +#[allow(dead_code)]
> +enum FilterElement {

Your only user of this is short-lived and can easily just borrow the
strings. I think we should give this a lifetime and use a str ref
instead of strings.

> +    And(Vec<FilterElement>),
> +    Or(Vec<FilterElement>),
> +    Condition(String, String),
> +    Not(Box<FilterElement>),
> +    Verbatim(String),
> +}
> +
> +impl ToString for FilterElement {

Is there a particular reason to implement `ToString` instead of
`Display`?
If not, a `Display` implementation would need much fewer temporary
allocated strings as it would just keep appending to the same
`fmt::Formatter`.

> +    fn to_string(&self) -> String {
> +        fn children_to_string(children: &[FilterElement]) -> String {
> +            children.iter().fold(String::new(), |mut acc, v| {
> +                acc.push_str(&v.to_string());
> +                acc
> +            })
> +        }
> +
> +        match self {
> +            FilterElement::And(children) => {
> +                format!("(&{})", children_to_string(children))
> +            }
> +            FilterElement::Or(children) => {
> +                format!("(|{})", children_to_string(children))
> +            }
> +            FilterElement::Not(element) => {
> +                format!("(!{})", element.to_string())
> +            }
> +            FilterElement::Condition(attr, value) => {
> +                format!("({attr}={value})")
> +            }
> +            FilterElement::Verbatim(verbatim) => verbatim.clone(),
> +        }
> +    }
> +}
> +
> +#[cfg(test)]
> +mod tests {
> +    use super::FilterElement::*;
> +
> +    #[test]
> +    fn test_filter_elements_to_string() {
> +        assert_eq!(
> +            "(uid=john)",
> +            Condition("uid".into(), "john".into()).to_string()
> +        );
> +        assert_eq!(
> +            "(!(uid=john))",
> +            Not(Box::new(Condition("uid".into(), "john".into()))).to_string()
> +        );
> +
> +        assert_eq!("(foo=bar)", Verbatim("(foo=bar)".into()).to_string());
> +
> +        let filter_string = And(vec![
> +            Condition("givenname".into(), "john".into()),
> +            Condition("sn".into(), "doe".into()),
> +            Or(vec![
> +                Condition("email".into(), "john@foo".into()),
> +                Condition("email".into(), "john@bar".into()),
> +            ]),
> +        ])
> +        .to_string();
> +
> +        assert_eq!(
> +            "(&(givenname=john)(sn=doe)(|(email=john@foo)(email=john@bar)))".to_owned(),

^ The `.to_owned()` is not needed there.

> +            filter_string
> +        );
> +    }
> +}
> -- 
> 2.30.2




  reply	other threads:[~2023-01-18 12:22 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-17 14:20 [pbs-devel] [PATCH proxmox-ldap 0/6] introduce proxmox-ldap crate Lukas Wagner
2023-01-17 14:20 ` [pbs-devel] [PATCH proxmox-ldap 1/6] initial commit Lukas Wagner
2023-01-17 14:20 ` [pbs-devel] [PATCH proxmox-ldap 2/6] add basic user auth functionality Lukas Wagner
2023-01-17 14:20 ` [pbs-devel] [PATCH proxmox-ldap 3/6] add helpers for constructing LDAP filters Lukas Wagner
2023-01-18 12:21   ` Wolfgang Bumiller [this message]
2023-01-17 14:20 ` [pbs-devel] [PATCH proxmox-ldap 4/6] allow searching for LDAP entities Lukas Wagner
2023-01-17 14:20 ` [pbs-devel] [PATCH proxmox-ldap 5/6] tests: add LDAP integration tests Lukas Wagner
2023-01-17 14:20 ` [pbs-devel] [PATCH proxmox-ldap 6/6] add debian packaging Lukas Wagner
2023-01-18 12:30 ` [pbs-devel] [PATCH proxmox-ldap 0/6] introduce proxmox-ldap crate Wolfgang Bumiller
2023-01-23 11:27 ` Thomas Lamprecht
2023-01-23 14:50   ` Lukas Wagner
2023-01-24  7:04     ` Thomas Lamprecht

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230118122130.d4gl3ytyv3muacrs@casey.proxmox.com \
    --to=w.bumiller@proxmox.com \
    --cc=l.wagner@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal