pub struct Template { /* private fields */ }
Expand description
String templating with std::fmt
syntax.
Example
use formatx::Template;
let mut template = "{percentage:.2}".parse::<Template>().unwrap();
template.replace("percentage", 67.7892);
assert_eq!(template.text().unwrap(), "67.79");
Implementations
sourceimpl Template
impl Template
sourcepub fn new<T: Into<String>>(template: T) -> Result<Self, Error>
pub fn new<T: Into<String>>(template: T) -> Result<Self, Error>
Create a new instance of format_template::Template
.
Example
use formatx::Template;
let template = Template::new("{percentage:.2}").unwrap();
sourcepub fn contains<T: ToString>(&self, placeholder: T) -> bool
pub fn contains<T: ToString>(&self, placeholder: T) -> bool
Checks wheter template contains placeholder or not.
sourcepub fn unchecked_text(&self) -> String
pub fn unchecked_text(&self) -> String
Returns unchecked formatted template.
For Result
version of fomatted template use .text
method.
sourcepub fn text(&self) -> Result<String, Error>
pub fn text(&self) -> Result<String, Error>
Checks wheter all placeholders are replaced or not and returns formatted template.
sourcepub fn replace<T, U>(&mut self, placeholder: T, value: U) where
T: ToString,
U: Display + Debug,
pub fn replace<T, U>(&mut self, placeholder: T, value: U) where
T: ToString,
U: Display + Debug,
Replace a template placeholder with a value. This change is inplace.
sourcepub fn replace_positional<T>(&mut self, value: T) where
T: Display + Debug,
pub fn replace_positional<T>(&mut self, value: T) where
T: Display + Debug,
Replace a template positional placeholder with a value. This change is inplace.
sourcepub fn replace_from_callback<T, U>(&mut self, placeholder: T, callback: U) where
T: ToString,
U: Fn(&Placeholder) -> String,
pub fn replace_from_callback<T, U>(&mut self, placeholder: T, callback: U) where
T: ToString,
U: Fn(&Placeholder) -> String,
Replace a template placeholder from a callback String
.
This change is inplace.
Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for Template
impl Send for Template
impl Sync for Template
impl Unpin for Template
impl UnwindSafe for Template
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more