Function macro_tools::indentation

source ·
pub fn indentation<Prefix, Src, Postfix>(
    prefix: Prefix,
    src: Src,
    postfix: Postfix
) -> String
where Prefix: AsRef<str>, Src: AsRef<str>, Postfix: AsRef<str>,
Expand description

Adds indentation and optional prefix/postfix to each line of the given string.

This function iterates over each line in the input string and applies the specified prefix and postfix to it, effectively indenting the string and optionally wrapping each line with additional content.

§Parameters

  • prefix : The string to prepend to each line, typically used for indentation.
  • src : The source string to be indented and modified.
  • postfix : The string to append to each line, can be used for line terminators or other suffixes.

§Type Parameters

  • Prefix : A type that can be referenced as a string slice, for the prefix.
  • Src : A type that can be referenced as a string slice, for the source string.
  • Postfix : A type that can be referenced as a string slice, for the postfix.

§Returns

A String that represents the original src string with prefix and postfix applied to each line.

§Example

use macro_tools::diag;

let input = "Line 1\nLine 2\nLine 3";
let indented = diag::indentation( "  ", input, ";" );
assert_eq!( indented, "  Line 1;\n  Line 2;\n  Line 3;" );

// Demonstrating the function's handling of trailing newlines
let input_with_newline = "Line 1\nLine 2\nLine 3\n";
let indented_with_newline = diag::indentation( "  ", input_with_newline, ";" );
assert_eq!( indented_with_newline, "  Line 1;\n  Line 2;\n  Line 3;\n  ;" );

In the example above, indentation is used to add two spaces before each line and a semicolon at the end of each line. The function also demonstrates handling of input strings that end with a newline character by appending an additional line consisting only of the prefix and postfix.