Trait HumusView

Source
pub trait HumusView<S, F>: Serialize + Sized{
    // Required methods
    fn get_template_name(&self) -> String;
    fn get_status_code(&self, settings: &S) -> StatusCode;

    // Provided methods
    fn get_cookie_header(&self, _settings: &S) -> Option<String> { ... }
    fn update_response(&self, _response: &mut Response, _settings: &S) { ... }
    fn get_api_response(self, _settings: &S) -> Response { ... }
}
Available on crate feature axum-view only.
Expand description

👁 Provides data and logic for the HumusEngine and knows how to put information together.

It is recommended to implement this as an enum carrying additional information.

💡Also have a look at the provided methods, while they may be sane defaults they are probably not always the desired behavior.

Required Methods§

Source

fn get_template_name(&self) -> String

Returns the template name that will be used to select the template file.

If the name is “404” for an html response the template file “404.html” will be used.

Also ends up as the view variable in the template. Example:

fn get_template_name(&self) -> String {
	match self {
		Self::Index{..} => "index",
		Self::Results{..} => "results",
		Self::NotFound => "404",
		Self::InternalError{..} => "500",
	}.to_string()
}
Source

fn get_status_code(&self, settings: &S) -> StatusCode

Returns the reponse code for the view.

The numeric value will be useable as http_status in the template.

Example:

use axum::http::StatusCode;

fn get_status_code(&self, settings: &SomeSettings) -> StatusCode {
	match self {
		Self::NotFound => StatusCode::NOT_FOUND,
		Self::InternalError{..} => StatusCode::INTERNAL_SERVER_ERROR,
		_ => StatusCode::OK,
	}
}

Provided Methods§

Available on crate feature axum-view+cookie only.

If this returns a String it will be used as the cookie header.

Only available when the axum-view+cookie feature is enabled.

See: axum: Constructing a Cookie

⚠️ Warning: When setting cookies in the update_response() or get_api_response() this should return None, otherise you are in undefined behavior territory.

Source

fn update_response(&self, _response: &mut Response, _settings: &S)

Update non-API responses after they have been built.

Useful for setting extra headers. Does noting by default.

Source

fn get_api_response(self, _settings: &S) -> Response

Return an API-Response

By default causes the view to Serialize itself to a json response using serde.

Response code and cookie headers are queried in advance and set on the reulting response if it has a status code of 200. Otherwise it is assumed that the response genrating logic alredy took care of that.

You’ll need the following imports when implementing:

use axum::Json;
use axum::response::IntoResponse;

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§