Skip to main content

rocketmq_model/common/attribute/
string_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
17use crate::common::attribute::Attribute;
18use crate::common::attribute::AttributeBase;
19
20#[derive(Debug, Clone)]
21pub struct StringAttribute {
22    attribute: AttributeBase,
23}
24
25impl StringAttribute {
26    pub fn new(name: CheetahString, changeable: bool) -> Self {
27        Self {
28            attribute: AttributeBase::new(name, changeable),
29        }
30    }
31}
32
33impl Attribute for StringAttribute {
34    fn verify(&self, _value: &str) -> Result<(), String> {
35        Ok(())
36    }
37
38    fn name(&self) -> &CheetahString {
39        self.attribute.name()
40    }
41
42    fn is_changeable(&self) -> bool {
43        self.attribute.is_changeable()
44    }
45}
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50
51    #[test]
52    fn string_attribute_accepts_empty_and_non_empty_values() {
53        let attribute = StringAttribute::new(CheetahString::from_static_str("lite.bind.topic"), true);
54
55        assert!(attribute.verify("").is_ok());
56        assert!(attribute.verify("parent-topic").is_ok());
57    }
58}