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
//! Functions and types useful for implementing the Service Protocol, as defined
//! in
//! [Section 5](https://melt-umn.github.io/monto-v3-draft/draft03/#5-the-service-protocol)
//! of the specification.
//!
//! This ought to be rewritten to use a trait for ServiceFn.

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;

/// A Service and the associated HTTP server.
pub struct Service {
    config: Config,
    funcs: BTreeMap<ProductDescriptor, Box<ServiceProvider>>,
    handle: Handle,
}

impl Service {
    /// Creates a new Service.
    pub fn new(config: Config, handle: Handle) -> Service {
        let funcs = BTreeMap::new();
        Service {
            config,
            funcs,
            handle,
        }
    }

    /// Creates a ServiceNegotiation.
    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(),
        }
    }

    /// Adds a ServiceProvider to the service.
    ///
    /// Replaces any ServiceProvider that provides the same Product.
    pub fn add_provider<P: ServiceProvider + 'static>(&mut self, provider: P) {
        let descriptor = provider.descriptor();
        self.funcs.insert(descriptor, Box::new(provider));
    }
}

/// A function for a service.
pub trait ServiceProvider {
    /// Returns a ProductDescriptor for the product this provider provides.
    fn descriptor(&self) -> ProductDescriptor;

    /// The function that actually runs the service.
    fn service(
        &mut self,
        path: &str,
        products: Vec<Product>,
    ) -> (Result<Value, Vec<ServiceError>>, Vec<ServiceNotice>);
}