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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use framework;
use framework::namespace;
use framework::endpoint;
use framework::client;
use json::{JsonValue};
use backend;
use server::method;
use errors;

pub trait Node {
    fn get_handlers<'a>(&'a self) -> &'a framework::ApiHandlers;
    fn get_handlers_mut<'a>(&'a mut self) -> &'a mut framework::ApiHandlers;

    fn get_before<'a>(&'a self) -> &'a framework::Callbacks;
    fn get_before_mut<'a>(&'a mut self) -> &'a mut framework::Callbacks;

    fn get_before_validation<'a>(&'a self) -> &'a framework::Callbacks;
    fn get_before_validation_mut<'a>(&'a mut self) -> &'a mut framework::Callbacks;

    fn get_after_validation<'a>(&'a self) -> &'a framework::Callbacks;
    fn get_after_validation_mut<'a>(&'a mut self) -> &'a mut framework::Callbacks;

    fn get_after<'a>(&'a self) -> &'a framework::Callbacks;
    fn get_after_mut<'a>(&'a mut self) -> &'a mut framework::Callbacks;

    fn push_node<'a>(&'a self, _info: &mut framework::CallInfo<'a>);
}

#[macro_export]
macro_rules! impl_nesting {
    ($t:ident) => (
        impl nesting::Node for $t {
            fn get_handlers<'a>(&'a self) -> &'a ::framework::ApiHandlers { &self.handlers }
            fn get_handlers_mut<'a>(&'a mut self) -> &'a mut ::framework::ApiHandlers { &mut self.handlers }

            fn get_before<'a>(&'a self) -> &'a ::framework::Callbacks { &self.before }
            fn get_before_mut<'a>(&'a mut self) -> &'a mut ::framework::Callbacks { &mut self.before }

            fn get_before_validation<'a>(&'a self) -> &'a ::framework::Callbacks { &self.before_validation }
            fn get_before_validation_mut<'a>(&'a mut self) -> &'a mut ::framework::Callbacks { &mut self.before_validation }

            fn get_after_validation<'a>(&'a self) -> &'a ::framework::Callbacks { &self.after_validation }
            fn get_after_validation_mut<'a>(&'a mut self) -> &'a mut ::framework::Callbacks { &mut self.after_validation }

            fn get_after<'a>(&'a self) -> &'a ::framework::Callbacks { &self.after }
            fn get_after_mut<'a>(&'a mut self) -> &'a mut ::framework::Callbacks { &mut self.after }

            fn push_node<'a>(&'a self, _info: &mut ::framework::CallInfo<'a>) {
                _info.parents.push(self);
            }
        }

        impl ::framework::nesting::Nesting for $t {}
    )
}

pub trait Nesting: Node {

    fn mount<H>(&mut self, edp: H) where H: framework::ApiHandler + Send+Sync {
        self.get_handlers_mut().push(Box::new(edp))
    }

    /*
     * namespace::Namespace aliases
     */

    fn namespace<F>(&mut self, path: &str, builder: F) where F: FnOnce(&mut namespace::Namespace) {
        self.mount(namespace::Namespace::build(path, builder));
    }
    fn group<F>(&mut self, path: &str, builder: F) where F: FnOnce(&mut namespace::Namespace) {
        self.mount(namespace::Namespace::build(path, builder));
    }
    fn resource<F>(&mut self, path: &str, builder: F) where F: FnOnce(&mut namespace::Namespace) {
        self.mount(namespace::Namespace::build(path, builder));
    }
    fn resources<F>(&mut self, path: &str, builder: F) where F: FnOnce(&mut namespace::Namespace) {
        self.mount(namespace::Namespace::build(path, builder));
    }
    fn segment<F>(&mut self, path: &str, builder: F) where F: FnOnce(&mut namespace::Namespace) {
        self.mount(namespace::Namespace::build(path, builder));
    }

    /*
     * endpoint::Endpoints
     */

    fn get<F>(&mut self, path: &str, builder: F) where F: FnOnce(&mut endpoint::Endpoint)
    -> endpoint::EndpointHandlerPresent {
        self.mount(endpoint::Endpoint::build(method::Method::Get, path, builder));
    }
    fn post<F>(&mut self, path: &str, builder: F) where F: FnOnce(&mut endpoint::Endpoint)
    -> endpoint::EndpointHandlerPresent {
        self.mount(endpoint::Endpoint::build(method::Method::Post, path, builder));
    }
    fn put<F>(&mut self, path: &str, builder: F) where F: FnOnce(&mut endpoint::Endpoint)
    -> endpoint::EndpointHandlerPresent {
        self.mount(endpoint::Endpoint::build(method::Method::Put, path, builder));
    }
    fn delete<F>(&mut self, path: &str, builder: F) where F: FnOnce(&mut endpoint::Endpoint)
    -> endpoint::EndpointHandlerPresent {
        self.mount(endpoint::Endpoint::build(method::Method::Delete, path, builder));
    }
    fn options<F>(&mut self, path: &str, builder: F) where F: FnOnce(&mut endpoint::Endpoint)
    -> endpoint::EndpointHandlerPresent {
        self.mount(endpoint::Endpoint::build(method::Method::Options, path, builder));
    }
    fn head<F>(&mut self, path: &str, builder: F) where F: FnOnce(&mut endpoint::Endpoint)
    -> endpoint::EndpointHandlerPresent {
        self.mount(endpoint::Endpoint::build(method::Method::Head, path, builder));
    }

    fn before<F: 'static>(&mut self, callback: F) where F: for<'a> Fn(&'a mut client::Client, &JsonValue)
    -> backend::HandleSuccessResult + Send+Sync {
        self.get_before_mut().push(Box::new(callback));
    }
    fn before_validation<F: 'static>(&mut self, callback: F) where F: for<'a> Fn(&'a mut client::Client, &JsonValue)
    -> backend::HandleSuccessResult + Send+Sync {
        self.get_before_validation_mut().push(Box::new(callback));
    }
    fn after<F: 'static>(&mut self, callback: F) where F: for<'a> Fn(&'a mut client::Client, &JsonValue)
    -> backend::HandleSuccessResult + Send+Sync {
        self.get_after_mut().push(Box::new(callback));
    }
    fn after_validation<F: 'static>(&mut self, callback: F) where F: for<'a> Fn(&'a mut client::Client, &JsonValue)
    -> backend::HandleSuccessResult + Send+Sync {
        self.get_after_validation_mut().push(Box::new(callback));
    }

    fn call_handlers<'a, 'r>(&'a self, rest_path: &str, params: &mut JsonValue, req: &'r mut (backend::Request + 'r),
                         info: &mut framework::CallInfo<'a>) -> backend::HandleResult<backend::Response> {

        for handler in self.get_handlers().iter() {
            match handler.api_call(rest_path, params, req, info) {
                Ok(response) => return Ok(response),
                Err(error_response) => {
                    if !errors::Error::is::<errors::NotMatch>(&*error_response.error) {
                        return Err(error_response)
                    }
                }
            };
        }

        Err(error_response!(errors::NotMatch))
    }

}