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:


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

Returns

Description

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

Returns

Description

Usage Example

let text = String::from("Hello  world!\n");
let replacements = [
    ("\n", " "),
    ("  ", " "),
];
let cleaned = repl(text, replacements);
println!("{}", cleaned); // "Hello world! "

Implementation Details


Interaction with Other Parts of the System


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