Trait HumusFormat

Source
pub trait HumusFormat:
    ToString
    + Clone
    + Default
    + Send {
    // Required method
    fn from_name(name: &str) -> Option<Self>;

    // Provided methods
    fn get_family(&self) -> HumusFormatFamily { ... }
    fn get_file_extension(&self) -> String { ... }
    fn get_name(&self) -> String { ... }
    fn get_less_well_known_mimetype(&self) -> Option<Mime> { ... }
    fn get_mime_type(&self) -> Mime { ... }
}
Available on crate feature axum-view only.
Expand description

Provides information on what to render and how to deliver it to the HumusEngine.

It is an integral part of a HumusQuerySettings implementation.

It is best implemented on an enum.

Also have a look at the provided methods! They may be sane defaults for testing and prototyping, implementing them yourself on enum match logic will improve performance and help with providing missing values.

For a quick prototype implementing ToString and the from_name() method is enough in most cases.

Recommended macros:

#[derive(Clone,Serialize,Deserialize,Default)]
#[serde(rename_all="lowercase")]

For an example implementation see: HtmlTextJsonFormat

Required Methods§

Source

fn from_name(name: &str) -> Option<Self>

Constructs a view from its name.

Provided Methods§

Source

fn get_family(&self) -> HumusFormatFamily

Return wheter this format should be processed in Template or API mode.

Source

fn get_file_extension(&self) -> String

Returns the file extnsion for the format.

Used for deriving the path for the template name.

Defaults to .{self.get_name()} with the exception of the name being text then it defaults to .txt.

Source

fn get_name(&self) -> String

Returns the name of the format, by default taken from the ToString implementation.

Source

fn get_less_well_known_mimetype(&self) -> Option<Mime>

Allows adding extra mimetypes quickly for prototyping

Implementing get_mime_type() properly is recommended for production use.

Source

fn get_mime_type(&self) -> Mime

Returns the Mimetype that is expected for this output format.

It is recommended to implement this when in production use.

For prototyping the default implementation makes assumptions based on the output of get_name(), falling back to get_less_well_known_mimetype() and the “application/octet-stream” type.

The default implementation knows the following associations:

  • text: text/plain; charset=utf-8
  • html: text/html; charset=utf-8
  • json: application/json
  • xml: application/xml
  • rss: application/rss+xml
  • atom: application/atom+xml

Implementation Note: It may be possible that two different views have the same MimeType (maybe two json representations for different consumers).

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§