Formatify

Struct Formatify 

Source
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

  1. Placeholder Replacement: Replace placeholders in strings with values from a HashMap.
  2. Length Measurement: Measure lengths of strings and placeholders.
  3. 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§

Source§

impl Formatify

Source

pub fn new() -> Self

Trait Implementations§

Source§

impl Default for Formatify

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl PlaceholderFormatter for Formatify

Source§

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
Source§

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. Read more
Source§

fn extract_placeholder_keys(&self, inp: &str) -> Vec<String>

Extracts and lists all placeholder keys from a given string. 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> 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, 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.