vector/components/validation/util.rs
1use std::{fmt, net::SocketAddr};
2
3use http::Uri;
4
5/// A gRPC address.
6///
7/// This wrapper provides the ability to use a simple `SocketAddr` and generate the appropriate
8/// output form of the address depending on whether or not the address is being used by a gRPC
9/// client or gRPC server.
10#[derive(Clone, Debug)]
11pub struct GrpcAddress {
12 addr: SocketAddr,
13}
14
15impl GrpcAddress {
16 /// Gets the socket address.
17 ///
18 /// This is typically used when actually binding a socket to use for listening for connections
19 /// as a gRPC server.
20 pub const fn as_socket_addr(&self) -> SocketAddr {
21 self.addr
22 }
23
24 /// Gets the fully-qualified endpoint address.
25 ///
26 /// This is a URI in the form of `http://<socket address>/`. The scheme and path are hard-coded.
27 pub fn as_uri(&self) -> Uri {
28 let addr_str = self.addr.to_string();
29 Uri::builder()
30 .scheme("http")
31 .authority(addr_str)
32 .path_and_query("/")
33 .build()
34 .expect("should not fail to build URI")
35 }
36}
37
38impl From<SocketAddr> for GrpcAddress {
39 fn from(addr: SocketAddr) -> Self {
40 Self { addr }
41 }
42}
43
44impl fmt::Display for GrpcAddress {
45 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46 self.addr.fmt(f)
47 }
48}