rocketmq_model/common/
attribute.rs1use 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
29pub trait Attribute: Send + Sync {
34 fn verify(&self, value: &str) -> Result<(), String>;
37
38 fn name(&self) -> &CheetahString;
40
41 fn is_changeable(&self) -> bool;
43}
44
45#[derive(Debug, Clone)]
47pub struct AttributeBase {
48 name: CheetahString,
50
51 changeable: bool,
53}
54
55impl AttributeBase {
56 pub fn new(name: CheetahString, changeable: bool) -> Self {
58 Self { name, changeable }
59 }
60
61 pub fn name(&self) -> &CheetahString {
63 &self.name
64 }
65
66 pub fn set_name(&mut self, name: CheetahString) {
68 self.name = name;
69 }
70
71 pub fn is_changeable(&self) -> bool {
73 self.changeable
74 }
75
76 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}