Skip to main content

rs_teststand/users/
privilege.rs

1//! Built-in privilege names.
2
3/// A built-in privilege, as passed to a privilege check.
4///
5/// The engine identifies privileges by name, so this enum exists to stop a
6/// caller mistyping one into a check that then silently reports `false`.
7///
8/// Note that the constant and the value it carries are not always the same
9/// word: `CtrlExecFlow` is written `ControlExecFlow` on the wire. Using this
10/// type rather than a literal avoids having to remember which is which.
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
12pub enum UserPrivilege {
13    /// `Abort`.
14    Abort,
15    /// `ConfigAdapter`.
16    ConfigAdapter,
17    /// `ConfigApp`.
18    ConfigApp,
19    /// `ConfigDatabase`.
20    ConfigDatabase,
21    /// `ConfigEngine`.
22    ConfigEngine,
23    /// `ConfigModel`.
24    ConfigModel,
25    /// `ConfigReport`.
26    ConfigReport,
27    /// `Configure`.
28    Configure,
29    /// `CtrlExecFlow`.
30    CtrlExecFlow,
31    /// `Debug`.
32    Debug,
33    /// `Develop`.
34    Develop,
35    /// `EditProcessModelFiles`.
36    EditProcessModelFiles,
37    /// `EditRuntimeVariables`.
38    EditRuntimeVariables,
39    /// `EditSequenceFiles`.
40    EditSequenceFiles,
41    /// `EditStationGlobals`.
42    EditStationGlobals,
43    /// `EditTemplates`.
44    EditTemplates,
45    /// `EditTypes`.
46    EditTypes,
47    /// `EditUsers`.
48    EditUsers,
49    /// `EditWorkspace`.
50    EditWorkspace,
51    /// `Execute`.
52    Execute,
53    /// `GrantAll`.
54    GrantAll,
55    /// `LoopSelectedTests`.
56    LoopSelectedTests,
57    /// `Operate`.
58    Operate,
59    /// `RunAnySequence`.
60    RunAnySequence,
61    /// `RunSelectedTests`.
62    RunSelectedTests,
63    /// `SaveSequenceFiles`.
64    SaveSequenceFiles,
65    /// `SinglePass`.
66    SinglePass,
67    /// `Terminate`.
68    Terminate,
69    /// `UseSourceControl`.
70    UseSourceControl,
71}
72
73impl UserPrivilege {
74    /// Every built-in privilege.
75    pub const ALL: [Self; 29] = [
76        Self::Abort,
77        Self::ConfigAdapter,
78        Self::ConfigApp,
79        Self::ConfigDatabase,
80        Self::ConfigEngine,
81        Self::ConfigModel,
82        Self::ConfigReport,
83        Self::Configure,
84        Self::CtrlExecFlow,
85        Self::Debug,
86        Self::Develop,
87        Self::EditProcessModelFiles,
88        Self::EditRuntimeVariables,
89        Self::EditSequenceFiles,
90        Self::EditStationGlobals,
91        Self::EditTemplates,
92        Self::EditTypes,
93        Self::EditUsers,
94        Self::EditWorkspace,
95        Self::Execute,
96        Self::GrantAll,
97        Self::LoopSelectedTests,
98        Self::Operate,
99        Self::RunAnySequence,
100        Self::RunSelectedTests,
101        Self::SaveSequenceFiles,
102        Self::SinglePass,
103        Self::Terminate,
104        Self::UseSourceControl,
105    ];
106
107    /// The name the engine expects.
108    ///
109    /// A privilege can also be named by its full path, such as
110    /// `Debug.RunSelectedTests`; this returns the base name.
111    #[must_use]
112    pub const fn name(self) -> &'static str {
113        match self {
114            Self::Abort => "Abort",
115            Self::ConfigAdapter => "ConfigAdapter",
116            Self::ConfigApp => "ConfigApp",
117            Self::ConfigDatabase => "ConfigDatabase",
118            Self::ConfigEngine => "ConfigEngine",
119            Self::ConfigModel => "ConfigModel",
120            Self::ConfigReport => "ConfigReport",
121            Self::Configure => "Configure",
122            Self::CtrlExecFlow => "ControlExecFlow",
123            Self::Debug => "Debug",
124            Self::Develop => "Develop",
125            Self::EditProcessModelFiles => "EditProcessModelFiles",
126            Self::EditRuntimeVariables => "EditRuntimeVariables",
127            Self::EditSequenceFiles => "EditSequenceFiles",
128            Self::EditStationGlobals => "EditStationGlobals",
129            Self::EditTemplates => "EditTemplates",
130            Self::EditTypes => "EditTypes",
131            Self::EditUsers => "EditUsers",
132            Self::EditWorkspace => "EditWorkspace",
133            Self::Execute => "Execute",
134            Self::GrantAll => "GrantAll",
135            Self::LoopSelectedTests => "LoopSelectedTests",
136            Self::Operate => "Operate",
137            Self::RunAnySequence => "RunAnySequence",
138            Self::RunSelectedTests => "RunSelectedTests",
139            Self::SaveSequenceFiles => "SaveSequenceFiles",
140            Self::SinglePass => "SinglePass",
141            Self::Terminate => "Terminate",
142            Self::UseSourceControl => "UseSourceControl",
143        }
144    }
145}
146
147#[cfg(test)]
148mod tests {
149    use super::UserPrivilege;
150
151    #[test]
152    fn every_privilege_name_is_distinct() {
153        let mut names: Vec<&str> = UserPrivilege::ALL.iter().map(|p| p.name()).collect();
154        names.sort_unstable();
155        let count = names.len();
156        names.dedup();
157        assert_eq!(names.len(), count);
158    }
159
160    #[test]
161    fn the_constant_and_its_value_can_differ() {
162        // The one place the spelling changes: writing "CtrlExecFlow" into a
163        // check would match nothing and report false without an error.
164        assert_eq!(UserPrivilege::CtrlExecFlow.name(), "ControlExecFlow");
165        assert_eq!(UserPrivilege::Debug.name(), "Debug");
166    }
167}