Skip to main content

HumusView

Trait HumusView 

Source
pub trait HumusView<S, ApiFormat>: Serialize + Sized
where S: HumusQuerySettings, ApiFormat: HumusApiFormat,
{ // Required methods fn get_template_name(&self) -> String; fn get_status_code(&self, settings: &S) -> StatusCode; // Provided methods fn initalize_template_context(&self, _context: &mut Context, _settings: &S) { ... } fn update_response(&self, response: Response, _settings: &S) -> Response { ... } fn into_api_response(self, settings: &S, _api_format: ApiFormat) -> Response { ... } }
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§

Source

fn initalize_template_context(&self, _context: &mut Context, _settings: &S)

Hook called before rendering a template to initalize it with additional values.

📖 Remember to document which values you set here in a place someone who wants to do something with templating is able to find it.

The default implementation does nothing.

Source

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

Hook to update non-API responses after they have been built.

Note: The default implementation of Self::into_api_response does call this method, when implementing a custom version of it it is your decision wheter to call it or not.

When migrating from earlier versions set your cookie headers in here. See also axum: Constructing a Cookie

Useful for setting extra headers. Does nothing by default.

Source

fn into_api_response(self, settings: &S, _api_format: ApiFormat) -> 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 generating logic alredy took care of that.

You’ll need the following imports when implementing:

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

The api format in the seconds argument is guaranteed to be derived from the settings given in the first argument.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§