Trait Nesting

Source
pub trait Nesting: Node {
Show 17 methods // Provided methods fn mount<H>(&mut self, edp: H) where H: ApiHandler + Send + Sync { ... } fn namespace<F>(&mut self, path: &str, builder: F) where F: FnOnce(&mut Namespace) { ... } fn group<F>(&mut self, path: &str, builder: F) where F: FnOnce(&mut Namespace) { ... } fn resource<F>(&mut self, path: &str, builder: F) where F: FnOnce(&mut Namespace) { ... } fn resources<F>(&mut self, path: &str, builder: F) where F: FnOnce(&mut Namespace) { ... } fn segment<F>(&mut self, path: &str, builder: F) where F: FnOnce(&mut Namespace) { ... } fn get<F>(&mut self, path: &str, builder: F) where F: FnOnce(&mut Endpoint) -> EndpointHandlerPresent { ... } fn post<F>(&mut self, path: &str, builder: F) where F: FnOnce(&mut Endpoint) -> EndpointHandlerPresent { ... } fn put<F>(&mut self, path: &str, builder: F) where F: FnOnce(&mut Endpoint) -> EndpointHandlerPresent { ... } fn delete<F>(&mut self, path: &str, builder: F) where F: FnOnce(&mut Endpoint) -> EndpointHandlerPresent { ... } fn options<F>(&mut self, path: &str, builder: F) where F: FnOnce(&mut Endpoint) -> EndpointHandlerPresent { ... } fn head<F>(&mut self, path: &str, builder: F) where F: FnOnce(&mut Endpoint) -> EndpointHandlerPresent { ... } fn before<F>(&mut self, callback: F) where F: for<'a> Fn(&'a mut Client<'_>, &JsonValue) -> HandleSuccessResult + Send + Sync + 'static { ... } fn before_validation<F>(&mut self, callback: F) where F: for<'a> Fn(&'a mut Client<'_>, &JsonValue) -> HandleSuccessResult + Send + Sync + 'static { ... } fn after<F>(&mut self, callback: F) where F: for<'a> Fn(&'a mut Client<'_>, &JsonValue) -> HandleSuccessResult + Send + Sync + 'static { ... } fn after_validation<F>(&mut self, callback: F) where F: for<'a> Fn(&'a mut Client<'_>, &JsonValue) -> HandleSuccessResult + Send + Sync + 'static { ... } fn call_handlers<'a, 'r>( &'a self, rest_path: &str, params: &mut JsonValue, req: &'r mut (dyn Request + 'r), info: &mut CallInfo<'a>, ) -> HandleResult<Response> { ... }
}

Provided Methods§

Source

fn mount<H>(&mut self, edp: H)
where H: ApiHandler + Send + Sync,

