Skip to main content

ephemeris_forge_forgejo/config/
push_access.rs

1use facet::Facet;
2
3/// Who may push directly to a protected branch.
4#[derive(Clone, Copy, Debug, Eq, Facet, PartialEq)]
5#[repr(u8)]
6pub enum PushAccess {
7    /// Nobody. Changes arrive through pull requests only.
8    #[facet(rename = "disabled")]
9    Disabled,
10    /// Everyone with write access.
11    #[facet(rename = "enabled")]
12    Enabled,
13    /// Only the push whitelist.
14    #[facet(rename = "whitelist")]
15    Whitelist,
16}
17
18impl PushAccess {
19    /// Read the access from Forgejo's two wire flags.
20    #[must_use]
21    pub const fn from_flags(enable_push: bool, enable_push_whitelist: bool) -> Self {
22        match (enable_push, enable_push_whitelist) {
23            (false, _) => Self::Disabled,
24            (true, false) => Self::Enabled,
25            (true, true) => Self::Whitelist,
26        }
27    }
28
29    /// Forgejo's `enable_push` flag.
30    #[must_use]
31    pub const fn enables_push(self) -> bool {
32        !matches!(self, Self::Disabled)
33    }
34
35    /// Forgejo's `enable_push_whitelist` flag.
36    #[must_use]
37    pub const fn restricts_to_whitelist(self) -> bool {
38        matches!(self, Self::Whitelist)
39    }
40}
41
42#[cfg(test)]
43mod tests {
44    use super::PushAccess;
45
46    #[test]
47    fn each_access_round_trips_through_the_flags() {
48        for access in [
49            PushAccess::Disabled,
50            PushAccess::Enabled,
51            PushAccess::Whitelist,
52        ] {
53            assert_eq!(
54                PushAccess::from_flags(access.enables_push(), access.restricts_to_whitelist()),
55                access
56            );
57        }
58    }
59
60    #[test]
61    fn a_whitelist_without_push_is_disabled() {
62        assert_eq!(PushAccess::from_flags(false, true), PushAccess::Disabled);
63    }
64}