rocketmq_common/common/
attribute.rs

1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17use cheetah_string::CheetahString;
18
19pub mod attribute_parser;
20pub mod attribute_util;
21pub mod bool_attribute;
22pub mod cleanup_policy;
23pub mod cq_type;
24pub mod enum_attribute;
25pub mod long_range_attribute;
26pub mod subscription_group_attributes;
27pub mod topic_attributes;
28pub mod topic_message_type;
29
30/// Trait representing an attribute with name and changeability properties.
31///
32/// This trait is the Rust equivalent of the Java abstract class Attribute.
33/// It defines common functionality for all attribute types.
34pub trait Attribute: Send + Sync {
35    /// Verify that the provided string value is valid for this attribute.
36    /// Implementations should validate according to their specific rules.
37    fn verify(&self, value: &str) -> Result<(), String>;
38
39    /// Get the name of this attribute.
40    fn name(&self) -> &CheetahString;
41
42    /// Check if this attribute can be changed after creation.
43    fn is_changeable(&self) -> bool;
44}
45
46/// Base implementation of the Attribute trait that can be extended by concrete attribute types.
47#[derive(Debug, Clone)]
48pub struct AttributeBase {
49    /// The name of the attribute.
50    name: CheetahString,
51
52    /// Whether the attribute can be changed after creation.
53    changeable: bool,
54}
55
56impl AttributeBase {
57    /// Create a new attribute base with the given name and changeability.
58    pub fn new(name: CheetahString, changeable: bool) -> Self {
59        Self { name, changeable }
60    }
61
62    /// Get the name of this attribute.
63    pub fn name(&self) -> &CheetahString {
64        &self.name
65    }
66
67    /// Set a new name for this attribute.
68    pub fn set_name(&mut self, name: CheetahString) {
69        self.name = name;
70    }
71
72    /// Check if this attribute can be changed after creation.
73    pub fn is_changeable(&self) -> bool {
74        self.changeable
75    }
76
77    /// Set whether this attribute can be changed.
78    pub fn set_changeable(&mut self, changeable: bool) {
79        self.changeable = changeable;
80    }
81}
82
83#[cfg(test)]
84mod tests {
85    use cheetah_string::CheetahString;
86
87    use super::*;
88
89    #[test]
90    fn create_new_attribute_base() {
91        let name = CheetahString::from_static_str("test_attribute");
92        let attribute = AttributeBase::new(name.clone(), true);
93        assert_eq!(attribute.name(), &name);
94        assert!(attribute.is_changeable());
95    }
96
97    #[test]
98    fn set_attribute_name() {
99        let mut attribute = AttributeBase::new(CheetahString::from_static_str("old_name"), true);
100        let new_name = CheetahString::from_static_str("new_name");
101        attribute.set_name(new_name.clone());
102        assert_eq!(attribute.name(), &new_name);
103    }
104
105    #[test]
106    fn set_attribute_changeable() {
107        let mut attribute =
108            AttributeBase::new(CheetahString::from_static_str("test_attribute"), false);
109        attribute.set_changeable(true);
110        assert!(attribute.is_changeable());
111    }
112
113    #[test]
114    fn attribute_not_changeable() {
115        let attribute = AttributeBase::new(CheetahString::from_static_str("test_attribute"), false);
116        assert!(!attribute.is_changeable());
117    }
118}