Skip to main content

rocketmq_model/common/sys_flag/
pull_sys_flag.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 struct PullSysFlag;
16
17impl PullSysFlag {
18    const FLAG_COMMIT_OFFSET: u32 = 0x1;
19    const FLAG_SUSPEND: u32 = 0x1 << 1;
20    const FLAG_SUBSCRIPTION: u32 = 0x1 << 2;
21    const FLAG_CLASS_FILTER: u32 = 0x1 << 3;
22    const FLAG_LITE_PULL_MESSAGE: u32 = 0x1 << 4;
23
24    pub fn build_sys_flag(commit_offset: bool, suspend: bool, subscription: bool, class_filter: bool) -> u32 {
25        let mut flag = 0;
26
27        if commit_offset {
28            flag |= Self::FLAG_COMMIT_OFFSET;
29        }
30
31        if suspend {
32            flag |= Self::FLAG_SUSPEND;
33        }
34
35        if subscription {
36            flag |= Self::FLAG_SUBSCRIPTION;
37        }
38
39        if class_filter {
40            flag |= Self::FLAG_CLASS_FILTER;
41        }
42
43        flag
44    }
45
46    pub fn build_sys_flag_with_lite_pull(
47        commit_offset: bool,
48        suspend: bool,
49        subscription: bool,
50        class_filter: bool,
51        lite_pull: bool,
52    ) -> u32 {
53        let mut flag = Self::build_sys_flag(commit_offset, suspend, subscription, class_filter);
54
55        if lite_pull {
56            flag |= Self::FLAG_LITE_PULL_MESSAGE;
57        }
58
59        flag
60    }
61
62    pub fn clear_commit_offset_flag(sys_flag: u32) -> u32 {
63        sys_flag & (!Self::FLAG_COMMIT_OFFSET)
64    }
65
66    pub fn has_commit_offset_flag(sys_flag: u32) -> bool {
67        (sys_flag & Self::FLAG_COMMIT_OFFSET) == Self::FLAG_COMMIT_OFFSET
68    }
69
70    pub fn has_suspend_flag(sys_flag: u32) -> bool {
71        (sys_flag & Self::FLAG_SUSPEND) == Self::FLAG_SUSPEND
72    }
73
74    pub fn clear_suspend_flag(sys_flag: u32) -> u32 {
75        sys_flag & (!Self::FLAG_SUSPEND)
76    }
77
78    pub fn has_subscription_flag(sys_flag: u32) -> bool {
79        (sys_flag & Self::FLAG_SUBSCRIPTION) == Self::FLAG_SUBSCRIPTION
80    }
81
82    pub fn build_sys_flag_with_subscription(sys_flag: u32) -> u32 {
83        sys_flag | Self::FLAG_SUBSCRIPTION
84    }
85
86    pub fn has_class_filter_flag(sys_flag: u32) -> bool {
87        (sys_flag & Self::FLAG_CLASS_FILTER) == Self::FLAG_CLASS_FILTER
88    }
89
90    pub fn has_lite_pull_flag(sys_flag: u32) -> bool {
91        (sys_flag & Self::FLAG_LITE_PULL_MESSAGE) == Self::FLAG_LITE_PULL_MESSAGE
92    }
93}
94
95#[cfg(test)]
96mod tests {
97    use super::*;
98
99    #[test]
100    fn build_sys_flag_sets_correct_flags() {
101        let flag = PullSysFlag::build_sys_flag(true, true, true, true);
102        assert_eq!(flag, 0b1111);
103    }
104
105    #[test]
106    fn build_sys_flag_with_lite_pull_sets_correct_flags() {
107        let flag = PullSysFlag::build_sys_flag_with_lite_pull(true, true, true, true, true);
108        assert_eq!(flag, 0b11111);
109    }
110
111    #[test]
112    fn clear_commit_offset_flag_clears_correct_flag() {
113        let flag = PullSysFlag::clear_commit_offset_flag(0b1111);
114        assert_eq!(flag, 0b1110);
115    }
116
117    #[test]
118    fn has_commit_offset_flag_returns_correct_result() {
119        assert!(PullSysFlag::has_commit_offset_flag(0b1));
120        assert!(!PullSysFlag::has_commit_offset_flag(0b10));
121    }
122
123    #[test]
124    fn has_suspend_flag_returns_correct_result() {
125        assert!(PullSysFlag::has_suspend_flag(0b10));
126        assert!(!PullSysFlag::has_suspend_flag(0b1));
127    }
128
129    #[test]
130    fn clear_suspend_flag_clears_correct_flag() {
131        let flag = PullSysFlag::clear_suspend_flag(0b1111);
132        assert_eq!(flag, 0b1101);
133    }
134
135    #[test]
136    fn has_subscription_flag_returns_correct_result() {
137        assert!(PullSysFlag::has_subscription_flag(0b100));
138        assert!(!PullSysFlag::has_subscription_flag(0b1));
139    }
140
141    #[test]
142    fn build_sys_flag_with_subscription_sets_correct_flag() {
143        let flag = PullSysFlag::build_sys_flag_with_subscription(0b1);
144        assert_eq!(flag, 0b101);
145    }
146
147    #[test]
148    fn has_class_filter_flag_returns_correct_result() {
149        assert!(PullSysFlag::has_class_filter_flag(0b1000));
150        assert!(!PullSysFlag::has_class_filter_flag(0b1));
151    }
152
153    #[test]
154    fn has_lite_pull_flag_returns_correct_result() {
155        assert!(PullSysFlag::has_lite_pull_flag(0b10000));
156        assert!(!PullSysFlag::has_lite_pull_flag(0b1));
157    }
158}