I have an LdapConn that I am passing around to multiple functions. Currently I'm passing the ldap variable to a function and then returning it. Inside of the function I'm not doing any dangerous modification of the ldapConn, I'm just changing the search result part. Passing it around works, but what's the best way to make a variable last the length of my program?
//main.rs
let mut ldap: LdapConn = LdapConn::with_settings(
LdapConnSettings::new()
.set_no_tls_verify(true)
.set_starttls(true),
"ldaps://ldap.example.com:636",
)
.unwrap();
//other_file.rs
pub fn get_group_members(group: &str, mut conn: LdapConn) -> (LdapConn, Vec<String>) {
let (s_filter, ou) = split_dn(group);
let search_result = conn
.search(
&ou,
Scope::Subtree,
&format!("(&(objectClass=group)({}))", s_filter),
vec!["member"],
)
.unwrap();
let resp: Vec<
std::collections::HashMap<std::string::String, std::vec::Vec<std::string::String>>,
> = search_result
.0
.iter()
.map(|x| SearchEntry::construct(x.clone()).attrs)
.collect();
(conn, trim_users(resp[0].get("member").unwrap().to_vec()))
}
//main.rs
let (ldap, users) = get_group_members(group, ldap);
PS: LdapConn is not cloneable
https://docs.rs/ldap3/latest/ldap3/struct.LdapConn.html
The API is virtually identical to the asynchronous one. The chief difference is that LdapConn is not cloneable: if you need another handle, you must open a new connection.