Skip to main content

rocketmq_model/common/
constant.rs

1// Copyright 2023 The RocketMQ Rust Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15pub mod common_constants;
16pub mod consume_init_mode;
17pub mod db_msg_constants;
18pub mod file_readahead_mode;
19pub mod ha_proxy_constants;
20
21pub struct PermName;
22
23impl PermName {
24    pub const INDEX_PERM_INHERIT: u32 = 0;
25    pub const INDEX_PERM_PRIORITY: u32 = 3;
26    pub const INDEX_PERM_READ: u32 = 2;
27    pub const INDEX_PERM_WRITE: u32 = 1;
28    pub const PERM_INHERIT: u32 = 0x1 << Self::INDEX_PERM_INHERIT;
29    pub const PERM_PRIORITY: u32 = 0x1 << Self::INDEX_PERM_PRIORITY;
30    pub const PERM_READ: u32 = 0x1 << Self::INDEX_PERM_READ;
31    pub const PERM_WRITE: u32 = 0x1 << Self::INDEX_PERM_WRITE;
32
33    #[inline]
34    pub fn perm2string(perm: u32) -> String {
35        let mut sb = String::from("---");
36
37        if Self::is_readable(perm) {
38            sb.replace_range(0..1, "R");
39        }
40
41        if Self::is_writeable(perm) {
42            sb.replace_range(1..2, "W");
43        }
44
45        if Self::is_inherited(perm) {
46            sb.replace_range(2..3, "X");
47        }
48
49        sb
50    }
51
52    #[inline]
53    pub fn perm_to_string(perm: u32) -> String {
54        let mut s = ['-', '-', '-'];
55        if Self::is_readable(perm) {
56            s[0] = 'R';
57        }
58        if Self::is_writeable(perm) {
59            s[1] = 'W';
60        }
61        if Self::is_inherited(perm) {
62            s[2] = 'X';
63        }
64        s.iter().collect()
65    }
66
67    #[inline]
68    pub fn is_readable(perm: u32) -> bool {
69        (perm & Self::PERM_READ) == Self::PERM_READ
70    }
71
72    #[inline]
73    pub fn is_writeable(perm: u32) -> bool {
74        (perm & Self::PERM_WRITE) == Self::PERM_WRITE
75    }
76
77    #[inline]
78    pub fn is_inherited(perm: u32) -> bool {
79        (perm & Self::PERM_INHERIT) == Self::PERM_INHERIT
80    }
81
82    #[inline]
83    pub fn is_valid(perm: u32) -> bool {
84        perm < Self::PERM_PRIORITY
85    }
86
87    #[inline]
88    pub fn is_priority(perm: u32) -> bool {
89        (perm & Self::PERM_PRIORITY) == Self::PERM_PRIORITY
90    }
91
92    #[inline]
93    pub fn is_valid_str(perm: &str) -> bool {
94        perm.parse::<u32>().ok().map(Self::is_valid).unwrap_or(false)
95    }
96}
97
98#[cfg(test)]
99mod tests {
100    use super::PermName;
101
102    #[test]
103    fn test_perm_2_string() {
104        assert_eq!(PermName::perm_to_string(0).as_str(), "---");
105        assert_eq!(PermName::perm_to_string(PermName::PERM_READ).as_str(), "R--");
106        assert_eq!(
107            PermName::perm_to_string(PermName::PERM_READ | PermName::PERM_WRITE).as_str(),
108            "RW-"
109        );
110
111        assert_eq!(
112            PermName::perm_to_string(PermName::PERM_READ | PermName::PERM_WRITE | PermName::PERM_INHERIT),
113            "RWX"
114        );
115    }
116
117    #[test]
118    fn test_is_readable() {
119        assert!(!PermName::is_readable(0));
120        assert!(PermName::is_readable(PermName::PERM_READ));
121        assert!(PermName::is_readable(PermName::PERM_READ | PermName::PERM_WRITE));
122        assert!(PermName::is_readable(
123            PermName::PERM_READ | PermName::PERM_WRITE | PermName::PERM_PRIORITY
124        ));
125        assert!(PermName::is_readable(
126            PermName::PERM_READ | PermName::PERM_WRITE | PermName::PERM_PRIORITY | PermName::PERM_INHERIT
127        ));
128    }
129
130    #[test]
131    fn test_is_writable() {
132        assert!(!PermName::is_writeable(0));
133        assert!(PermName::is_writeable(PermName::PERM_WRITE));
134        assert!(PermName::is_writeable(PermName::PERM_READ | PermName::PERM_WRITE));
135        assert!(PermName::is_writeable(
136            PermName::PERM_READ | PermName::PERM_WRITE | PermName::PERM_PRIORITY
137        ));
138        assert!(PermName::is_writeable(
139            PermName::PERM_READ | PermName::PERM_WRITE | PermName::PERM_PRIORITY | PermName::PERM_INHERIT
140        ));
141    }
142
143    #[test]
144    fn test_is_priority() {
145        assert!(!PermName::is_priority(0));
146        assert!(PermName::is_priority(PermName::PERM_PRIORITY));
147        assert!(!PermName::is_priority(PermName::PERM_READ | PermName::PERM_WRITE));
148        assert!(PermName::is_priority(
149            PermName::PERM_READ | PermName::PERM_WRITE | PermName::PERM_PRIORITY
150        ));
151        assert!(PermName::is_priority(
152            PermName::PERM_READ | PermName::PERM_WRITE | PermName::PERM_PRIORITY | PermName::PERM_INHERIT
153        ));
154    }
155
156    #[test]
157    fn test_is_inherit() {
158        assert!(!PermName::is_inherited(0));
159        assert!(PermName::is_inherited(PermName::PERM_INHERIT));
160        assert!(!PermName::is_inherited(PermName::PERM_READ | PermName::PERM_WRITE));
161        assert!(!PermName::is_inherited(
162            PermName::PERM_READ | PermName::PERM_WRITE | PermName::PERM_PRIORITY
163        ));
164        assert!(PermName::is_inherited(
165            PermName::PERM_READ | PermName::PERM_WRITE | PermName::PERM_PRIORITY | PermName::PERM_INHERIT
166        ));
167    }
168
169    #[test]
170    fn valid_str_returns_true_for_valid_permission() {
171        assert!(PermName::is_valid_str("1"));
172        assert!(PermName::is_valid_str("0"));
173    }
174
175    #[test]
176    fn valid_str_returns_false_for_invalid_permission() {
177        assert!(PermName::is_valid_str("4"));
178        assert!(!PermName::is_valid_str("-1"));
179    }
180
181    #[test]
182    fn valid_str_returns_false_for_non_numeric_input() {
183        assert!(!PermName::is_valid_str("abc"));
184        assert!(!PermName::is_valid_str(""));
185    }
186}