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
use crate::client::Client;
use crate::client::ClientBuilder;
use crate::ClientConf;
use crate::Result as grpc_Result;
use std::sync::Arc;

/// Trait implemented by `XxxClient` structs for `Xxx` trait.
pub trait ClientStub: Sized {
    /// Create a client stub using given `Client` object.
    fn with_client(grpc_client: Arc<Client>) -> Self;
}

/// Utilities to work with generated code clients.
pub trait ClientStubExt: Sized {
    /// Create a plain (non-encrypted) client connected to specified host and port
    fn new_plain(host: &str, port: u16, conf: ClientConf) -> crate::Result<Self>;

    /// Create a client connected to specified host and port using TLS.
    fn new_tls<C: ::tls_api::TlsConnector>(
        host: &str,
        port: u16,
        conf: ClientConf,
    ) -> grpc_Result<Self>;

    /// Create a client connected to specified unix socket.
    fn new_plain_unix(addr: &str, conf: ClientConf) -> grpc_Result<Self>;
}

impl<C: ClientStub> ClientStubExt for C {
    fn new_plain(host: &str, port: u16, conf: ClientConf) -> grpc_Result<Self> {
        ClientBuilder::new(host, port)
            .conf(conf)
            .build()
            .map(|c| Self::with_client(Arc::new(c)))
    }

    fn new_tls<T: ::tls_api::TlsConnector>(
        host: &str,
        port: u16,
        conf: ClientConf,
    ) -> grpc_Result<Self> {
        ClientBuilder::new(host, port)
            .tls::<T>()
            .conf(conf)
            .build()
            .map(|c| Self::with_client(Arc::new(c)))
    }

    #[cfg(unix)]
    fn new_plain_unix(addr: &str, conf: ClientConf) -> grpc_Result<Self> {
        ClientBuilder::new_unix(addr)
            .conf(conf)
            .build()
            .map(|c| Self::with_client(Arc::new(c)))
    }

    #[cfg(not(unix))]
    fn new_plain_unix(addr: &str, conf: ClientConf) -> grpc_Result<Self> {
        Err(::Error::Other("new_plain_unix unsupported"))
    }
}