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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use std::sync::Arc;
use bytes::Bytes;
use dashmap::DashMap;
use futures::future::{ready, BoxFuture};
pub trait Provider<S: Sync + Send>: Sync + Send {
fn call(
&self,
session: &Arc<S>,
service: &str,
method: &str,
data: Bytes,
) -> BoxFuture<'static, Result<Bytes, ProviderError>>;
}
pub trait NamedProvider<S: Sync + Send>: Provider<S> {
const NAME: &'static str;
}
pub type Response<T> = Result<T, ProviderError>;
#[derive(Debug)]
pub enum ProviderError {
ServiceNotFound,
MethodNotFound,
DeserializerError(serde_json::Error),
SerializerError(serde_json::Error),
ValidationError(validator::ValidationErrors),
InternalError(Box<dyn std::error::Error + Sync + Send>),
Shutdown,
}
impl ProviderError {
pub fn to_bytes(&self) -> Bytes {
match self {
Self::ServiceNotFound => "ServiceNotFound",
Self::MethodNotFound => "MethodNotFound",
Self::SerializerError(_) => "InternalError",
Self::DeserializerError(_) => "DeserializerError",
Self::ValidationError(_) => "ValidationError",
Self::InternalError(_) => "InternalError",
Self::Shutdown => "InternalError",
}
.into()
}
}
pub struct Router<S: Sync + Send> {
services: DashMap<String, Box<dyn Provider<S>>>,
}
impl<S: Sync + Send> Router<S> {
pub fn new() -> Self {
Self {
services: DashMap::new(),
}
}
pub fn service<P>(&self, provider: P)
where
P: NamedProvider<S> + 'static,
{
self.services.insert(P::NAME.to_owned(), Box::new(provider));
}
}
impl<S: Sync + Send> Provider<S> for Router<S> {
fn call(
&self,
session: &Arc<S>,
service: &str,
method: &str,
data: Bytes,
) -> BoxFuture<'static, Result<Bytes, ProviderError>> {
match self.services.get(service) {
Some(provider) => provider.call(session, service, method, data),
None => Box::pin(ready(Err(ProviderError::ServiceNotFound))),
}
}
}
impl<S: Sync + Send> Default for Router<S> {
fn default() -> Self {
Self::new()
}
}