Examples found in repository?
examples/simple.rs (line 42)
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
147
148
149
150
fn main() {

    let mut app = rustless::Application::new(rustless::Api::build(|api| {
        api.prefix("api");
        api.version("v1", rustless::Versioning::Path);

        api.mount(swagger::create_api("api-docs"));

        api.error_formatter(|err, _media| {
            match err.downcast::<UnauthorizedError>() {
                Some(_) => {
                    return Some(rustless::Response::from(
                        status::StatusCode::Unauthorized,
                        Box::new("Please provide correct `token` parameter")
                    ))
                },
                None => None
            }
        });

        api.post("greet/:name", |endpoint| {
            endpoint.summary("Sends greeting");
            endpoint.desc("Use this to talk to yourself");
            endpoint.params(|params| {
                params.req_typed("name", json_dsl::string());
                params.req_typed("greeting", json_dsl::string());
            });
            endpoint.handle(|client, params| {
                client.text(
                    format!("{}, {}",
                        params.find("greeting").unwrap().to_string(),
                        params.find("name").unwrap().to_string())
                )
            })
        });

        api.get("echo", |endpoint| {
            endpoint.summary("Sends back what it gets");
            endpoint.desc("Use this to talk to yourself");
            endpoint.handle(|client, params| {
                client.json(params)
            })
        });

        api.namespace("admin", |admin_ns| {

            admin_ns.params(|params| {
                params.req_typed("token", json_dsl::string())
            });

            // Using after_validation callback to check token
            admin_ns.after_validation(|_client, params| {

                match params.find("token") {
                    // We can unwrap() safely because token in validated already
                    Some(token) => if token.as_str().unwrap() == "password1" { return Ok(()) },
                    None => ()
                }

                // Fire error from callback is token is wrong
                return Err(rustless::ErrorResponse{
                    error: Box::new(UnauthorizedError) as Box<Error + Send>,
                    response: None
                })

            });

            // This `/api/admin/server_status` endpoint is secure now
            admin_ns.get("server_status", |endpoint| {
                endpoint.summary("Get server status");
                endpoint.desc("Use this API to receive some useful information about the state of our server");
                endpoint.handle(|client, _params| {
                    {
                        let cookies = client.request.cookies();

                        #[cfg(feature = "ssl")]
                        let signed_cookies = cookies.signed();
                        #[cfg(not(feature = "ssl"))]
                        let signed_cookies = cookies;

                        let user_cookie = Cookie::new("session".to_string(), "verified".to_string());
                        signed_cookies.add(user_cookie);
                    }

                    client.text("Everything is OK".to_string())
                })
            });
        })
    }));

    swagger::enable(&mut app, swagger::Spec {
        info: swagger::Info {
            title: "Example API".to_string(),
            description: Some("Simple API to demonstration".to_string()),
            contact: Some(swagger::Contact {
                name: "Stanislav Panferov".to_string(),
                url: Some("http://panferov.me".to_string()),
                ..std::default::Default::default()
            }),
            license: Some(swagger::License {
                name: "MIT".to_string(),
                url: "http://opensource.org/licenses/MIT".to_string()
            }),
            ..std::default::Default::default()
        },
        ..std::default::Default::default()
    });

    let mut chain = iron::Chain::new(app);
    chain.link(::rustless::batteries::cookie::new("secretsecretsecretsecretsecretsecretsecret".as_bytes()));

    iron::Iron::new(chain).http("0.0.0.0:4000").unwrap();
    println!("On 4000");

}
Source

fn namespace<F>(&mut self, path: &str, builder: F)
where F: FnOnce(&mut Namespace),

Examples found in repository?
examples/simple.rs (lines 80-123)
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
147
148
149
150
fn main() {

    let mut app = rustless::Application::new(rustless::Api::build(|api| {
        api.prefix("api");
        api.version("v1", rustless::Versioning::Path);

        api.mount(swagger::create_api("api-docs"));

        api.error_formatter(|err, _media| {
            match err.downcast::<UnauthorizedError>() {
                Some(_) => {
                    return Some(rustless::Response::from(
                        status::StatusCode::Unauthorized,
                        Box::new("Please provide correct `token` parameter")
                    ))
                },
                None => None
            }
        });

        api.post("greet/:name", |endpoint| {
            endpoint.summary("Sends greeting");
            endpoint.desc("Use this to talk to yourself");
            endpoint.params(|params| {
                params.req_typed("name", json_dsl::string());
                params.req_typed("greeting", json_dsl::string());
            });
            endpoint.handle(|client, params| {
                client.text(
                    format!("{}, {}",
                        params.find("greeting").unwrap().to_string(),
                        params.find("name").unwrap().to_string())
                )
            })
        });

        api.get("echo", |endpoint| {
            endpoint.summary("Sends back what it gets");
            endpoint.desc("Use this to talk to yourself");
            endpoint.handle(|client, params| {
                client.json(params)
            })
        });

        api.namespace("admin", |admin_ns| {

            admin_ns.params(|params| {
                params.req_typed("token", json_dsl::string())
            });

            // Using after_validation callback to check token
            admin_ns.after_validation(|_client, params| {

                match params.find("token") {
                    // We can unwrap() safely because token in validated already
                    Some(token) => if token.as_str().unwrap() == "password1" { return Ok(()) },
                    None => ()
                }

                // Fire error from callback is token is wrong
                return Err(rustless::ErrorResponse{
                    error: Box::new(UnauthorizedError) as Box<Error + Send>,
                    response: None
                })

            });

            // This `/api/admin/server_status` endpoint is secure now
            admin_ns.get("server_status", |endpoint| {
                endpoint.summary("Get server status");
                endpoint.desc("Use this API to receive some useful information about the state of our server");
                endpoint.handle(|client, _params| {
                    {
                        let cookies = client.request.cookies();

                        #[cfg(feature = "ssl")]
                        let signed_cookies = cookies.signed();
                        #[cfg(not(feature = "ssl"))]
                        let signed_cookies = cookies;

                        let user_cookie = Cookie::new("session".to_string(), "verified".to_string());
                        signed_cookies.add(user_cookie);
                    }

                    client.text("Everything is OK".to_string())
                })
            });
        })
    }));

    swagger::enable(&mut app, swagger::Spec {
        info: swagger::Info {
            title: "Example API".to_string(),
            description: Some("Simple API to demonstration".to_string()),
            contact: Some(swagger::Contact {
                name: "Stanislav Panferov".to_string(),
                url: Some("http://panferov.me".to_string()),
                ..std::default::Default::default()
            }),
            license: Some(swagger::License {
                name: "MIT".to_string(),
                url: "http://opensource.org/licenses/MIT".to_string()
            }),
            ..std::default::Default::default()
        },
        ..std::default::Default::default()
    });

    let mut chain = iron::Chain::new(app);
    chain.link(::rustless::batteries::cookie::new("secretsecretsecretsecretsecretsecretsecret".as_bytes()));

    iron::Iron::new(chain).http("0.0.0.0:4000").unwrap();
    println!("On 4000");

}
Source

