diff --git a/src/permissions.rs b/src/permissions.rs index d7d1b912..442b5fa5 100644 --- a/src/permissions.rs +++ b/src/permissions.rs @@ -22,25 +22,38 @@ impl fmt::Display for PermissionType { } } -pub struct TeamDirectory { - usernames: HashSet, - teams: HashSet, -} +// Separate module to ensure that the struct cannot be constructed without invoking `new` +mod directory { + use std::collections::HashSet; -impl TeamDirectory { - pub fn new(usernames: HashSet, teams: HashSet) -> Self { - Self { usernames, teams } + pub(super) struct TeamDirectory { + usernames: HashSet, + teams: HashSet, } - fn user_exists(&self, username: &str) -> bool { - self.usernames.contains(&username.to_lowercase()) - } + impl TeamDirectory { + pub(super) fn new(usernames: HashSet, teams: HashSet) -> Self { + let usernames: HashSet = usernames + .into_iter() + .map(|name| name.to_lowercase()) + .collect(); + let teams: HashSet = + teams.into_iter().map(|name| name.to_lowercase()).collect(); + Self { usernames, teams } + } - fn team_exists(&self, team: &str) -> bool { - self.teams.contains(&team.to_lowercase()) + pub(super) fn user_exists(&self, username: &str) -> bool { + self.usernames.contains(&username.to_lowercase()) + } + + pub(super) fn team_exists(&self, team: &str) -> bool { + self.teams.contains(&team.to_lowercase()) + } } } +use directory::TeamDirectory; + pub struct UserPermissions { review_users: HashSet, try_users: HashSet, @@ -48,32 +61,6 @@ pub struct UserPermissions { } impl UserPermissions { - pub fn new( - review_users: HashSet, - try_users: HashSet, - directory: TeamDirectory, - ) -> Self { - let team_names: HashSet = directory - .teams - .iter() - .map(|name| name.to_lowercase()) - .collect(); - let people_names: HashSet = directory - .usernames - .iter() - .map(|name| name.to_lowercase()) - .collect(); - let directory = TeamDirectory { - teams: team_names, - usernames: people_names, - }; - Self { - review_users, - try_users, - directory, - } - } - pub fn has_permission(&self, user_id: UserId, permission: PermissionType) -> bool { match permission { PermissionType::Review => self.review_users.contains(&user_id),