Skip to main content

Template

Struct Template 

Source
pub struct Template { /* private fields */ }
Expand description

An owned, parsed format string that can be rendered many times with different arguments.

§Examples

use formatx::Template;

let template = Template::new("{name} scored {score:.1}%").unwrap();
assert!(template.contains("name"));

let result = template.render()
    .named("name", &"Alice")
    .named("score", &95.678)
    .finish()
    .unwrap();
assert_eq!(result, "Alice scored 95.7%");

Implementations§

Source§

impl Template

Source

pub fn new<S: Into<String>>(source: S) -> Result<Self, Error>

Parse a format string into a reusable template.

Returns Err if the format string is malformed (unmatched braces, invalid specs, etc.).

Examples found in repository?
examples/struct.rs (line 32)
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 render(&self) -> Renderer<'_>

Create a Renderer to format this template with arguments.

The renderer collects arguments and produces the formatted output.

Examples found in repository?
examples/struct.rs (line 33)
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 contains(&self, name: &str) -> bool

Returns true if the template contains a placeholder with the given name.

Source

pub fn placeholders(&self) -> Vec<&str>

Returns the names of all named placeholders in the template.

Source

pub fn source(&self) -> &str

Returns the original format string.

Trait Implementations§

Source§

impl Debug for Template

Source§

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

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

impl Display for Template

Source§

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

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

impl FromStr for Template

Source§

type Err = Error

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more

Auto Trait Implementations§

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> FormatValue for T
where T: Display + Debug,

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.