Struct lib_humus::HumusEngine

source ยท
pub struct HumusEngine<V, S, F>
where V: HumusView<S, F>, S: HumusQuerySettings<F>, F: HumusFormat,
{ pub tera: Tera, pub template_config: Option<Table>, /* private fields */ }
Expand description

๐ŸŒ„ Tera based Templating Engine that writes out axum Response structs.

The engine uses logic and data from the configured View (V) type as well as data and configuration provided by the QuerySettings (S) type to produce the desired response with minimal code overhead inside your main logic.

Note: The private fields are PhantomData because the V, S and N generics are only used for the implementation.

The Engine operates in one of two modes determined by the Format (F) which is provided by the QuerySettings:

ยงTemplate Mode

In template mode the following sequence of events happens:

  • status code, format and cookies are fetched. (The cookie functionality is enabled with the axum-view+cookie feature)
  • Template name and MimeType are fetched.
  • The Template context is populated with metadatam the View and template_config.
  • The QuerySettings are given the chance to populate the context with their own values using the initalize_template_context() hook.
  • The template gets rendered, resulting in further processing or an error response.
  • The response is constructed using the MimeType from earlier and the text from the template.
  • The View is given a chance to alter the Response using the update_response() hook.
  • If the status code of the Response is โ€œ200 OKโ€ (the default) the status code and cookies fetched earlier are applied.

ยงTemplate Symbols

The symbols defined for inside templates are:

  • view will contain the template name, that coincides with the basename of the template file.
  • format will contain the format name (i.e. html, text, json)
  • mime_type will contain the serialized mime_type (i.e. text/plain; charset=utf-8 or application/json)
  • http_status will contain the numeric HTTP status code.
  • data will contain the serde serialized View
  • extra will be set to template_config which usually comes from an extra.toml file in the template directory or a configured custom location.
  • others that were added by the QuerySettings in initalize_template_context()

ยงAPI Mode

In API mode the following, simpler sequence of events happens:

  • status code, format and cookies are fetched. (The cookie functionality is enabled with the axum-view+cookie feature)
  • The View serializes itself using the get_api_response() callback.
  • If the status code of the Response is โ€œ200 OKโ€ (the default) the status code and cookies fetched earlier are applied.

Fieldsยง

ยงtera: Tera

An instance of the tera templating engine.

ยงtemplate_config: Option<Table>

If it was possible to read any extra configuration it will be stored here.

Implementationsยง

sourceยง

impl<V, S, F> HumusEngine<V, S, F>
where V: HumusView<S, F>, S: HumusQuerySettings<F>, F: HumusFormat,

source

pub fn new(tera: Tera, template_config: Option<Table>) -> Self

Creates a new Templating Engine.

An alternative would be converting from a HumusProtoEngine.

source

pub fn render_view(&self, settings: &S, view: V) -> Response

Takes settings and a view, converting it to a serveable response.

Example:

async fn hello_world_handler(
	State(arc_state): State<Arc<ServiceSharedState>>,
	Extension(settings): Extension<QuerySettings>,
) -> Response {
	let state = Arc::clone(&arc_state);

	state.templating_engine.render_view(
		&settings,
		View::Message{
			title: "Hey There!".to_string(),
			message: "You are an awesome creature!".to_string()
		},
	)
}

Trait Implementationsยง

sourceยง

impl<V, S, F> Clone for HumusEngine<V, S, F>
where V: HumusView<S, F> + Clone, S: HumusQuerySettings<F> + Clone, F: HumusFormat + Clone,

sourceยง

fn clone(&self) -> HumusEngine<V, S, F>

Returns a copy of the value. Read more
1.0.0 ยท sourceยง

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
sourceยง

impl<V, S, F> From<HumusEngine<V, S, F>> for HumusProtoEngine
where V: HumusView<S, F>, S: HumusQuerySettings<F>, F: HumusFormat,

sourceยง

fn from(e: HumusEngine<V, S, F>) -> Self

Converts to this type from the input type.
sourceยง

impl<V, S, F> From<HumusProtoEngine> for HumusEngine<V, S, F>
where V: HumusView<S, F>, S: HumusQuerySettings<F>, F: HumusFormat,

sourceยง

fn from(e: HumusProtoEngine) -> Self

Converts to this type from the input type.

Auto Trait Implementationsยง

ยง

impl<V, S, F> !RefUnwindSafe for HumusEngine<V, S, F>

ยง

impl<V, S, F> Send for HumusEngine<V, S, F>
where S: Send, V: Send,

ยง

impl<V, S, F> Sync for HumusEngine<V, S, F>
where F: Sync, S: Sync, V: Sync,

ยง

impl<V, S, F> Unpin for HumusEngine<V, S, F>
where F: Unpin, S: Unpin, V: Unpin,

ยง

impl<V, S, F> !UnwindSafe for HumusEngine<V, S, F>

Blanket Implementationsยง

sourceยง

impl<T> Any for T
where T: 'static + ?Sized,

sourceยง

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
sourceยง

impl<T> Borrow<T> for T
where T: ?Sized,

sourceยง

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
sourceยง

impl<T> BorrowMut<T> for T
where T: ?Sized,

sourceยง

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
sourceยง

impl<T> From<T> for T

sourceยง

fn from(t: T) -> T

Returns the argument unchanged.

ยง

impl<T> FromRef<T> for T
where T: Clone,

ยง

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
ยง

impl<T> Instrument for T

ยง

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
ยง

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
sourceยง

impl<T, U> Into<U> for T
where U: From<T>,

sourceยง

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

ยง

impl<T> Pointable for T

ยง

const ALIGN: usize = _

The alignment of pointer.
ยง

type Init = T

The type for initializers.
ยง

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
ยง

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
ยง

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
ยง

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
sourceยง

impl<T> Same for T

ยง

type Output = T

Should always be Self
sourceยง

impl<T> ToOwned for T
where T: Clone,

ยง

type Owned = T

The resulting type after obtaining ownership.
sourceยง

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
sourceยง

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
sourceยง

impl<T, U> TryFrom<U> for T
where U: Into<T>,

ยง

type Error = Infallible

The type returned in the event of a conversion error.
sourceยง

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
sourceยง

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

ยง

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
sourceยง

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
ยง

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

ยง

fn vzip(self) -> V

ยง

impl<T> WithSubscriber for T

ยง

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
ยง

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more