rocketmq_auth/authorization/enums/
decision.rs1#[derive(Debug, Copy, Clone, Eq, PartialEq)]
16#[repr(u8)]
17pub enum Decision {
18 Allow = 1,
19 Deny = 2,
20}
21
22impl Decision {
23 pub fn code(self) -> u8 {
24 self as u8
25 }
26
27 pub fn name(self) -> &'static str {
28 match self {
29 Decision::Allow => "Allow",
30 Decision::Deny => "Deny",
31 }
32 }
33
34 pub fn get_by_name(name: &str) -> Option<Self> {
35 if name.eq_ignore_ascii_case("Allow") {
36 Some(Decision::Allow)
37 } else if name.eq_ignore_ascii_case("Deny") {
38 Some(Decision::Deny)
39 } else {
40 None
41 }
42 }
43}
44
45impl From<Decision> for u8 {
46 fn from(d: Decision) -> Self {
47 d.code()
48 }
49}
50
51impl TryFrom<u8> for Decision {
52 type Error = ();
53
54 fn try_from(value: u8) -> Result<Self, Self::Error> {
55 match value {
56 1 => Ok(Decision::Allow),
57 2 => Ok(Decision::Deny),
58 _ => Err(()),
59 }
60 }
61}
62
63#[cfg(test)]
64mod tests {
65 use super::*;
66
67 #[test]
68 fn test_decision_code() {
69 assert_eq!(Decision::Allow.code(), 1);
70 assert_eq!(Decision::Deny.code(), 2);
71 }
72
73 #[test]
74 fn test_decision_name() {
75 assert_eq!(Decision::Allow.name(), "Allow");
76 assert_eq!(Decision::Deny.name(), "Deny");
77 }
78
79 #[test]
80 fn test_decision_get_by_name() {
81 assert_eq!(Decision::get_by_name("Allow"), Some(Decision::Allow));
82 assert_eq!(Decision::get_by_name("allow"), Some(Decision::Allow));
83 assert_eq!(Decision::get_by_name("Deny"), Some(Decision::Deny));
84 assert_eq!(Decision::get_by_name("deny"), Some(Decision::Deny));
85 assert_eq!(Decision::get_by_name("Unknown"), None);
86 }
87
88 #[test]
89 fn test_decision_from_u8() {
90 assert_eq!(u8::from(Decision::Allow), 1);
91 assert_eq!(u8::from(Decision::Deny), 2);
92 }
93
94 #[test]
95 fn test_decision_try_from_u8() {
96 assert_eq!(Decision::try_from(1), Ok(Decision::Allow));
97 assert_eq!(Decision::try_from(2), Ok(Decision::Deny));
98 assert_eq!(Decision::try_from(3), Err(()));
99 }
100}