Skip to main content

rocketmq_client_rust/base/
query_result.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 std::fmt;
16
17use rocketmq_common::common::message::message_ext::MessageExt;
18
19#[derive(Debug, Clone, Default)]
20pub struct QueryResult {
21    index_last_update_timestamp: u64,
22    message_list: Vec<MessageExt>,
23}
24
25impl QueryResult {
26    // Constructor equivalent
27    #[inline]
28    pub fn new(index_last_update_timestamp: u64, message_list: Vec<MessageExt>) -> Self {
29        QueryResult {
30            index_last_update_timestamp,
31            message_list,
32        }
33    }
34
35    // Getter methods equivalent
36    #[inline]
37    pub fn index_last_update_timestamp(&self) -> u64 {
38        self.index_last_update_timestamp
39    }
40
41    #[inline]
42    pub fn message_list(&self) -> &Vec<MessageExt> {
43        &self.message_list
44    }
45}
46
47fn format_message_list(message_list: &[MessageExt]) -> String {
48    let mut result = String::from("[");
49    for (index, message) in message_list.iter().enumerate() {
50        if index > 0 {
51            result.push_str(", ");
52        }
53        result.push_str(&message.to_string());
54    }
55    result.push(']');
56    result
57}
58
59// Implementing the Display trait for pretty printing, similar to Java's toString method
60impl fmt::Display for QueryResult {
61    #[inline]
62    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63        write!(
64            f,
65            "QueryResult [indexLastUpdateTimestamp={}, messageList={}]",
66            self.index_last_update_timestamp,
67            format_message_list(&self.message_list)
68        )
69    }
70}
71
72#[cfg(test)]
73mod tests {
74    use super::*;
75
76    #[test]
77    fn query_result_display_matches_java_to_string_shape_for_empty_list() {
78        let query_result = QueryResult::new(123, Vec::new());
79
80        assert_eq!(
81            query_result.to_string(),
82            "QueryResult [indexLastUpdateTimestamp=123, messageList=[]]"
83        );
84    }
85}