Skip to main content

rocketmq_protocol/protocol/admin/
topic_offset.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
15use serde::Deserialize;
16use serde::Serialize;
17
18#[derive(Debug, Clone, Serialize, Deserialize, Default)]
19#[serde(rename_all = "camelCase")]
20pub struct TopicOffset {
21    min_offset: i64,
22    max_offset: i64,
23    last_update_timestamp: i64,
24}
25
26impl TopicOffset {
27    pub fn new() -> Self {
28        Self {
29            min_offset: 0,
30            max_offset: 0,
31            last_update_timestamp: 0,
32        }
33    }
34
35    pub fn get_min_offset(&self) -> i64 {
36        self.min_offset
37    }
38
39    pub fn set_min_offset(&mut self, min_offset: i64) {
40        self.min_offset = min_offset;
41    }
42
43    pub fn get_max_offset(&self) -> i64 {
44        self.max_offset
45    }
46
47    pub fn set_max_offset(&mut self, max_offset: i64) {
48        self.max_offset = max_offset;
49    }
50
51    pub fn get_last_update_timestamp(&self) -> i64 {
52        self.last_update_timestamp
53    }
54
55    pub fn set_last_update_timestamp(&mut self, last_update_timestamp: i64) {
56        self.last_update_timestamp = last_update_timestamp;
57    }
58}
59
60impl std::fmt::Display for TopicOffset {
61    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
62        write!(
63            f,
64            "TopicOffset{{min_offset={}, max_offset={}, last_update_timestamp={}}}",
65            self.min_offset, self.max_offset, self.last_update_timestamp
66        )
67    }
68}
69
70#[cfg(test)]
71mod tests {
72    use super::*;
73
74    #[test]
75    fn methods_serde_and_display_preserve_the_offset() {
76        let mut offset = TopicOffset::new();
77        assert_eq!(
78            (
79                offset.get_min_offset(),
80                offset.get_max_offset(),
81                offset.get_last_update_timestamp(),
82            ),
83            (0, 0, 0)
84        );
85        offset.set_min_offset(-10);
86        offset.set_max_offset(-20);
87        offset.set_last_update_timestamp(-1000);
88
89        let serialized = serde_json::to_string(&offset).unwrap();
90        let expected = r#"{"minOffset":-10,"maxOffset":-20,"lastUpdateTimestamp":-1000}"#;
91        assert_eq!(serialized, expected);
92
93        let deserialized: TopicOffset = serde_json::from_str(&serialized).unwrap();
94        assert_eq!(
95            (
96                deserialized.get_min_offset(),
97                deserialized.get_max_offset(),
98                deserialized.get_last_update_timestamp(),
99            ),
100            (-10, -20, -1000)
101        );
102        assert_eq!(
103            deserialized.to_string(),
104            "TopicOffset{min_offset=-10, max_offset=-20, last_update_timestamp=-1000}"
105        );
106    }
107}