Struct chilli::Pencil [] [src]

pub struct Pencil {
    pub root_path: String,
    pub name: String,
    pub static_folder: String,
    pub static_url_path: String,
    pub template_folder: String,
    pub config: Config,
    pub extensions: ShareMap,
    pub handlebars_registry: RwLock<Box<Handlebars>>,
    pub url_map: Map,
    pub modules: HashMap<String, Module>,
    // some fields omitted
}

The pencil type. It acts as the central application object. Once it is created it will act as a central registry for the view functions, the URL rules and much more.

Fields

The path where your application locates.

The name of the application. By default it's guessed from the root path.

The folder with static files that should be served at static_url_path. Defaults to the "static" folder in the root path of the application.

The url path for the static files on the web, defaults to be "/static".

The folder that contains the templates that should be used for the application. Defaults to ''templates'' folder in the root path of the application.

The configuration for this application.

For storing arbitrary types as "static" data.

The Handlebars registry used to load templates and register helpers.

The url map for this pencil application.

All the attached modules in a hashmap by name.

Methods

impl Pencil
[src]

[src]

Create a new pencil object. It is passed the root path of your application. The root path is used to resolve resources from inside it, for more information about resource loading, see method open_resource.

Usually you create a pencil object in your main function like this:

use chilli::Pencil;

fn main() {
    let mut app = Pencil::new("/web/myapp");
}

[src]

The debug flag. This field is configured from the config with the DEBUG configuration key. Defaults to False.

[src]

The testing flag. This field is configured from the config with the TESTING configuration key. Defaults to False.

[src]

Set the debug flag. This field is configured from the config with the DEBUG configuration key. Set this to True to enable debugging of the application.

[src]

Set the testing flag. This field is configured from the config with the TESTING configuration key. Set this to True to enable the test mode of the application.

[src]

Set global log level based on the application's debug flag. This is only useful for env_logger crate users. On debug mode, this turns on all debug logging.

[src]

This is used to register a view function for a given URL rule. Basically this example:

This example is not tested
app.route("/home", &[Get], "home", home);
app.route("/user/<user_id:int>", &[Get], "user", user);

A rule that listens for GET will implicitly listen for HEAD.

[src]

This is a shortcut for route, register a view function for a given URL rule with just GET method (implicitly HEAD).

[src]

This is a shortcut for route, register a view function for a given URL rule with just POST method.

[src]

This is a shortcut for route, register a view function for a given URL rule with just DELETE method.

[src]

This is a shortcut for route, register a view function for a given URL rule with just PATCH method.

[src]

This is a shortcut for route, register a view function for a given URL rule with just PUT method.

[src]

Connects a URL rule.

[src]

Register a module on the application.

[src]

Enables static file handling.

[src]

Enables static file handling with caching. (304 Not Modified + Max-Age) The static files are considered "not modified" since the server was started.

[src]

Registers a function to run before each request.

[src]

Registers a function to run after each request. Your function must take a response object and modify it.

[src]

Registers a function to run at the end of each request, regardless of whether there was an error or not.

[src]

Registers a function as one http error handler. Same to httperrorhandler.

[src]

Registers a function as one user error handler. Same to usererrorhandler.

[src]

Registers a function as one http error handler. Example:

use chilli::{Pencil, PencilResult, Response};
use chilli::HTTPError;


fn page_not_found(error: HTTPError) -> PencilResult {
    let mut response = Response::from("The page does not exist");
    response.status_code = 404;
    return Ok(response);
}


fn main() {
    let mut app = Pencil::new("/web/demo");
    app.httperrorhandler(404, page_not_found);
}

[src]

Registers a function as one user error handler. There are two ways to handle user errors currently, you can do it in your own view like this:

use chilli::Request;
use chilli::{PencilResult, Response};


#[derive(Clone, Copy)]
struct MyErr(isize);


fn some_operation() -> Result<isize, MyErr> {
    return Err(MyErr(10));
}


fn my_err_handler(_: MyErr) -> PencilResult {
    Ok(Response::from("My err occurred!"))
}


fn hello(_: &mut Request) -> PencilResult {
    match some_operation() {
        Ok(_) => Ok(Response::from("Hello!")),
        Err(e) => my_err_handler(e),
    }
}

The problem with this is that you have to do it in all of your views, it brings a lot of redundance, so pencil provides another solution, currently I still haven't got any better idea on how to store user error handlers, this feature is really just experimental, if you have any good idea, please wake me up. Here is one simple example:

use std::convert;

use chilli::Request;
use chilli::{Pencil, PencilResult, Response};
use chilli::{PencilError, PenUserError, UserError};


#[derive(Clone, Copy)]
pub struct MyErr(isize);

impl convert::From<MyErr> for PencilError {
    fn from(err: MyErr) -> PencilError {
        let user_error = UserError::new("MyErr");
        return PenUserError(user_error);
    }
}


fn my_err_handler(_: UserError) -> PencilResult {
    Ok(Response::from("My err occurred!"))
}


fn some_operation() -> Result<String, MyErr> {
    return Err(MyErr(10));
}


fn hello(_: &mut Request) -> PencilResult {
    let rv = try!(some_operation());
    return Ok(rv.into());
}


fn main() {
    let mut app = Pencil::new("/web/demo");
    // Use error description as key to store handlers, really ugly...
    app.usererrorhandler("MyErr", my_err_handler);
}

[src]

Load and compile and register a template.

[src]

We use handlebars-rs as template engine. Renders a template from the template folder with the given context. The template name is the name of the template to be rendered. The context is the variables that should be available in the template.

[src]

We use handlebars-rs as template engine. Renders a template from the given template source string with the given context. The source is the sourcecode of the template to be rendered. The context is the variables that should be available in the template.

[src]

The actual application handler.

[src]

Runs the application on a hyper HTTP server.

[src]

Runs the application on a hyper HTTP server.

Trait Implementations

impl Handler for Pencil
[src]

[src]

Receives a Request/Response pair, and should perform some action on them. Read more

[src]

Called when a Request includes a Expect: 100-continue header. Read more

[src]

This is run after a connection is received, on a per-connection basis (not a per-request basis, as a connection with keep-alive may handle multiple requests) Read more

[src]

This is run before a connection is closed, on a per-connection basis (not a per-request basis, as a connection with keep-alive may handle multiple requests) Read more

impl PathBound for Pencil
[src]

Important traits for &'a File
[src]

Opens a resource from the root path folder. Consider the following folder structure: Read more

impl Display for Pencil
[src]

[src]

Formats the value using the given formatter. Read more

impl Debug for Pencil
[src]

[src]

Formats the value using the given formatter. Read more

Auto Trait Implementations

impl Send for Pencil

impl Sync for Pencil