fn group<F>(&mut self, path: &str, builder: F)
where F: FnOnce(&mut Namespace),

Source

fn resource<F>(&mut self, path: &str, builder: F)
where F: FnOnce(&mut Namespace),

Source

fn resources<F>(&mut self, path: &str, builder: F)
where F: FnOnce(&mut Namespace),

Source

fn segment<F>(&mut self, path: &str, builder: F)
where F: FnOnce(&mut Namespace),

Source

fn get<F>(&mut self, path: &str, builder: F)

Examples found in repository?
examples/simple.rs (lines 72-78)
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
147
148
149
150
fn main() {

    let mut app = rustless::Application::new(rustless::Api::build(|api| {
        api.prefix("api");
        api.version("v1", rustless::Versioning::Path);

        api.mount(swagger::create_api("api-docs"));

        api.error_formatter(|err, _media| {
            match err.downcast::<UnauthorizedError>() {
                Some(_) => {
                    return Some(rustless::Response::from(
                        status::StatusCode::Unauthorized,
                        Box::new("Please provide correct `token` parameter")
                    ))
                },
                None => None
            }
        });

        api.post("greet/:name", |endpoint| {
            endpoint.summary("Sends greeting");
            endpoint.desc("Use this to talk to yourself");
            endpoint.params(|params| {
                params.req_typed("name", json_dsl::string());
                params.req_typed("greeting", json_dsl::string());
            });
            endpoint.handle(|client, params| {
                client.text(
                    format!("{}, {}",
                        params.find("greeting").unwrap().to_string(),
                        params.find("name").unwrap().to_string())
                )
            })
        });

        api.get("echo", |endpoint| {
            endpoint.summary("Sends back what it gets");
            endpoint.desc("Use this to talk to yourself");
            endpoint.handle(|client, params| {
                client.json(params)
            })
        });

        api.namespace("admin", |admin_ns| {

            admin_ns.params(|params| {
                params.req_typed("token", json_dsl::string())
            });

            // Using after_validation callback to check token
            admin_ns.after_validation(|_client, params| {

                match params.find("token") {
                    // We can unwrap() safely because token in validated already
                    Some(token) => if token.as_str().unwrap() == "password1" { return Ok(()) },
                    None => ()
                }

                // Fire error from callback is token is wrong
                return Err(rustless::ErrorResponse{
                    error: Box::new(UnauthorizedError) as Box<Error + Send>,
                    response: None
                })

            });

            // This `/api/admin/server_status` endpoint is secure now
            admin_ns.get("server_status", |endpoint| {
                endpoint.summary("Get server status");
                endpoint.desc("Use this API to receive some useful information about the state of our server");
                endpoint.handle(|client, _params| {
                    {
                        let cookies = client.request.cookies();

                        #[cfg(feature = "ssl")]
                        let signed_cookies = cookies.signed();
                        #[cfg(not(feature = "ssl"))]
                        let signed_cookies = cookies;

                        let user_cookie = Cookie::new("session".to_string(), "verified".to_string());
                        signed_cookies.add(user_cookie);
                    }

                    client.text("Everything is OK".to_string())
                })
            });
        })
    }));

    swagger::enable(&mut app, swagger::Spec {
        info: swagger::Info {
            title: "Example API".to_string(),
            description: Some("Simple API to demonstration".to_string()),
            contact: Some(swagger::Contact {
                name: "Stanislav Panferov".to_string(),
                url: Some("http://panferov.me".to_string()),
                ..std::default::Default::default()
            }),
            license: Some(swagger::License {
                name: "MIT".to_string(),
                url: "http://opensource.org/licenses/MIT".to_string()
            }),
            ..std::default::Default::default()
        },
        ..std::default::Default::default()
    });

    let mut chain = iron::Chain::new(app);
    chain.link(::rustless::batteries::cookie::new("secretsecretsecretsecretsecretsecretsecret".as_bytes()));

    iron::Iron::new(chain).http("0.0.0.0:4000").unwrap();
    println!("On 4000");

}
Source

