Skip to main content

rocketmq_protocol/trace/
trace_record.rs

1// Copyright 2026 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 cheetah_string::CheetahString;
16
17use super::TraceType;
18
19#[derive(Clone, Debug, Eq, PartialEq)]
20pub struct TraceRecord {
21    pub trace_type: TraceType,
22    pub fields: Vec<CheetahString>,
23}
24
25impl TraceRecord {
26    pub fn new(trace_type: TraceType, fields: Vec<CheetahString>) -> Self {
27        Self { trace_type, fields }
28    }
29}
30
31#[cfg(test)]
32mod tests {
33    use super::*;
34
35    fn fields() -> Vec<CheetahString> {
36        vec![
37            CheetahString::from_static_str("1710000000000"),
38            CheetahString::from_static_str("region"),
39            CheetahString::from_static_str("topic"),
40        ]
41    }
42
43    #[test]
44    fn new_stores_trace_type_and_fields_in_order() {
45        let record = TraceRecord::new(TraceType::SubAfter, fields());
46        assert_eq!(record.trace_type, TraceType::SubAfter);
47        assert_eq!(record.fields, fields());
48    }
49
50    #[test]
51    fn records_built_from_same_inputs_are_equal() {
52        let left = TraceRecord::new(TraceType::Pub, fields());
53        let right = TraceRecord::new(TraceType::Pub, fields());
54        assert_eq!(left, right);
55        assert_eq!(left.clone(), right);
56    }
57
58    #[test]
59    fn records_differ_when_trace_type_or_fields_differ() {
60        let base = TraceRecord::new(TraceType::Pub, fields());
61        assert_ne!(base, TraceRecord::new(TraceType::Recall, fields()));
62        assert_ne!(base, TraceRecord::new(TraceType::Pub, Vec::new()));
63    }
64}