Oneshot

Struct Oneshot 

Source
pub struct Oneshot<'fmt> { /* private fields */ }
Expand description

A template which can be rendered at most once.

In many cases, you want to use Template since the additional cost of preparing a Template is relatively minimal (a single Vec allocation).

A Oneshot template provides the same render methods as a Template: Oneshot::render, Oneshot::render_io, and Oneshot::render_fmt. The main difference is that these methods accept self and return a more general Error type since parsing and rendering occur at the same time.

§Examples

Check syntax using Oneshot::validate:

use mufmt::{Ast, Oneshot, types::IgnoredAny};

fn is_valid<'fmt, A: Ast<'fmt>>(s: &'fmt str) -> bool {
    Oneshot::new(s).validate::<A>().is_ok()
}

// `IgnoredAny` is a special type which can be parsed from any expression
// and ignores the contents
assert!(is_valid::<IgnoredAny>("No errors {{ {expr}"));
assert!(!is_valid::<IgnoredAny>("Invalid: {{expr}"));
// validate expressions as `usize`
assert!(!is_valid::<usize>("Not a usize: {-100}"));

Implementations§

Source§

impl<'fmt> Oneshot<'fmt>

Source

pub fn new(s: &'fmt str) -> Self

Initialize from a template string.

Source

pub fn validate<A>(self) -> Result<(), SyntaxError<A::Error>>
where A: Ast<'fmt>,

Consume this template, checking if the syntax is valid for the provided Ast.

This method only reports the first error encountered. If you want to report as many errors as possible, use a TemplateSpans iterator instead which supports error recovery.

§Examples

Validate template syntax.

use mufmt::Oneshot;

let oneshot = Oneshot::new("Valid {template}");
assert!(oneshot.validate::<&str>().is_ok());

let oneshot = Oneshot::new("{unclosed");
assert!(oneshot.validate::<&str>().is_err());

The provided Ast determines the expression validation rules.

let s = "Not a number: {template}";
assert!(Oneshot::new(s).validate::<usize>().is_err());
assert!(Oneshot::new(s).validate::<&str>().is_ok());

Check that a template contains no expression blocks using Infallible:

use std::convert::Infallible;
assert!(Oneshot::new("Contains an expr: {}").validate::<Infallible>().is_err());
assert!(Oneshot::new("Only text!").validate::<Infallible>().is_ok());
Source

pub fn validate_any(self) -> Result<(), SyntaxError<Infallible>>

Consumes this template, checking if the syntax is valid according to the global syntax rules (i.e., without checking the expressions).

This is a shorthand for calling Oneshot::validate with Ast type IgnoredAny.

Source

pub fn spans<A>(self) -> TemplateSpans<'fmt, A>
where A: Ast<'fmt>,

Returns an iterator over the spans corresponding to the underlying template string.

See the TemplateSpans docs for more detail.

Source

pub fn render<A, M>(self, mfst: &M) -> Result<String, Error<A::Error, M::Error>>
where A: Ast<'fmt>, M: ManifestMut<A>,

A convenience function to render directly into a newly allocated String.

This is equivalent to allocating a new String yourself and writing into it with render_fmt.

Source

pub fn render_io<A, M, W>( self, mfst: &M, writer: W, ) -> Result<(), Error<A::Error, M::Error>>
where A: Ast<'fmt>, M: ManifestMut<A>, W: Write,

Write the template into the provided io::Write implementation.

The writer is not flushed unless the Manifest implementation overrides the default Manifest::write_io implementation to manually flush the writer.

Source

pub fn render_fmt<A, M, W>( self, mfst: &M, writer: W, ) -> Result<(), Error<A::Error, M::Error>>
where A: Ast<'fmt>, M: ManifestMut<A>, W: Write,

Write the template into the provided fmt::Write implementation.

Auto Trait Implementations§

§

impl<'fmt> Freeze for Oneshot<'fmt>

§

impl<'fmt> RefUnwindSafe for Oneshot<'fmt>

§

impl<'fmt> Send for Oneshot<'fmt>

§

impl<'fmt> Sync for Oneshot<'fmt>

§

impl<'fmt> Unpin for Oneshot<'fmt>

§

impl<'fmt> UnwindSafe for Oneshot<'fmt>

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