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>
impl<'fmt> Oneshot<'fmt>
Sourcepub fn validate<A>(self) -> Result<(), SyntaxError<A::Error>>where
A: Ast<'fmt>,
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());Sourcepub fn validate_any(self) -> Result<(), SyntaxError<Infallible>>
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.
Sourcepub fn spans<A>(self) -> TemplateSpans<'fmt, A> ⓘwhere
A: Ast<'fmt>,
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.
Sourcepub fn render<A, M>(self, mfst: &M) -> Result<String, Error<A::Error, M::Error>>where
A: Ast<'fmt>,
M: ManifestMut<A>,
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.
Sourcepub fn render_io<A, M, W>(
self,
mfst: &M,
writer: W,
) -> Result<(), Error<A::Error, M::Error>>
pub fn render_io<A, M, W>( self, mfst: &M, writer: W, ) -> Result<(), Error<A::Error, M::Error>>
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.
Sourcepub fn render_fmt<A, M, W>(
self,
mfst: &M,
writer: W,
) -> Result<(), Error<A::Error, M::Error>>
pub fn render_fmt<A, M, W>( self, mfst: &M, writer: W, ) -> Result<(), Error<A::Error, M::Error>>
Write the template into the provided fmt::Write implementation.