tina-core 0.0.2

Tina platform
Documentation
//! gRPC mod

pub mod tonic;

use std::fmt::Debug;

#[cfg(any(feature = "client-tonic", feature = "server-tonic"))]
pub use self::tonic::*;
use self::{request::Request, response::Response};

/// gRPC service trait
#[async_trait]
pub trait ApiGrpcService: Clone + Send + Sync + 'static {
    /// 服务名
    const NAME: &'static str;
    /// 异常
    type Rejection: IntoGrpcResponse + Send + Sync + 'static;
    /// 方法调用
    async fn call(&self, req: Request) -> Result<Response, Self::Rejection>;
}

/// 从 gRPC request转换
#[async_trait]
pub trait FromGrpcRequest: Debug + Send + Sync + 'static {
    /// Request数据
    type Request: Debug + Send + Sync + 'static;
    /// 异常
    type Rejection: Debug + Send + Sync + 'static;
    /// 转换
    async fn from_grpc_request(req: Self::Request) -> Result<Self, Self::Rejection>
    where
        Self: Sized;
}

/// 转换成 gRPC request
#[async_trait]
pub trait IntoGrpcRequest: Debug + Send + Sync + 'static {
    /// Request数据
    type Request: Debug + Send + Sync + 'static;
    /// 转换
    async fn into_grpc_request(self) -> Self::Request;
}

/// 从 gRPC response 转换
#[async_trait]
pub trait FromGrpcResponse: Debug + Send + Sync + 'static {
    /// Response数据
    type Response: Debug + Send + 'static;
    /// 异常
    type Rejection: Debug + Send + Sync + 'static;
    /// 转换
    async fn from_grpc_response(res: Self::Response) -> Result<Self, Self::Rejection>
    where
        Self: Sized;
}

/// 转换成 gRPC response
#[async_trait]
pub trait IntoGrpcResponse: Debug + Send + Sync + 'static {
    /// Response数据
    type Response: Debug + Send + 'static;
    /// 转换
    async fn into_grpc_response(self) -> Self::Response;
}