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='<script>'></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: FastWritable> FastWritable for MaybeSafe<T>
impl<T: FastWritable> FastWritable for MaybeSafe<T>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more