pub const ROLE_HIERARCHY: &[&str] =
&["public", "follower", "supporter", "contributor", "moderator", "leader"];
pub fn expand_roles(highest_roles: &[Box<str>]) -> String {
if highest_roles.is_empty() {
return String::new();
}
let mut highest_idx: Option<usize> = None;
for role in highest_roles {
if let Some(idx) = ROLE_HIERARCHY.iter().position(|&r| r == role.as_ref()) {
highest_idx = Some(highest_idx.map_or(idx, |h| h.max(idx)));
}
}
match highest_idx {
Some(idx) => ROLE_HIERARCHY[..=idx].join(","),
None => String::new(),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_expand_roles_empty() {
assert_eq!(expand_roles(&[]), "");
}
#[test]
fn test_expand_roles_single() {
assert_eq!(expand_roles(&["public".into()]), "public");
assert_eq!(expand_roles(&["follower".into()]), "public,follower");
assert_eq!(
expand_roles(&["moderator".into()]),
"public,follower,supporter,contributor,moderator"
);
assert_eq!(
expand_roles(&["leader".into()]),
"public,follower,supporter,contributor,moderator,leader"
);
}
#[test]
fn test_expand_roles_multiple() {
assert_eq!(
expand_roles(&["contributor".into(), "moderator".into()]),
"public,follower,supporter,contributor,moderator"
);
assert_eq!(
expand_roles(&["public".into(), "leader".into()]),
"public,follower,supporter,contributor,moderator,leader"
);
}
#[test]
fn test_expand_roles_unknown() {
assert_eq!(expand_roles(&["unknown".into()]), "");
assert_eq!(
expand_roles(&["unknown".into(), "contributor".into()]),
"public,follower,supporter,contributor"
);
}
}