use std::sync::Arc;
use prelude::*;
#[derive(Clone)]
pub struct Namespace {
name: &'static [&'static str],
apis: Vec<Arc<Api>>,
}
impl Namespace {
pub fn new(name: &'static [&'static str]) -> Namespace {
Namespace {
name: name,
apis: Vec::new(),
}
}
pub fn with_api<A: Api>(mut self, api: A) -> Namespace {
self.apis.push(Arc::new(api) as Arc<Api>);
self
}
pub fn bind<A: Api>(&mut self, api: A) {
self.apis.push(Arc::new(api) as Arc<Api>)
}
}
impl Api for Namespace {
fn name(&self) -> &[&str] {
self.name
}
fn route(&self, req: &mut Request) -> ApiResult {
for api in self.apis.iter() {
if req.match_segs(api.name()) {
return api.route(req)
}
}
gen_api_not_found()
}
}
fn gen_api_not_found() -> ApiResult {
let err = Error::not_found("Unable to find the requested API.");
Err(err)
}