Skip to main content

rocketmq_model/common/
attribute.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 cheetah_string::CheetahString;
16
17pub mod attribute_parser;
18pub mod attribute_util;
19pub mod bool_attribute;
20pub mod cleanup_policy;
21pub mod cq_type;
22pub mod enum_attribute;
23pub mod long_range_attribute;
24pub mod string_attribute;
25pub mod subscription_group_attributes;
26pub mod topic_attributes;
27pub mod topic_message_type;
28
29/// Trait representing an attribute with name and changeability properties.
30///
31/// This trait is the Rust equivalent of the Java abstract class Attribute.
32/// It defines common functionality for all attribute types.
33pub trait Attribute: Send + Sync {
34    /// Verify that the provided string value is valid for this attribute.
35    /// Implementations should validate according to their specific rules.
36    fn verify(&self, value: &str) -> Result<(), String>;
37
38    /// Get the name of this attribute.
39    fn name(&self) -> &CheetahString;
40
41    /// Check if this attribute can be changed after creation.
42    fn is_changeable(&self) -> bool;
43}
44
45/// Base implementation of the Attribute trait that can be extended by concrete attribute types.
46#[derive(Debug, Clone)]
47pub struct AttributeBase {
48    /// The name of the attribute.
49    name: CheetahString,
50
51    /// Whether the attribute can be changed after creation.
52    changeable: bool,
53}
54
55impl AttributeBase {
56    /// Create a new attribute base with the given name and changeability.
57    pub fn new(name: CheetahString, changeable: bool) -> Self {
58        Self { name, changeable }
59    }
60
61    /// Get the name of this attribute.
62    pub fn name(&self) -> &CheetahString {
63        &self.name
64    }
65
66    /// Set a new name for this attribute.
67    pub fn set_name(&mut self, name: CheetahString) {
68        self.name = name;
69    }
70
71    /// Check if this attribute can be changed after creation.
72    pub fn is_changeable(&self) -> bool {
73        self.changeable
74    }
75
76    /// Set whether this attribute can be changed.
77    pub fn set_changeable(&mut self, changeable: bool) {
78        self.changeable = changeable;
79    }
80}
81
82#[cfg(test)]
83mod tests {
84    use cheetah_string::CheetahString;
85
86    use super::*;
87
88    #[test]
89    fn create_new_attribute_base() {
90        let name = CheetahString::from_static_str("test_attribute");
91        let attribute = AttributeBase::new(name.clone(), true);
92        assert_eq!(attribute.name(), &name);
93        assert!(attribute.is_changeable());
94    }
95
96    #[test]
97    fn set_attribute_name() {
98        let mut attribute = AttributeBase::new(CheetahString::from_static_str("old_name"), true);
99        let new_name = CheetahString::from_static_str("new_name");
100        attribute.set_name(new_name.clone());
101        assert_eq!(attribute.name(), &new_name);
102    }
103
104    #[test]
105    fn set_attribute_changeable() {
106        let mut attribute = AttributeBase::new(CheetahString::from_static_str("test_attribute"), false);
107        attribute.set_changeable(true);
108        assert!(attribute.is_changeable());
109    }
110
111    #[test]
112    fn attribute_not_changeable() {
113        let attribute = AttributeBase::new(CheetahString::from_static_str("test_attribute"), false);
114        assert!(!attribute.is_changeable());
115    }
116}