fn post<F>(&mut self, path: &str, builder: F)

Examples found in repository?
examples/simple.rs (lines 56-70)
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
147
148
149
150
fn main() {

    let mut app = rustless::Application::new(rustless::Api::build(|api| {
        api.prefix("api");
        api.version("v1", rustless::Versioning::Path);

        api.mount(swagger::create_api("api-docs"));

        api.error_formatter(|err, _media| {
            match err.downcast::<UnauthorizedError>() {
                Some(_) => {
                    return Some(rustless::Response::from(
                        status::StatusCode::Unauthorized,
                        Box::new("Please provide correct `token` parameter")
                    ))
                },
                None => None
            }
        });

        api.post("greet/:name", |endpoint| {
            endpoint.summary("Sends greeting");
            endpoint.desc("Use this to talk to yourself");
            endpoint.params(|params| {
                params.req_typed("name", json_dsl::string());
                params.req_typed("greeting", json_dsl::string());
            });
            endpoint.handle(|client, params| {
                client.text(
                    format!("{}, {}",
                        params.find("greeting").unwrap().to_string(),
                        params.find("name").unwrap().to_string())
                )
            })
        });

        api.get("echo", |endpoint| {
            endpoint.summary("Sends back what it gets");
            endpoint.desc("Use this to talk to yourself");
            endpoint.handle(|client, params| {
                client.json(params)
            })
        });

        api.namespace("admin", |admin_ns| {

            admin_ns.params(|params| {
                params.req_typed("token", json_dsl::string())
            });

            // Using after_validation callback to check token
            admin_ns.after_validation(|_client, params| {

                match params.find("token") {
                    // We can unwrap() safely because token in validated already
                    Some(token) => if token.as_str().unwrap() == "password1" { return Ok(()) },
                    None => ()
                }

                // Fire error from callback is token is wrong
                return Err(rustless::ErrorResponse{
                    error: Box::new(UnauthorizedError) as Box<Error + Send>,
                    response: None
                })

            });

            // This `/api/admin/server_status` endpoint is secure now
            admin_ns.get("server_status", |endpoint| {
                endpoint.summary("Get server status");
                endpoint.desc("Use this API to receive some useful information about the state of our server");
                endpoint.handle(|client, _params| {
                    {
                        let cookies = client.request.cookies();

                        #[cfg(feature = "ssl")]
                        let signed_cookies = cookies.signed();
                        #[cfg(not(feature = "ssl"))]
                        let signed_cookies = cookies;

                        let user_cookie = Cookie::new("session".to_string(), "verified".to_string());
                        signed_cookies.add(user_cookie);
                    }

                    client.text("Everything is OK".to_string())
                })
            });
        })
    }));

    swagger::enable(&mut app, swagger::Spec {
        info: swagger::Info {
            title: "Example API".to_string(),
            description: Some("Simple API to demonstration".to_string()),
            contact: Some(swagger::Contact {
                name: "Stanislav Panferov".to_string(),
                url: Some("http://panferov.me".to_string()),
                ..std::default::Default::default()
            }),
            license: Some(swagger::License {
                name: "MIT".to_string(),
                url: "http://opensource.org/licenses/MIT".to_string()
            }),
            ..std::default::Default::default()
        },
        ..std::default::Default::default()
    });

    let mut chain = iron::Chain::new(app);
    chain.link(::rustless::batteries::cookie::new("secretsecretsecretsecretsecretsecretsecret".as_bytes()));

    iron::Iron::new(chain).http("0.0.0.0:4000").unwrap();
    println!("On 4000");

}
Source

fn put<F>(&mut self, path: &str, builder: F)

Source

fn delete<F>(&mut self, path: &str, builder: F)

Source

