Skip to main content

rocketmq_client_rust/common/
nameserver_access_config.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
17#[derive(Debug, Clone, PartialEq, Eq, Default)]
18pub struct NameserverAccessConfig {
19    namesrv_addr: CheetahString,
20    namesrv_domain: CheetahString,
21    namesrv_domain_subgroup: CheetahString,
22}
23
24impl NameserverAccessConfig {
25    pub fn new(
26        namesrv_addr: impl Into<CheetahString>,
27        namesrv_domain: impl Into<CheetahString>,
28        namesrv_domain_subgroup: impl Into<CheetahString>,
29    ) -> Self {
30        Self {
31            namesrv_addr: namesrv_addr.into(),
32            namesrv_domain: namesrv_domain.into(),
33            namesrv_domain_subgroup: namesrv_domain_subgroup.into(),
34        }
35    }
36
37    #[inline]
38    pub fn namesrv_addr(&self) -> &CheetahString {
39        &self.namesrv_addr
40    }
41
42    #[inline]
43    pub fn namesrv_domain(&self) -> &CheetahString {
44        &self.namesrv_domain
45    }
46
47    #[inline]
48    pub fn namesrv_domain_subgroup(&self) -> &CheetahString {
49        &self.namesrv_domain_subgroup
50    }
51
52    #[inline]
53    pub fn has_namesrv_domain(&self) -> bool {
54        !self.namesrv_domain.is_empty()
55    }
56}
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    #[test]
63    fn nameserver_access_config_matches_java_fields() {
64        let config = NameserverAccessConfig::new("127.0.0.1:9876", "domain", "subgroup");
65
66        assert_eq!(config.namesrv_addr(), "127.0.0.1:9876");
67        assert_eq!(config.namesrv_domain(), "domain");
68        assert_eq!(config.namesrv_domain_subgroup(), "subgroup");
69    }
70}