ephemeris_forge/access/caller.rs
1use std::collections::BTreeSet;
2
3use super::{EmptyUserName, TeamName, UserName};
4
5/// The account the tool runs as.
6#[derive(Clone, Debug, Eq, PartialEq)]
7pub enum Caller {
8 /// A user account and the teams it belongs to.
9 User {
10 /// The caller's login.
11 login: UserName,
12 /// The teams the caller belongs to in the repository's organization.
13 teams: BTreeSet<TeamName>,
14 },
15 /// An app installation. Its access does not depend on collaborators or
16 /// teams.
17 App {
18 /// The numeric app id.
19 id: u64,
20 /// The app slug.
21 slug: String,
22 /// Whether the installation holds the administration permission.
23 admin: bool,
24 },
25}
26
27impl Caller {
28 /// A user caller with the teams it belongs to.
29 ///
30 /// # Errors
31 /// Returns an error when `login` is empty.
32 pub fn user(login: String, teams: BTreeSet<TeamName>) -> Result<Self, EmptyUserName> {
33 Ok(Self::User {
34 login: UserName::try_from(login)?,
35 teams,
36 })
37 }
38
39 /// Whether this caller is the user `name`.
40 #[must_use]
41 pub fn is_user(&self, name: &UserName) -> bool {
42 matches!(self, Self::User { login, teams: _ } if login == name)
43 }
44
45 /// Whether this caller is a user that belongs to `team`.
46 #[must_use]
47 pub fn belongs_to(&self, team: &TeamName) -> bool {
48 matches!(self, Self::User { login: _, teams } if teams.contains(team))
49 }
50}