rocketmq-protocol 1.0.0

Runtime-neutral RocketMQ wire protocol contracts
Documentation
// Copyright 2023 The RocketMQ Rust Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use serde::Deserialize;
use serde::Serialize;

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct TopicOffset {
    min_offset: i64,
    max_offset: i64,
    last_update_timestamp: i64,
}

impl TopicOffset {
    pub fn new() -> Self {
        Self {
            min_offset: 0,
            max_offset: 0,
            last_update_timestamp: 0,
        }
    }

    pub fn get_min_offset(&self) -> i64 {
        self.min_offset
    }

    pub fn set_min_offset(&mut self, min_offset: i64) {
        self.min_offset = min_offset;
    }

    pub fn get_max_offset(&self) -> i64 {
        self.max_offset
    }

    pub fn set_max_offset(&mut self, max_offset: i64) {
        self.max_offset = max_offset;
    }

    pub fn get_last_update_timestamp(&self) -> i64 {
        self.last_update_timestamp
    }

    pub fn set_last_update_timestamp(&mut self, last_update_timestamp: i64) {
        self.last_update_timestamp = last_update_timestamp;
    }
}

impl std::fmt::Display for TopicOffset {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "TopicOffset{{min_offset={}, max_offset={}, last_update_timestamp={}}}",
            self.min_offset, self.max_offset, self.last_update_timestamp
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn methods_serde_and_display_preserve_the_offset() {
        let mut offset = TopicOffset::new();
        assert_eq!(
            (
                offset.get_min_offset(),
                offset.get_max_offset(),
                offset.get_last_update_timestamp(),
            ),
            (0, 0, 0)
        );
        offset.set_min_offset(-10);
        offset.set_max_offset(-20);
        offset.set_last_update_timestamp(-1000);

        let serialized = serde_json::to_string(&offset).unwrap();
        let expected = r#"{"minOffset":-10,"maxOffset":-20,"lastUpdateTimestamp":-1000}"#;
        assert_eq!(serialized, expected);

        let deserialized: TopicOffset = serde_json::from_str(&serialized).unwrap();
        assert_eq!(
            (
                deserialized.get_min_offset(),
                deserialized.get_max_offset(),
                deserialized.get_last_update_timestamp(),
            ),
            (-10, -20, -1000)
        );
        assert_eq!(
            deserialized.to_string(),
            "TopicOffset{min_offset=-10, max_offset=-20, last_update_timestamp=-1000}"
        );
    }
}