Struct formatify::StringFormatter
source · pub struct StringFormatter;Implementations§
source§impl StringFormatter
impl StringFormatter
pub fn new() -> Self
sourcepub fn replace_placeholders(
&self,
key_value: &HashMap<&str, String>,
inp: &str
) -> String
pub fn replace_placeholders( &self, key_value: &HashMap<&str, String>, inp: &str ) -> String
Replaces placeholders in the input string with corresponding values from a HashMap.
This method scans the input string (inp) for placeholders, identified by a specific
syntax (e.g., “%(var1)”), and replaces them with corresponding values from the
key_value HashMap. The replacement process is versatile, accommodating various placeholder
formats, including those requiring left alignment and truncation. It’s ideal for dynamically
generating strings where placeholders are replaced by context-specific values.
Arguments
key_value- A reference to a HashMap where keys correspond to placeholder identifiers in the input string and values are their replacements.inp- The input string containing placeholders.
Returns
A new String with placeholders replaced by their respective values from the key_value HashMap.
If a placeholder has no corresponding value in the map, it remains unchanged in the output string.
Examples
use std::collections::HashMap;
use formatify::StringFormatter;
let mut key_value : HashMap<&str, String> = HashMap::new();
key_value.insert("name", "Alice".into());
let formatter = StringFormatter::new();
let formatted_string = formatter.replace_placeholders(&key_value, "Hello, %(name)!");
assert_eq!(formatted_string, "Hello, Alice!");sourcepub fn measure_lengths(
&self,
key_value: &HashMap<&str, String>,
inp: &str
) -> Vec<usize>
pub fn measure_lengths( &self, key_value: &HashMap<&str, String>, inp: &str ) -> Vec<usize>
Measures the length of the entire string and the lengths of valid placeholders within it.
This method processes the input string (inp), which is analyzed as if it were to be formatted.
Instead of replacing the placeholders, it calculates the overall length of the string with
placeholders hypothetically replaced, followed by the lengths of each valid placeholder. This
is particularly useful for layout planning and understanding the impact of placeholders on the
total length of the string.
Arguments
key_value- A reference to a HashMap containing key-value pairs. The keys represent placeholders in the input string, and the values are their potential replacements.inp- The input string with placeholders to be measured.
Returns
A Vec<usize> where the first element represents the length of the entire string with placeholders
replaced, and subsequent elements represent the lengths of each valid placeholder. Placeholders
are replaced with their corresponding values from the key_value HashMap for these calculations.
Examples
use std::collections::HashMap;
use formatify::StringFormatter;
let mut key_value : HashMap<&str, String> = HashMap::new();
key_value.insert("name", "Alice".into());
let formatter = StringFormatter::new();
let segment_lengths = formatter.measure_lengths(&key_value, "Hello, %(name)! This is a test.");
assert_eq!(segment_lengths, vec![29, 5]); // Total length with "Alice" as the placeholder, length of "Alice"sourcepub fn extract_placeholder_keys(
&self,
key_value: &HashMap<&str, String>,
inp: &str
) -> Vec<String>
pub fn extract_placeholder_keys( &self, key_value: &HashMap<&str, String>, inp: &str ) -> Vec<String>
Extracts and lists all valid placeholder keys from a given string.
This method analyzes the input string (inp) to identify and collect the keys of all
placeholders defined within it. Placeholders are identified by a specific syntax, typically
denoted by “%(key)”. This function is particularly useful for determining which placeholders
are used in a string without modifying the string itself. It helps in preparing or validating
the necessary keys in a key-value map for subsequent processing, like formatting or replacing
placeholders.
Arguments
key_value- A reference to a HashMap containing key-value pairs. These pairs may be used within the placeholder syntax in the input string.inp- The input string to be analyzed for placeholder keys.
Returns
A Vec<String> containing all distinct placeholder keys found in the input string. If no
placeholders are found, an empty vector is returned.
Examples
use std::collections::HashMap;
use formatify::StringFormatter;
let mut key_value : HashMap<&str, String> = HashMap::new();
key_value.insert("name", "Alice".into());
let formatter = StringFormatter::new();
let placeholder_keys = formatter.extract_placeholder_keys(&key_value, "Hello, %(name)! Today is %(day).");
assert_eq!(placeholder_keys, vec!["name"]);