Skip to main content

base/interface/permission/
profile.rs

1//! What a permit hands on: the confinement the call runs under, what is owed
2//! once it has run, and the two kept for the call as its approval.
3
4use super::{Attendance, Basis, Runs};
5
6/// How a permitted command is confined.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
8#[non_exhaustive]
9pub enum ExecProfile {
10    #[default]
11    Session,
12    /// Let through on its own word: held tighter than one a person approved.
13    Strict,
14    /// Nobody attends the session.
15    Unattended,
16}
17
18/// What the caller owes once a permitted call has run.
19#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
20#[non_exhaustive]
21pub struct Obligations {
22    pub scan_nested_repos: bool,
23}
24
25impl Obligations {
26    pub fn scanning_nested_repos(yes: bool) -> Self {
27        Self {
28            scan_nested_repos: yes,
29        }
30    }
31
32    /// A command confined any looser than [`ExecProfile::Strict`] can leave
33    /// behind a repository whose configuration the engine's own git would
34    /// act on.
35    pub fn after(runs: Option<Runs>, profile: ExecProfile) -> Self {
36        Self::scanning_nested_repos(runs.is_some() && profile != ExecProfile::Strict)
37    }
38}
39
40impl ExecProfile {
41    /// The confinement a permitted call runs under follows from why it was
42    /// let through: on a command's own word it is held tighter than one a
43    /// person (or a rule, a hook, bypass) let through, and nobody attending
44    /// tightens it too.
45    pub fn for_permit(basis: &Basis, runs: Option<Runs>, attendance: Attendance) -> Self {
46        match (basis, runs) {
47            (Basis::Contained | Basis::AcceptEdits, Some(_)) => Self::Strict,
48            (Basis::UnattendedConfined, _) => Self::Unattended,
49            _ if attendance == Attendance::Unattended => Self::Unattended,
50            _ => Self::Session,
51        }
52    }
53}
54
55/// What the call being run was let through on: `ToolContext::approval`.
56#[derive(Debug, Clone, PartialEq, Eq)]
57#[non_exhaustive]
58pub struct Approval {
59    pub basis: Basis,
60    pub profile: ExecProfile,
61    /// Where the call's write was judged to land
62    /// ([`PermissionFacts::lands_at`](super::PermissionFacts::lands_at)).
63    pub landing: Option<std::path::PathBuf>,
64}
65
66impl Approval {
67    pub fn new(basis: Basis, profile: ExecProfile) -> Self {
68        Self {
69            basis,
70            profile,
71            landing: None,
72        }
73    }
74
75    pub fn landing_at(mut self, landing: Option<std::path::PathBuf>) -> Self {
76        self.landing = landing;
77        self
78    }
79}