Skip to main content

sofie_engine/rendering/
gates.rs

1//! A part the viewer may not see is never rendered.
2
3pub trait Gates {
4    fn can(&self, permission: &str) -> bool;
5
6    fn enabled(&self, flag: &str) -> bool;
7
8    /// What the viewer's plan holds: a switch, or a limit or quota above zero.
9    fn entitled(&self, entitlement: &str) -> bool {
10        let _ = entitlement;
11        false
12    }
13}
14
15#[derive(Clone, Copy, Default)]
16pub(crate) struct GatesRef<'a>(pub(crate) Option<&'a dyn Gates>);
17
18impl std::fmt::Debug for GatesRef<'_> {
19    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20        formatter.write_str(if self.0.is_some() { "gated" } else { "ungated" })
21    }
22}
23
24impl<'a> crate::language::Language<'a> {
25    pub(crate) fn gates(self) -> Option<&'a dyn Gates> {
26        self.gates.0
27    }
28
29    pub(crate) fn with_gates(mut self, gates: Option<&'a dyn Gates>) -> Self {
30        self.gates = GatesRef(gates);
31        self
32    }
33}
34
35#[derive(Clone, Debug, PartialEq, Eq)]
36pub(crate) struct Gate {
37    pub(crate) asks: Asks,
38    pub(crate) name: String,
39}
40
41#[derive(Clone, Copy, Debug, PartialEq, Eq)]
42pub(crate) enum Asks {
43    Permission,
44    Flag,
45    Entitlement,
46}
47
48impl Asks {
49    /// As a client's view and runtime name the kind of gate.
50    #[cfg(feature = "clients")]
51    pub(crate) fn name(self) -> &'static str {
52        match self {
53            Self::Permission => "permission",
54            Self::Flag => "flag",
55            Self::Entitlement => "entitlement",
56        }
57    }
58}
59
60impl Gate {
61    /// Without gates, as when rendering for no request, nothing gated is shown.
62    pub(crate) fn holds(&self, gates: Option<&dyn Gates>) -> bool {
63        gates.is_some_and(|gates| match self.asks {
64            Asks::Permission => gates.can(&self.name),
65            Asks::Flag => gates.enabled(&self.name),
66            Asks::Entitlement => gates.entitled(&self.name),
67        })
68    }
69}