pub struct Formatify;Expand description
Formatify: Main struct for dynamic string formatting.
This struct is part of the formatify library, offering tools to parse strings with
placeholders and replace them with values from a HashMap. It handles various placeholder
formats and alignment options, suitable for adaptive text representation in diverse applications.
Key functionalities include replacing placeholders, measuring string lengths with placeholders,
and extracting placeholder keys.
§Usage
Import the necessary modules and use Formatify for string formatting tasks:
use formatify::{Formatify, PlaceholderFormatter};
use std::collections::HashMap;§Features
- Placeholder Replacement: Replace placeholders in strings with values from a
HashMap. - Length Measurement: Measure lengths of strings and placeholders.
- Placeholder Extraction: Extract placeholder keys from a string.
§Examples
§Replacing Placeholders
let mut key_value: HashMap<&str, String> = HashMap::new();
key_value.insert("name", "Alice".into());
let formatter = Formatify::new();
let formatted_string = formatter.replace_placeholders(&key_value, "Hello, %(name)!");
assert_eq!(formatted_string, "Hello, Alice!");§Measuring Lengths
let mut key_value: HashMap<&str, String> = HashMap::new();
key_value.insert("name", "Alice".into());
let formatter = Formatify::new();
let lengths = formatter.measure_lengths(&key_value, "Hello, %(name)! This is a test.");
assert_eq!(lengths, vec![29, 5]); // Total length with "Alice" as the placeholder, length of "Alice"§Extracting Placeholder Keys
let formatter = Formatify::new();
let placeholder_keys = formatter.extract_placeholder_keys("Hello, %(name)! Today is %(day).");
assert_eq!(placeholder_keys, vec!["name", "day"]);Implementations§
Trait Implementations§
Source§impl PlaceholderFormatter for Formatify
impl PlaceholderFormatter for Formatify
Source§fn replace_placeholders(
&self,
key_value: &HashMap<&str, String>,
inp: &str,
) -> String
fn replace_placeholders( &self, key_value: &HashMap<&str, String>, inp: &str, ) -> String
Replaces placeholders in the input string with corresponding values from a HashMap. Read more
Auto Trait Implementations§
impl Freeze for Formatify
impl RefUnwindSafe for Formatify
impl Send for Formatify
impl Sync for Formatify
impl Unpin for Formatify
impl UnwindSafe for Formatify
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