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
//  Copyright 2017 Palantir Technologies, Inc.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.

//! Endpoints.
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

/// The network context of a node in the service graph.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub struct Endpoint {
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
    service_name: Option<String>,
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
    ipv4: Option<Ipv4Addr>,
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
    ipv6: Option<Ipv6Addr>,
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
    port: Option<u16>,
}

impl Endpoint {
    /// Returns a builder type used to construct an `Endpoint`.
    pub fn builder() -> Builder {
        Builder {
            service_name: None,
            ipv4: None,
            ipv6: None,
            port: None,
        }
    }

    /// Returns the name of the service at this endpoint.
    pub fn service_name(&self) -> Option<&str> {
        self.service_name.as_ref().map(|s| &**s)
    }

    /// Returns the IPv4 address of the service at this endpoint.
    pub fn ipv4(&self) -> Option<Ipv4Addr> {
        self.ipv4
    }

    /// Returns the IPv6 address of the service at this endpoint.
    pub fn ipv6(&self) -> Option<Ipv6Addr> {
        self.ipv6
    }

    /// Returns the port of the service at this endpoint.
    pub fn port(&self) -> Option<u16> {
        self.port
    }
}

/// A builder type for `Endpoint`s.
pub struct Builder {
    service_name: Option<String>,
    ipv4: Option<Ipv4Addr>,
    ipv6: Option<Ipv6Addr>,
    port: Option<u16>,
}

impl Builder {
    /// Sets the service name associated with the endpoint.
    ///
    /// Defaults to `None`.
    pub fn service_name(&mut self, service_name: &str) -> &mut Builder {
        self.service_name = Some(service_name.to_string());
        self
    }

    /// Sets the IPv4 address associated with the endpoint.
    ///
    /// Defaults to `None`.
    pub fn ipv4(&mut self, ipv4: Ipv4Addr) -> &mut Builder {
        self.ipv4 = Some(ipv4);
        self
    }

    /// Sets the IPv6 address associated with the endpoint.
    ///
    /// Defaults to `None`.
    pub fn ipv6(&mut self, ipv6: Ipv6Addr) -> &mut Builder {
        self.ipv6 = Some(ipv6);
        self
    }

    /// Sets the IP address associated with the endpoint.
    ///
    /// This is simply a convenience function which delegates to `ipv4` and
    /// `ipv6`.
    pub fn ip(&mut self, ip: IpAddr) -> &mut Builder {
        match ip {
            IpAddr::V4(addr) => self.ipv4(addr),
            IpAddr::V6(addr) => self.ipv6(addr),
        }
    }

    /// Sets the port associated with the endpoint.
    ///
    /// Defaults to `None`.
    pub fn port(&mut self, port: u16) -> &mut Builder {
        self.port = Some(port);
        self
    }

    /// Constructs the `Endpoint`.
    pub fn build(&self) -> Endpoint {
        Endpoint {
            service_name: self.service_name.clone(),
            ipv4: self.ipv4,
            ipv6: self.ipv6,
            port: self.port,
        }
    }
}