nrpc/
service.rs

1use futures::Stream;
2use core::marker::Unpin;
3
4#[cfg(feature = "client-send")]
5pub type ServiceClientStream<'a, T> = Box<dyn Stream<Item=Result<T, ServiceError>> + Unpin + Send + 'a>;
6
7#[cfg(not(feature = "client-send"))]
8pub type ServiceClientStream<'a, T> = Box<dyn Stream<Item=Result<T, ServiceError>> + Unpin + 'a>;
9
10#[cfg(feature = "server-send")]
11pub type ServiceServerStream<'a, T> = Box<dyn Stream<Item=Result<T, ServiceError>> + Unpin + Send + 'a>;
12
13#[cfg(not(feature = "server-send"))]
14pub type ServiceServerStream<'a, T> = Box<dyn Stream<Item=Result<T, ServiceError>> + Unpin + 'a>;
15
16#[cfg_attr(feature = "server-send", async_trait::async_trait)]
17#[cfg_attr(not(feature = "server-send"), async_trait::async_trait(?Send))]
18pub trait ServerService<'b> {
19    fn descriptor(&self) -> &'static str;
20
21    async fn call<'a: 'b>(
22        &mut self,
23        method: &str,
24        input: ServiceServerStream<'a, bytes::Bytes>,
25    ) -> Result<ServiceServerStream<'a, bytes::Bytes>, ServiceError>;
26}
27
28#[cfg_attr(feature = "client-send", async_trait::async_trait)]
29#[cfg_attr(not(feature = "client-send"), async_trait::async_trait(?Send))]
30pub trait ClientHandler<'b> {
31    async fn call<'a: 'b>(
32        &self,
33        package: &str,
34        service: &str,
35        method: &str,
36        input: ServiceClientStream<'a, bytes::Bytes>,
37    ) -> Result<ServiceClientStream<'a, bytes::Bytes>, ServiceError>;
38}
39
40pub trait ClientService {
41    fn descriptor(&self) -> &'static str;
42}
43
44#[derive(Debug)]
45pub enum ServiceError {
46    Encode(prost::EncodeError),
47    Decode(prost::DecodeError),
48    MethodNotFound,
49    ServiceNotFound,
50    Method(Box<dyn std::error::Error + Send + 'static>),
51    StreamLength {
52        want: u64,
53        got: u64,
54    }
55}
56
57impl std::fmt::Display for ServiceError {
58    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59        match self {
60            Self::Encode(en) => write!(f, "Encode error: {}", en),
61            Self::Decode(de) => write!(f, "Decode error: {}", de),
62            Self::MethodNotFound => write!(f, "Method not found error"),
63            Self::ServiceNotFound => write!(f, "Service not found error"),
64            Self::Method(e) => write!(f, "Method error: {}", e),
65            Self::StreamLength{ want, got } => write!(f, "Stream length error: wanted {}, got {}", want, got),
66        }
67    }
68}
69
70impl std::convert::From<prost::EncodeError> for ServiceError {
71    fn from(value: prost::EncodeError) -> Self {
72        Self::Encode(value)
73    }
74}
75
76impl std::convert::From<prost::DecodeError> for ServiceError {
77    fn from(value: prost::DecodeError) -> Self {
78        Self::Decode(value)
79    }
80}
81
82impl std::convert::From<Box<dyn std::error::Error + Send>> for ServiceError {
83    fn from(value: Box<dyn std::error::Error + Send>) -> Self {
84        Self::Method(value)
85    }
86}
87
88impl std::error::Error for ServiceError {}