Closed Zerowalker closed 1 year ago
Yes, it can be tedious to retrieve the values, but you can't make it less general without losing the correspondence to the LDAP data model. If I understand the semantics of your query properly (use one value of either userPrincipalName
or mail
, preferring userPrincipalName
, and returning an error if neither exists), I'd write it like this:
let mut entry = SearchEntry::construct(entry);
let mail = if let Some(upn) = entry.attrs.remove("userPrincipalName") {
upn[0].to_owned()
} else if let Some(mail) = entry.attrs.remove("mail") {
mail[0].to_owned()
} else {
return Err(OptionError::NoMail {
name: username.to_owned(),
});
};
Ok(mail)
The beauty of Rust is that not everything has to be a functional adapter chain :)
Closing for lack of feedback and being a usage question.
Really sorry for the very late response! Your suggestion was really helpful, you got what i was aiming for and approached it differently and much cleaner in my opinion.
Much appreciated, and sorry for taking your time and then not getting back until now.
Hi!
been trying to make getting some attributes safely a bit less messy. I might be going at it the wrong way, but the problem i have is that attributes have a 2 level
Option
going on. Here's an example of me getting theuserPrincipalName
andmail
while preferring the second:Thanks:)!