Skip to content

A Rust utility for automatically normalizing multi-line string indentation while preserving platform-specific line endings.

License

Notifications You must be signed in to change notification settings

jzombie/rust-string-auto-indent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Multi-line String Auto-Indent

made-with-rust crates.io Documentation MIT licensed

OS Status
Ubuntu-latest Ubuntu Tests
macOS-latest macOS Tests
Windows-latest Windows Tests

A Rust utility for automatically normalizing multi-line string indentation while preserving platform-specific line endings.

Overview

When working with multi-line strings inside indented code blocks, unwanted leading spaces may be introduced. This can affect readability, logging output, and formatted text generation.

string-auto-indent provides an automated way to normalize multi-line strings without modifying the first line's indentation.

Install

cargo add string-auto-indent

Usage

use string_auto_indent::{auto_indent, LineEnding};

let excessively_indented_text = r#"
                    Best Practices for Text Indentation
                    -----------------------------------

                        1. Importance of Proper Indentation
                            a. Enhances readability by clearly defining structure.
                            b. Prevents misinterpretation of hierarchical content.
                            c. Improves maintainability in collaborative environments.

                        2. Common Indentation Guidelines
                            a. Use consistent spacing (e.g., 2 or 4 spaces per level).
                            b. Avoid mixing spaces and tabs to ensure uniform formatting.
                            c. Align nested elements to maintain structural clarity.
                                1b. Maintain relative indentation depth across all nested elements.
                                2b. Ensure indentation reflects logical hierarchy.
            "#;


// Expected output after applying `auto_indent`
let normalized_indentation = r#"
Best Practices for Text Indentation
-----------------------------------

    1. Importance of Proper Indentation
        a. Enhances readability by clearly defining structure.
        b. Prevents misinterpretation of hierarchical content.
        c. Improves maintainability in collaborative environments.

    2. Common Indentation Guidelines
        a. Use consistent spacing (e.g., 2 or 4 spaces per level).
        b. Avoid mixing spaces and tabs to ensure uniform formatting.
        c. Align nested elements to maintain structural clarity.
            1b. Maintain relative indentation depth across all nested elements.
            2b. Ensure indentation reflects logical hierarchy.
"#;

// Verify that `auto_indent` correctly normalizes indentation
assert_eq!(
    auto_indent(excessively_indented_text),
    normalized_indentation,
    "The auto_indent function should normalize leading whitespace."
);

How It Works

  1. Detects the platform’s line endings (\n, \r\n, \r) and normalizes input for processing.
  2. Preserves the first line exactly as written.
  3. Finds the least-indented non-empty line (excluding the first) and adjusts all others accordingly.
  4. Ensures blank lines remain but contain no extra spaces.
  5. Restores platform-specific line endings when outputting the result.

When to Use

  • Formatting log messages or CLI output while ensuring alignment.
  • Cleaning up documentation strings or multi-line literals in indented Rust code.
  • Processing structured text while ensuring consistent indentation.
  • Declaring multi-line variables in code where the indentation should match the codebase for readability, but the actual string content should not retain unnecessary leading spaces.
  • Ensuring consistent formatting in generated strings for use in templates, serialization, or output rendering.

License

Licensed under MIT. See LICENSE for details.

About

A Rust utility for automatically normalizing multi-line string indentation while preserving platform-specific line endings.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages