pub struct Template { /* private fields */ }
Expand description
Implementations§
Source§impl 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.
Examples found in repository?
examples/advance.rs (line 18)
3fn main() {
4 let mut template = "{percentage color=true:.2} => {percentage color=false:.0}%"
5 .parse::<Template>()
6 .unwrap();
7
8 template.replace_with_callback("percentage", 99.9999, |fmtval, placeholder| {
9 if let Some(color) = placeholder.attr("color") {
10 if color == "true" {
11 return "\x1b[31m".to_owned() + &fmtval + "\x1b[0m";
12 }
13 }
14
15 fmtval
16 });
17
18 println!("{}", template.text().unwrap());
19}
Sourcepub fn replace<T, U>(&mut self, placeholder: T, value: U)
pub fn replace<T, U>(&mut self, placeholder: T, value: U)
Replace a template placeholder with a value. This change is inplace.
Sourcepub fn replace_positional<T>(&mut self, value: T)
pub fn replace_positional<T>(&mut self, value: T)
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)
pub fn replace_from_callback<T, U>(&mut self, placeholder: T, callback: U)
Replace a template placeholder from a callback String
.
This change is inplace.
Sourcepub fn replace_with_callback<T, U, V>(
&mut self,
placeholder: T,
value: U,
callback: V,
)
pub fn replace_with_callback<T, U, V>( &mut self, placeholder: T, value: U, callback: V, )
Replace a template placeholder with a value and do additional formatting using callback. This change is inplace.
Examples found in repository?
examples/advance.rs (lines 8-16)
3fn main() {
4 let mut template = "{percentage color=true:.2} => {percentage color=false:.0}%"
5 .parse::<Template>()
6 .unwrap();
7
8 template.replace_with_callback("percentage", 99.9999, |fmtval, placeholder| {
9 if let Some(color) = placeholder.attr("color") {
10 if color == "true" {
11 return "\x1b[31m".to_owned() + &fmtval + "\x1b[0m";
12 }
13 }
14
15 fmtval
16 });
17
18 println!("{}", template.text().unwrap());
19}
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Template
impl RefUnwindSafe for Template
impl Send for Template
impl Sync for Template
impl Unpin for Template
impl UnwindSafe for Template
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more