Skip to main content

rs_teststand/expression/function/
switching.rs

1//! Switching expression functions.
2
3/// A switching function of the expression language.
4///
5/// Every name begins with `Switch`, matching the engine; renaming them to drop
6/// the prefix would break the one-to-one mapping this crate keeps.
7///
8/// Names only: what each one computes is the engine's to document.
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
10#[allow(clippy::enum_variant_names, reason = "names mirror the engine exactly")]
11pub enum SwitchingFunction {
12    /// `SwitchConnect`.
13    SwitchConnect,
14    /// `SwitchConnectDisconnect`.
15    SwitchConnectDisconnect,
16    /// `SwitchDisconnect`.
17    SwitchDisconnect,
18    /// `SwitchDisconnectAll`.
19    SwitchDisconnectAll,
20    /// `SwitchFindRoute`.
21    SwitchFindRoute,
22}
23
24impl SwitchingFunction {
25    /// Every function in this family.
26    pub const ALL: [Self; 5] = [
27        Self::SwitchConnect,
28        Self::SwitchConnectDisconnect,
29        Self::SwitchDisconnect,
30        Self::SwitchDisconnectAll,
31        Self::SwitchFindRoute,
32    ];
33
34    /// The name as written in an expression.
35    #[must_use]
36    pub const fn name(self) -> &'static str {
37        match self {
38            Self::SwitchConnect => "SwitchConnect",
39            Self::SwitchConnectDisconnect => "SwitchConnectDisconnect",
40            Self::SwitchDisconnect => "SwitchDisconnect",
41            Self::SwitchDisconnectAll => "SwitchDisconnectAll",
42            Self::SwitchFindRoute => "SwitchFindRoute",
43        }
44    }
45}
46
47#[cfg(test)]
48mod tests {
49    use super::SwitchingFunction;
50
51    #[test]
52    fn every_name_is_distinct() {
53        let mut names: Vec<&str> = SwitchingFunction::ALL.iter().map(|f| f.name()).collect();
54        names.sort_unstable();
55        let count = names.len();
56        names.dedup();
57        assert_eq!(names.len(), count);
58    }
59}