Skip to main content

ephemeris_forge/access/
state.rs

1use std::collections::{BTreeMap, BTreeSet};
2
3use super::{Caller, Grants, Observed, PermissionScope, TeamName, UserName};
4
5/// The team and collaborator grants a forge reports for one repository.
6///
7/// `P` is the forge's permission type. Every field is empty unless access is
8/// managed.
9#[derive(Clone, Debug)]
10pub struct AccessState<P> {
11    /// Direct repository team grants.
12    pub teams: BTreeMap<TeamName, Observed<P>>,
13    /// Teams that reach the repository through an organization or an
14    /// enterprise and cannot be removed from it.
15    pub inherited_teams: BTreeSet<TeamName>,
16    /// Organization teams available for assignment.
17    pub organization_teams: BTreeMap<TeamName, Observed<P>>,
18    /// Direct collaborators.
19    pub collaborators: BTreeMap<UserName, Observed<P>>,
20    /// The authenticated account. `None` unless access is managed.
21    pub caller: Option<Caller>,
22}
23
24impl<P> AccessState<P> {
25    /// The team grants as the planner reads them.
26    #[must_use]
27    pub const fn grants(&self, scope: PermissionScope) -> Grants<'_, P> {
28        Grants {
29            scope,
30            direct: &self.teams,
31            inherited: &self.inherited_teams,
32            organization: &self.organization_teams,
33            caller: self.caller.as_ref(),
34        }
35    }
36}
37
38impl<P> Default for AccessState<P> {
39    fn default() -> Self {
40        Self {
41            teams: BTreeMap::new(),
42            inherited_teams: BTreeSet::new(),
43            organization_teams: BTreeMap::new(),
44            collaborators: BTreeMap::new(),
45            caller: None,
46        }
47    }
48}