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
pub mod config;
pub mod helpers;
pub mod messages;
mod serve;
use std::collections::BTreeMap;
use serde_json::Value;
use tokio_core::reactor::Handle;
use common::messages::{Product, ProductDescriptor, ProtocolVersion};
use self::config::Config;
use self::messages::{ServiceError, ServiceNegotiation, ServiceNotice};
pub use self::serve::ServeFuture;
pub struct Service {
config: Config,
funcs: BTreeMap<ProductDescriptor, Box<ServiceProvider>>,
handle: Handle,
}
impl Service {
pub fn new(config: Config, handle: Handle) -> Service {
let funcs = BTreeMap::new();
Service {
config,
funcs,
handle,
}
}
pub fn negotiation(&self) -> ServiceNegotiation {
ServiceNegotiation {
extensions: self.config.extensions.clone(),
monto: ProtocolVersion {
major: 3,
minor: 0,
patch: 0,
},
products: self.funcs.keys().cloned().collect(),
service: self.config.version.clone().into(),
}
}
pub fn add_provider<P: ServiceProvider + 'static>(&mut self, provider: P) {
let descriptor = provider.descriptor();
self.funcs.insert(descriptor, Box::new(provider));
}
}
pub trait ServiceProvider {
fn descriptor(&self) -> ProductDescriptor;
fn service(
&mut self,
path: &str,
products: Vec<Product>,
) -> (Result<Value, Vec<ServiceError>>, Vec<ServiceNotice>);
}