rocketmq_remoting/common/
remoting_helper.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 */
17
18pub struct RemotingHelper;
19
20impl RemotingHelper {
21    pub fn parse_host_from_address(address: Option<&str>) -> String {
22        match address {
23            Some(addr) if !addr.is_empty() => {
24                let splits: Vec<&str> = addr.split(':').collect();
25                if !splits.is_empty() {
26                    splits[0].to_string()
27                } else {
28                    String::new()
29                }
30            }
31            _ => String::new(),
32        }
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39
40    #[test]
41    fn parse_host_from_valid_address() {
42        let result = RemotingHelper::parse_host_from_address(Some("127.0.0.1:8080"));
43        assert_eq!(result, "127.0.0.1");
44    }
45
46    #[test]
47    fn parse_host_from_address_without_port() {
48        let result = RemotingHelper::parse_host_from_address(Some("localhost"));
49        assert_eq!(result, "localhost");
50    }
51
52    #[test]
53    fn parse_host_from_empty_address() {
54        let result = RemotingHelper::parse_host_from_address(Some(""));
55        assert_eq!(result, "");
56    }
57
58    #[test]
59    fn parse_host_from_none_address() {
60        let result = RemotingHelper::parse_host_from_address(None);
61        assert_eq!(result, "");
62    }
63}