zipkin_types/
endpoint.rs

1//  Copyright 2017 Palantir Technologies, Inc.
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
15//! Endpoints.
16use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
17
18/// The network context of a node in the service graph.
19#[derive(Debug, Clone)]
20#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
21#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
22pub struct Endpoint {
23    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
24    service_name: Option<String>,
25    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
26    ipv4: Option<Ipv4Addr>,
27    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
28    ipv6: Option<Ipv6Addr>,
29    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
30    port: Option<u16>,
31}
32
33impl Endpoint {
34    /// Returns a builder type used to construct an `Endpoint`.
35    #[inline]
36    pub fn builder() -> Builder {
37        Builder {
38            service_name: None,
39            ipv4: None,
40            ipv6: None,
41            port: None,
42        }
43    }
44
45    /// Returns the name of the service at this endpoint.
46    #[inline]
47    pub fn service_name(&self) -> Option<&str> {
48        self.service_name.as_deref()
49    }
50
51    /// Returns the IPv4 address of the service at this endpoint.
52    #[inline]
53    pub fn ipv4(&self) -> Option<Ipv4Addr> {
54        self.ipv4
55    }
56
57    /// Returns the IPv6 address of the service at this endpoint.
58    #[inline]
59    pub fn ipv6(&self) -> Option<Ipv6Addr> {
60        self.ipv6
61    }
62
63    /// Returns the port of the service at this endpoint.
64    #[inline]
65    pub fn port(&self) -> Option<u16> {
66        self.port
67    }
68}
69
70/// A builder type for `Endpoint`s.
71pub struct Builder {
72    service_name: Option<String>,
73    ipv4: Option<Ipv4Addr>,
74    ipv6: Option<Ipv6Addr>,
75    port: Option<u16>,
76}
77
78impl From<Endpoint> for Builder {
79    #[inline]
80    fn from(e: Endpoint) -> Builder {
81        Builder {
82            service_name: e.service_name,
83            ipv4: e.ipv4,
84            ipv6: e.ipv6,
85            port: e.port,
86        }
87    }
88}
89
90impl Builder {
91    /// Sets the service name associated with the endpoint.
92    ///
93    /// Defaults to `None`.
94    #[inline]
95    pub fn service_name(&mut self, service_name: &str) -> &mut Builder {
96        self.service_name = Some(service_name.to_string());
97        self
98    }
99
100    /// Sets the IPv4 address associated with the endpoint.
101    ///
102    /// Defaults to `None`.
103    #[inline]
104    pub fn ipv4(&mut self, ipv4: Ipv4Addr) -> &mut Builder {
105        self.ipv4 = Some(ipv4);
106        self
107    }
108
109    /// Sets the IPv6 address associated with the endpoint.
110    ///
111    /// Defaults to `None`.
112    #[inline]
113    pub fn ipv6(&mut self, ipv6: Ipv6Addr) -> &mut Builder {
114        self.ipv6 = Some(ipv6);
115        self
116    }
117
118    /// Sets the IP address associated with the endpoint.
119    ///
120    /// This is simply a convenience function which delegates to `ipv4` and
121    /// `ipv6`.
122    #[inline]
123    pub fn ip(&mut self, ip: IpAddr) -> &mut Builder {
124        match ip {
125            IpAddr::V4(addr) => self.ipv4(addr),
126            IpAddr::V6(addr) => self.ipv6(addr),
127        }
128    }
129
130    /// Sets the port associated with the endpoint.
131    ///
132    /// Defaults to `None`.
133    #[inline]
134    pub fn port(&mut self, port: u16) -> &mut Builder {
135        self.port = Some(port);
136        self
137    }
138
139    /// Constructs the `Endpoint`.
140    #[inline]
141    pub fn build(&self) -> Endpoint {
142        Endpoint {
143            service_name: self.service_name.clone(),
144            ipv4: self.ipv4,
145            ipv6: self.ipv6,
146            port: self.port,
147        }
148    }
149}