EscapeWrapper

Struct EscapeWrapper 

Source
pub struct EscapeWrapper<E>(/* private fields */);
Expand description

Zero sized wrapper used to select an escape function

To implement your own specialization, you can implement your own trait this way:

use std::fmt;

// First you have to add a "marker" trait for types that you want to escape
// with your custom escaper.

trait MyEscapeMarker {}

impl<T: MyEscapeMarker> MyEscapeMarker for &T {}

// You can implement your custom escaper for multiple types.

enum TerribleXml<'a> {
    Start(&'a str),
    End(&'a str),
    Text(&'a str),
}

impl MyEscapeMarker for TerribleXml<'_> {}

// Second you add a new trait that wraps a reference to the value to escape.
// If the value is `Copy`, then you don't have to keep reference to `value`.
// You must not capture a reference to `self`, because `self` is ephemeral.

trait MyEscapeKind {
    #[inline]
    fn wrap<'a>(&self, value: &'a TerribleXml) -> MyEscaper<'a> {
        MyEscaper { value }
    }
}

impl<T: MyEscapeMarker> MyEscapeKind for nate::EscapeWrapper<T> {}

// Lastly you have to implement `std::fmt::Display` for your escaper.

struct MyEscaper<'a> {
    value: &'a TerribleXml<'a>,
}

impl fmt::Display for MyEscaper<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.value {
            TerribleXml::Start(tag) => write!(f, "<{}>", tag),
            TerribleXml::End(tag) => write!(f, "</{}>", tag),
            TerribleXml::Text(text) => f.write_str(text),
        }
    }
}

// Then you can use the escaper in your templates.
// The trait `MyEscapeKind` has to be in scope of the template declaration.

#[derive(nate::Nate)]
#[template(path = "templates/custom-escaper.html")]
struct Template<'a> {
    elems: &'a [TerribleXml<'a>],
}

#[test]
fn test_custom_escaper() {
    let template = Template { elems: &[
        TerribleXml::Text("Hello, "),
        TerribleXml::Start("strong"),
        TerribleXml::Text("world"),
        TerribleXml::End("b"),
        TerribleXml::Text("!"),
    ] };
    let data = format!("{}", template);
    assert_eq!(data, "Hello, <strong>world</b>!");
}

Trait Implementations§

Source§

impl<E: Clone> Clone for EscapeWrapper<E>

Source§

fn clone(&self) -> EscapeWrapper<E>

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<E: Debug> Debug for EscapeWrapper<E>

Source§

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

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

impl<E: Default> Default for EscapeWrapper<E>

Source§

fn default() -> EscapeWrapper<E>

Returns the “default value” for a type. Read more
Source§

impl<E: Copy> Copy for EscapeWrapper<E>

Auto Trait Implementations§

§

impl<E> Freeze for EscapeWrapper<E>

§

impl<E> RefUnwindSafe for EscapeWrapper<E>
where E: RefUnwindSafe,

§

impl<E> Send for EscapeWrapper<E>
where E: Send,

§

impl<E> Sync for EscapeWrapper<E>
where E: Sync,

§

impl<E> Unpin for EscapeWrapper<E>
where E: Unpin,

§

impl<E> UnwindSafe for EscapeWrapper<E>
where E: 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> 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, 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> 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.