Skip to main content

Renderer

Struct Renderer 

Source
pub struct Renderer<'a> { /* private fields */ }
Expand description

A builder for rendering a Template with arguments.

§Examples

use formatx::Template;

let template = Template::new("{} + {} = {}").unwrap();
let result = template.render()
    .arg(&1)
    .arg(&2)
    .arg(&3)
    .finish()
    .unwrap();
assert_eq!(result, "1 + 2 = 3");

Implementations§

Source§

impl<'a> Renderer<'a>

Source

pub fn arg(&mut self, value: &'a (impl Display + Debug)) -> &mut Self

Add a positional argument.

Source

pub fn named( &mut self, name: &'a str, value: &'a (impl Display + Debug), ) -> &mut Self

Add a named argument.

Examples found in repository?
examples/struct.rs (line 34)
15fn main() {
16    let origin = Point { x: 0.0, y: 0.0 };
17    let target = Point { x: 3.5, y: -2.1 };
18
19    // Display
20    let result = formatx!("From {} to {}", origin, target).unwrap();
21    println!("{result}");
22
23    // Debug
24    let result = formatx!("Debug: {:?}", origin).unwrap();
25    println!("{result}");
26
27    // Pretty debug
28    let result = formatx!("Pretty: {:#?}", origin).unwrap();
29    println!("{result}");
30
31    // Template reuse
32    let template = formatx::Template::new("Point {name}: {point}").unwrap();
33    let r1 = template.render()
34        .named("name", &"Origin")
35        .named("point", &origin)
36        .finish()
37        .unwrap();
38    let r2 = template.render()
39        .named("name", &"Target")
40        .named("point", &target)
41        .finish()
42        .unwrap();
43    println!("{r1}");
44    println!("{r2}");
45}
Source

pub fn finish(&self) -> Result<String, Error>

Strict: produce the formatted output.

Returns Err(Error::MissingArgument) if any placeholder references an argument that was not provided.

Examples found in repository?
examples/struct.rs (line 36)
15fn main() {
16    let origin = Point { x: 0.0, y: 0.0 };
17    let target = Point { x: 3.5, y: -2.1 };
18
19    // Display
20    let result = formatx!("From {} to {}", origin, target).unwrap();
21    println!("{result}");
22
23    // Debug
24    let result = formatx!("Debug: {:?}", origin).unwrap();
25    println!("{result}");
26
27    // Pretty debug
28    let result = formatx!("Pretty: {:#?}", origin).unwrap();
29    println!("{result}");
30
31    // Template reuse
32    let template = formatx::Template::new("Point {name}: {point}").unwrap();
33    let r1 = template.render()
34        .named("name", &"Origin")
35        .named("point", &origin)
36        .finish()
37        .unwrap();
38    let r2 = template.render()
39        .named("name", &"Target")
40        .named("point", &target)
41        .finish()
42        .unwrap();
43    println!("{r1}");
44    println!("{r2}");
45}
Source

pub fn finish_lenient(&self) -> Result<String, Error>

Lenient: produce the formatted output.

Missing arguments are replaced with an empty string "" instead of producing an error.

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for Renderer<'a>

§

impl<'a> !Send for Renderer<'a>

§

impl<'a> !Sync for Renderer<'a>

§

impl<'a> !UnwindSafe for Renderer<'a>

§

impl<'a> Freeze for Renderer<'a>

§

impl<'a> Unpin for Renderer<'a>

§

impl<'a> UnsafeUnpin for Renderer<'a>

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.