Struct 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 */ }
Available on crate feature axum-view only.
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 duplicate 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> Debug for HumusEngine<V, S, F>
where V: HumusView<S, F> + Debug, S: HumusQuerySettings<F> + Debug, F: HumusFormat + Debug,

Sourceยง

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. 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> Freeze for HumusEngine<V, S, F>

ยง

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

ยง

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

ยง

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

ยง

impl<V, S, F> Unpin for HumusEngine<V, S, F>
where V: Unpin, S: Unpin, F: 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> CloneToUninit for T
where T: Clone,

Sourceยง

unsafe fn clone_to_uninit(&self, dest: *mut u8)

๐Ÿ”ฌThis is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Sourceยง

impl<T> From<T> for T

Sourceยง

fn from(t: T) -> T

Returns the argument unchanged.

Sourceยง

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

Sourceยง

fn from_ref(input: &T) -> T

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

impl<T> Instrument for T

Sourceยง

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

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

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.

Sourceยง

impl<T> Pointable for T

Sourceยง

const ALIGN: usize

The alignment of pointer.
Sourceยง

type Init = T

The type for initializers.
Sourceยง

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

Initializes a with the given initializer. Read more
Sourceยง

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

Dereferences the given pointer. Read more
Sourceยง

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

Mutably dereferences the given pointer. Read more
Sourceยง

unsafe fn drop(ptr: usize)

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

impl<T> Same for T

Sourceยง

type Output = T

Should always be Self
Sourceยง

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

Sourceยง

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>,

Sourceยง

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>,

Sourceยง

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.
Sourceยง

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

Sourceยง

fn vzip(self) -> V

Sourceยง

impl<T> WithSubscriber for T

Sourceยง

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
Sourceยง

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Sourceยง

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,