rs-zero 0.1.1

Rust-first microservice framework inspired by go-zero engineering practices
Documentation
use tonic::transport::{Channel, Endpoint};

use crate::rpc::RpcClientConfig;

/// Builds a tonic endpoint from rs-zero client config.
pub fn endpoint_from_config(config: &RpcClientConfig) -> Result<Endpoint, tonic::transport::Error> {
    Endpoint::from_shared(config.endpoint.clone()).map(|endpoint| {
        endpoint
            .connect_timeout(config.connect_timeout)
            .timeout(config.request_timeout)
    })
}

/// Connects a tonic channel from rs-zero client config.
pub async fn connect_channel(config: &RpcClientConfig) -> Result<Channel, tonic::transport::Error> {
    endpoint_from_config(config)?.connect().await
}

#[cfg(test)]
mod tests {
    use super::endpoint_from_config;
    use crate::rpc::RpcClientConfig;

    #[test]
    fn endpoint_accepts_http_uri() {
        let config = RpcClientConfig::new("http://127.0.0.1:50051");

        assert!(endpoint_from_config(&config).is_ok());
    }
}