rocketmq_common/common/
attribute.rs1use 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
30pub trait Attribute: Send + Sync {
35 fn verify(&self, value: &str) -> Result<(), String>;
38
39 fn name(&self) -> &CheetahString;
41
42 fn is_changeable(&self) -> bool;
44}
45
46#[derive(Debug, Clone)]
48pub struct AttributeBase {
49 name: CheetahString,
51
52 changeable: bool,
54}
55
56impl AttributeBase {
57 pub fn new(name: CheetahString, changeable: bool) -> Self {
59 Self { name, changeable }
60 }
61
62 pub fn name(&self) -> &CheetahString {
64 &self.name
65 }
66
67 pub fn set_name(&mut self, name: CheetahString) {
69 self.name = name;
70 }
71
72 pub fn is_changeable(&self) -> bool {
74 self.changeable
75 }
76
77 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}