rocketmq_common/common/
constant.rs1pub mod consume_init_mode;
19
20use std::ops::Deref;
21
22pub struct PermName;
23
24impl PermName {
25 pub const INDEX_PERM_INHERIT: u32 = 0;
26 pub const INDEX_PERM_PRIORITY: u32 = 3;
27 pub const INDEX_PERM_READ: u32 = 2;
28 pub const INDEX_PERM_WRITE: u32 = 1;
29 pub const PERM_INHERIT: u32 = 0x1 << Self::INDEX_PERM_INHERIT;
30 pub const PERM_PRIORITY: u32 = 0x1 << Self::INDEX_PERM_PRIORITY;
31 pub const PERM_READ: u32 = 0x1 << Self::INDEX_PERM_READ;
32 pub const PERM_WRITE: u32 = 0x1 << Self::INDEX_PERM_WRITE;
33
34 #[inline]
35 pub fn perm2string(perm: u32) -> String {
36 let mut sb = String::from("---");
37
38 if Self::is_readable(perm) {
39 sb.replace_range(0..1, "R");
40 }
41
42 if Self::is_writeable(perm) {
43 sb.replace_range(1..2, "W");
44 }
45
46 if Self::is_inherited(perm) {
47 sb.replace_range(2..3, "X");
48 }
49
50 sb
51 }
52
53 #[inline]
54 pub fn perm_to_string(perm: u32) -> String {
55 let mut s = ['-', '-', '-'];
56 if Self::is_readable(perm) {
57 s[0] = 'R';
58 }
59 if Self::is_writeable(perm) {
60 s[1] = 'W';
61 }
62 if Self::is_inherited(perm) {
63 s[2] = 'X';
64 }
65 s.iter().collect()
66 }
67
68 #[inline]
69 pub fn is_readable(perm: u32) -> bool {
70 (perm & Self::PERM_READ) == Self::PERM_READ
71 }
72
73 #[inline]
74 pub fn is_writeable(perm: u32) -> bool {
75 (perm & Self::PERM_WRITE) == Self::PERM_WRITE
76 }
77
78 #[inline]
79 pub fn is_inherited(perm: u32) -> bool {
80 (perm & Self::PERM_INHERIT) == Self::PERM_INHERIT
81 }
82
83 #[inline]
84 pub fn is_valid(perm: u32) -> bool {
85 perm < Self::PERM_PRIORITY
86 }
87
88 #[inline]
89 pub fn is_priority(perm: u32) -> bool {
90 (perm & Self::PERM_PRIORITY) == Self::PERM_PRIORITY
91 }
92
93 #[inline]
94 pub fn is_valid_str(perm: &str) -> bool {
95 perm.parse::<u32>()
96 .ok()
97 .map(Self::is_valid)
98 .unwrap_or(false)
99 }
100}
101
102#[cfg(test)]
103mod tests {
104 use super::PermName;
105
106 #[test]
107 fn test_perm_2_string() {
108 assert_eq!(PermName::perm_to_string(0).as_str(), "---");
109 assert_eq!(
110 PermName::perm_to_string(PermName::PERM_READ).as_str(),
111 "R--"
112 );
113 assert_eq!(
114 PermName::perm_to_string(PermName::PERM_READ | PermName::PERM_WRITE).as_str(),
115 "RW-"
116 );
117
118 assert_eq!(
119 PermName::perm_to_string(
120 PermName::PERM_READ | PermName::PERM_WRITE | PermName::PERM_INHERIT
121 ),
122 "RWX"
123 );
124 }
125
126 #[test]
127 fn test_is_readable() {
128 assert!(!PermName::is_readable(0));
129 assert!(PermName::is_readable(PermName::PERM_READ));
130 assert!(PermName::is_readable(
131 PermName::PERM_READ | PermName::PERM_WRITE
132 ));
133 assert!(PermName::is_readable(
134 PermName::PERM_READ | PermName::PERM_WRITE | PermName::PERM_PRIORITY
135 ));
136 assert!(PermName::is_readable(
137 PermName::PERM_READ
138 | PermName::PERM_WRITE
139 | PermName::PERM_PRIORITY
140 | PermName::PERM_INHERIT
141 ));
142 }
143
144 #[test]
145 fn test_is_writable() {
146 assert!(!PermName::is_writeable(0));
147 assert!(PermName::is_writeable(PermName::PERM_WRITE));
148 assert!(PermName::is_writeable(
149 PermName::PERM_READ | PermName::PERM_WRITE
150 ));
151 assert!(PermName::is_writeable(
152 PermName::PERM_READ | PermName::PERM_WRITE | PermName::PERM_PRIORITY
153 ));
154 assert!(PermName::is_writeable(
155 PermName::PERM_READ
156 | PermName::PERM_WRITE
157 | PermName::PERM_PRIORITY
158 | PermName::PERM_INHERIT
159 ));
160 }
161
162 #[test]
163 fn test_is_priority() {
164 assert!(!PermName::is_priority(0));
165 assert!(PermName::is_priority(PermName::PERM_PRIORITY));
166 assert!(!PermName::is_priority(
167 PermName::PERM_READ | PermName::PERM_WRITE
168 ));
169 assert!(PermName::is_priority(
170 PermName::PERM_READ | PermName::PERM_WRITE | PermName::PERM_PRIORITY
171 ));
172 assert!(PermName::is_priority(
173 PermName::PERM_READ
174 | PermName::PERM_WRITE
175 | PermName::PERM_PRIORITY
176 | PermName::PERM_INHERIT
177 ));
178 }
179
180 #[test]
181 fn test_is_inherit() {
182 assert!(!PermName::is_inherited(0));
183 assert!(PermName::is_inherited(PermName::PERM_INHERIT));
184 assert!(!PermName::is_inherited(
185 PermName::PERM_READ | PermName::PERM_WRITE
186 ));
187 assert!(!PermName::is_inherited(
188 PermName::PERM_READ | PermName::PERM_WRITE | PermName::PERM_PRIORITY
189 ));
190 assert!(PermName::is_inherited(
191 PermName::PERM_READ
192 | PermName::PERM_WRITE
193 | PermName::PERM_PRIORITY
194 | PermName::PERM_INHERIT
195 ));
196 }
197
198 #[test]
199 fn valid_str_returns_true_for_valid_permission() {
200 assert!(PermName::is_valid_str("1"));
201 assert!(PermName::is_valid_str("0"));
202 }
203
204 #[test]
205 fn valid_str_returns_false_for_invalid_permission() {
206 assert!(PermName::is_valid_str("4"));
207 assert!(!PermName::is_valid_str("-1"));
208 }
209
210 #[test]
211 fn valid_str_returns_false_for_non_numeric_input() {
212 assert!(!PermName::is_valid_str("abc"));
213 assert!(!PermName::is_valid_str(""));
214 }
215}