Expand description
§Formatify: Dynamic String Formatting Library
Formatify is a Rust library designed for dynamic string formatting. It provides flexible and powerful tools for parsing strings with placeholders, similar to how Git formats commit logs (see Git pretty format), and replacing them with corresponding values. The library excels in handling various placeholder formats and alignment options, ideal for applications requiring adaptable text representation.
§Placeholder Formats
Formatify supports several types of placeholders, enabling a wide range of formatting options.
Placeholders are defined within a string using a specific syntax, typically denoted by %(key)
.
The library processes these placeholders and replaces them with corresponding values at runtime.
§Supported Placeholder Types:
-
Single-Character Placeholders:
- New Line (
%n
): Inserts a newline character where%n
is placed. - Percentage (
%%
): Escapes and inserts a literal percent sign.
- New Line (
-
Variable Substitution:
- Syntax:
%(key)
- Description: Replaces this placeholder with the value associated with
key
in thekey_value
HashMap.
- Syntax:
-
Format Placeholders:
- Left Alignment:
- Syntax:
%<(width)
- Description: Aligns the subsequent placeholder to the left within a field of
width
characters. The placeholder itself is not displayed.
- Syntax:
- Left Alignment with Truncation:
- Syntax:
%<(width,trunc)
- Description: Similar to left alignment, but truncates the text to fit within the specified
width
. The placeholder itself is not displayed.
- Syntax:
- Left Alignment with left Truncation:
- Syntax:
%>(width,ltrunc)
- Description: Similar to left alignment, but left truncates the text to fit within the specified
width
. The placeholder itself is not displayed.
- Syntax:
- Right Alignment:
- Syntax:
%>(width)
- Description: Aligns the subsequent placeholder to the right within a field of
width
characters. The placeholder itself is not displayed.
- Syntax:
- Right Alignment with Truncation:
- Syntax:
%>(width,trunc)
- Description: Similar to right alignment, but truncates the text to fit within the specified
width
. The placeholder itself is not displayed.
- Syntax:
- Right Alignment with left Truncation:
- Syntax:
%>(width,ltrunc)
- Description: Similar to right alignment, but left truncates the text to fit within the specified
width
. The placeholder itself is not displayed.
- Syntax:
- Left Alignment:
Note: In the context of format placeholders, width
refers to the total number of characters allocated for the value being formatted. For example, %<(10)
aligns the value within a 10-character wide field.
§Example Usage:
let mut key_value = 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!");
§Public Methods
Public methods utilizing these placeholders include:
replace_placeholders
: Replaces placeholders in a string with values from a HashMap.measure_lengths
: Calculates the length of strings and placeholders.extract_placeholder_keys
: Extracts and lists all valid placeholder keys from a string.
For more details on these methods and their usage, refer to the respective method documentation in this module.
§Integration and Compatibility
Formatify is designed to be easily integrated into existing Rust projects and works seamlessly with standard data types and collections.
§Contribution and Feedback
Contributions to Formatify are welcome. For bug reports, feature requests, or general feedback, please open an issue on the repository’s issue tracker.
Structs§
- Formatify
Formatify
: Main struct for dynamic string formatting.
Traits§
- Placeholder
Formatter - Trait used to abstract Formatify from a system.