1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright Materialize, Inc. All rights reserved.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0.

use std::error::Error;
use std::fmt::{self, Write};
use std::num::ParseIntError;
use std::str::FromStr;

use serde::{Deserialize, Serialize};

/// Represents the addresses of several Kafka brokers.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct KafkaAddrs(Vec<(String, u16)>);

impl FromStr for KafkaAddrs {
    type Err = KafkaAddrsParseError;

    fn from_str(s: &str) -> Result<KafkaAddrs, Self::Err> {
        let mut addrs = vec![];
        for s in s.split(',') {
            let mut parts = s.splitn(2, ':');
            let host = parts.next().expect("splitn returns at least one part");
            let port = match parts.next() {
                None => 9092,
                Some(port) => port.parse().map_err(KafkaAddrsParseError::InvalidPort)?,
            };
            addrs.push((host.to_owned(), port));
        }
        Ok(KafkaAddrs(addrs))
    }
}

impl fmt::Display for KafkaAddrs {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        for (i, (host, port)) in self.0.iter().enumerate() {
            if i > 0 {
                f.write_char(',')?;
            }
            write!(f, "{}:{}", host, port)?;
        }
        Ok(())
    }
}

/// An error while parsing a Kafka address.
#[derive(Clone, Debug)]
pub enum KafkaAddrsParseError {
    /// The Kafka address contained an invalid port.
    InvalidPort(ParseIntError),
}

impl fmt::Display for KafkaAddrsParseError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            KafkaAddrsParseError::InvalidPort(e) => write!(
                f,
                "unable to parse Kafka broker address: invalid port: {}",
                e
            ),
        }
    }
}

impl Error for KafkaAddrsParseError {}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_ok() -> Result<(), Box<dyn Error>> {
        struct TestCase {
            input: &'static str,
            addrs: Vec<(String, u16)>,
            output: &'static str,
        }

        let test_cases = vec![
            TestCase {
                input: "localhost",
                addrs: vec![("localhost".into(), 9092)],
                output: "localhost:9092",
            },
            TestCase {
                input: "foohost",
                addrs: vec![("foohost".into(), 9092)],
                output: "foohost:9092",
            },
            TestCase {
                input: "1.2.3.4",
                addrs: vec![("1.2.3.4".into(), 9092)],
                output: "1.2.3.4:9092",
            },
            TestCase {
                input: "localhost:42",
                addrs: vec![("localhost".into(), 42)],
                output: "localhost:42",
            },
            TestCase {
                input: "1.2.3.4:1234",
                addrs: vec![("1.2.3.4".into(), 1234)],
                output: "1.2.3.4:1234",
            },
            TestCase {
                input: "host1,host2",
                addrs: vec![("host1".into(), 9092), ("host2".into(), 9092)],
                output: "host1:9092,host2:9092",
            },
            TestCase {
                input: "host1:42,host2:42",
                addrs: vec![("host1".into(), 42), ("host2".into(), 42)],
                output: "host1:42,host2:42",
            },
            TestCase {
                input: "host1,host2:42",
                addrs: vec![("host1".into(), 9092), ("host2".into(), 42)],
                output: "host1:9092,host2:42",
            },
        ];

        for tc in test_cases {
            let addrs: KafkaAddrs = tc.input.parse()?;
            assert_eq!(addrs.0, tc.addrs);
            assert_eq!(addrs.to_string(), tc.output);
        }

        Ok(())
    }

    #[test]
    fn test_parse_err() {
        assert_eq!(
            "host:badport"
                .parse::<KafkaAddrs>()
                .unwrap_err()
                .to_string(),
            "unable to parse Kafka broker address: invalid port: invalid digit found in string",
        )
    }
}