fn options<F>(&mut self, path: &str, builder: F)

Source

fn head<F>(&mut self, path: &str, builder: F)

Source

fn before<F>(&mut self, callback: F)
where F: for<'a> Fn(&'a mut Client<'_>, &JsonValue) -> HandleSuccessResult + Send + Sync + 'static,

Source

fn before_validation<F>(&mut self, callback: F)
where F: for<'a> Fn(&'a mut Client<'_>, &JsonValue) -> HandleSuccessResult + Send + Sync + 'static,

Source

fn after<F>(&mut self, callback: F)
where F: for<'a> Fn(&'a mut Client<'_>, &JsonValue) -> HandleSuccessResult + Send + Sync + 'static,

Source

fn after_validation<F>(&mut self, callback: F)
where F: for<'a> Fn(&'a mut Client<'_>, &JsonValue) -> HandleSuccessResult + Send + Sync + 'static,

Examples found in repository?
examples/simple.rs (lines 87-101)
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
147
148
149
150
fn main() {

    let mut app = rustless::Application::new(rustless::Api::build(|api| {
        api.prefix("api");
        api.version("v1", rustless::Versioning::Path);

        api.mount(swagger::create_api("api-docs"));

        api.error_formatter(|err, _media| {
            match err.downcast::<UnauthorizedError>() {
                Some(_) => {
                    return Some(rustless::Response::from(
                        status::StatusCode::Unauthorized,
                        Box::new("Please provide correct `token` parameter")
                    ))
                },
                None => None
            }
        });

        api.post("greet/:name", |endpoint| {
            endpoint.summary("Sends greeting");
            endpoint.desc("Use this to talk to yourself");
            endpoint.params(|params| {
                params.req_typed("name", json_dsl::string());
                params.req_typed("greeting", json_dsl::string());
            });
            endpoint.handle(|client, params| {
                client.text(
                    format!("{}, {}",
                        params.find("greeting").unwrap().to_string(),
                        params.find("name").unwrap().to_string())
                )
            })
        });

        api.get("echo", |endpoint| {
            endpoint.summary("Sends back what it gets");
            endpoint.desc("Use this to talk to yourself");
            endpoint.handle(|client, params| {
                client.json(params)
            })
        });

        api.namespace("admin", |admin_ns| {

            admin_ns.params(|params| {
                params.req_typed("token", json_dsl::string())
            });

            // Using after_validation callback to check token
            admin_ns.after_validation(|_client, params| {

                match params.find("token") {
                    // We can unwrap() safely because token in validated already
                    Some(token) => if token.as_str().unwrap() == "password1" { return Ok(()) },
                    None => ()
                }

                // Fire error from callback is token is wrong
                return Err(rustless::ErrorResponse{
                    error: Box::new(UnauthorizedError) as Box<Error + Send>,
                    response: None
                })

            });

            // This `/api/admin/server_status` endpoint is secure now
            admin_ns.get("server_status", |endpoint| {
                endpoint.summary("Get server status");
                endpoint.desc("Use this API to receive some useful information about the state of our server");
                endpoint.handle(|client, _params| {
                    {
                        let cookies = client.request.cookies();

                        #[cfg(feature = "ssl")]
                        let signed_cookies = cookies.signed();
                        #[cfg(not(feature = "ssl"))]
                        let signed_cookies = cookies;

                        let user_cookie = Cookie::new("session".to_string(), "verified".to_string());
                        signed_cookies.add(user_cookie);
                    }

                    client.text("Everything is OK".to_string())
                })
            });
        })
    }));

    swagger::enable(&mut app, swagger::Spec {
        info: swagger::Info {
            title: "Example API".to_string(),
            description: Some("Simple API to demonstration".to_string()),
            contact: Some(swagger::Contact {
                name: "Stanislav Panferov".to_string(),
                url: Some("http://panferov.me".to_string()),
                ..std::default::Default::default()
            }),
            license: Some(swagger::License {
                name: "MIT".to_string(),
                url: "http://opensource.org/licenses/MIT".to_string()
            }),
            ..std::default::Default::default()
        },
        ..std::default::Default::default()
    });

    let mut chain = iron::Chain::new(app);
    chain.link(::rustless::batteries::cookie::new("secretsecretsecretsecretsecretsecretsecret".as_bytes()));

    iron::Iron::new(chain).http("0.0.0.0:4000").unwrap();
    println!("On 4000");

}
Source

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

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§