Enum MaybeSafe

Source
pub enum MaybeSafe<T> {
    Safe(T),
    NeedsEscaping(T),
}
Expand description

Mark the output of a filter as “maybe safe”

This enum can be used as a transparent return type of custom filters that want to mark their output as “safe” depending on some circumstances, i.e. that their output maybe does not need to be escaped.

If the filter is not used as the last element in the filter chain, then any assumption is void. Let the next filter decide if the output is safe or not.

§Example

mod filters {
    use askama::{filters::MaybeSafe, Result};

    // Do not actually use this filter! It's an intentionally bad example.
    pub fn backdoor<T: std::fmt::Display>(s: T, enable: &bool) -> Result<MaybeSafe<T>> {
        Ok(match *enable {
            true => MaybeSafe::Safe(s),
            false => MaybeSafe::NeedsEscaping(s),
        })
    }
}

#[derive(askama::Template)]
#[template(
    source = "<div class='{{ klass|backdoor(enable_backdoor) }}'></div>",
    ext = "html"
)]
struct DivWithBackdoor<'a> {
    klass: &'a str,
    enable_backdoor: bool,
}

assert_eq!(
    DivWithBackdoor { klass: "<script>", enable_backdoor: false }.to_string(),
    "<div class='&#60;script&#62;'></div>",
);
assert_eq!(
    DivWithBackdoor { klass: "<script>", enable_backdoor: true }.to_string(),
    "<div class='<script>'></div>",
);

Variants§

§

Safe(T)

The contained value does not need escaping

§

NeedsEscaping(T)

The contained value needs to be escaped

Trait Implementations§

Source§

impl<T: Display> Display for MaybeSafe<T>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T: FastWritable> FastWritable for MaybeSafe<T>

Source§

fn write_into<W: Write + ?Sized>(&self, dest: &mut W) -> Result<()>

Used internally by askama to speed up writing some types.

Auto Trait Implementations§

§

impl<T> Freeze for MaybeSafe<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for MaybeSafe<T>
where T: RefUnwindSafe,

§

impl<T> Send for MaybeSafe<T>
where T: Send,

§

impl<T> Sync for MaybeSafe<T>
where T: Sync,

§

impl<T> Unpin for MaybeSafe<T>
where T: Unpin,

§

impl<T> UnwindSafe for MaybeSafe<T>
where T: UnwindSafe,

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.

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.