utils.rs
Overview
This file provides utility functions primarily focused on string manipulation and formatting. Its main functionality is to convert complex debug representations of errors or other data structures into more concise, readable single-line strings by replacing various whitespace and formatting characters. This is useful when detailed debug output needs to be condensed for logging or display purposes.
The two core functions are:
detailed: Converts theDebugtrait output of an object into a simplified single-line string.repl: A generic string replacement utility that applies multiple substring replacements iteratively.
Functions
detailed
pub(crate) fn detailed(err: &impl Debug) -> String
Purpose
Formats the debug representation of a value into a more compact, single-line string by removing redundant whitespace and certain formatting characters.
Parameters
err: A reference to any type implementing theDebugtrait. This is the object whose debug output will be simplified.
Returns
A
Stringcontaining the compacted debug representation.
Description
Uses Rust's pretty-print debug formatter (
{:#?}) to produce a multi-line, indented string representation of the input.Passes this string to the
replfunction to replace newline characters, multiple spaces, and certain bracket spacing with more compact equivalents.This transformation eliminates newlines and unnecessary spaces while maintaining structural clarity.
Usage Example
use std::fmt::Debug;
let error = SomeErrorType { /* fields */ };
let compact_str = detailed(&error);
println!("Compact error: {}", compact_str);
repl
pub(crate) fn repl<const N: usize>(mut s: String, old_new: [(&str, &str); N]) -> String
Purpose
Performs multiple substring replacements on a string, repeatedly replacing all occurrences of each old substring with its corresponding new substring until none remain.
Parameters
s: TheStringto be modified.old_new: A fixed-size array of tuple pairs, where each tuple contains:old(&str): The substring to find.new(&str): The substring to replaceoldwith.
Returns
A
Stringwith all specified substitutions applied.
Description
Iterates over all
(old, new)pairs in theold_newarray.For each pair, repeatedly replaces all occurrences of
oldwithnewuntil no occurrences remain.The loop ensures even overlapping or newly introduced target substrings are replaced in subsequent iterations.
Usage Example
let text = String::from("Hello world!\n");
let replacements = [
("\n", " "),
(" ", " "),
];
let cleaned = repl(text, replacements);
println!("{}", cleaned); // "Hello world! "
Implementation Details
The
detailedfunction leverages Rust's built-in pretty debug formatting ({:#?}), which outputs multi-line, indented debug info.To convert this multi-line output into a compact single-line string, it calls
replwith a set of replacements targeting:Newline characters (
\n) replaced with spaces.Double spaces replaced with single spaces.
Spaces around parentheses and braces removed.
Removal of trailing commas before closing parentheses or braces.
The
replfunction uses awhileloop inside aforloop to ensure all occurrences are replaced, including those introduced by previous replacements.This ensures that the transformations fully compact the string without leaving partial matches.
Interaction with Other Parts of the System
These utilities are designed to be used internally (
pub(crate)), meaning they are accessible within the current crate but not exposed externally.The
detailedfunction is likely used wherever detailed error or object debugging information is needed in a condensed form, such as in logging or error messages.The
replfunction is a generic utility that can be reused wherever multiple substring replacements are needed on strings.
Diagram
flowchart TD
A["detailed(err: &impl Debug)"] --> B[Format with "{:#?}"]
B --> C["repl(input_string, replacements)"]
C --> D[Return compacted String]
subgraph repl function
C --> E["For each (old, new) in replacements"]
E --> F[While string contains old]
F --> G[Replace old with new]
G --> F
F --> H[Next replacement pair]
H --> I[Return modified string]
end