dedent

Crates.iodedent
lib.rsdedent
version0.1.1
sourcesrc
created_at2024-11-03 00:22:55.642631
updated_at2024-11-03 01:05:25.534218
descriptionProcedural macro for stripping indentation from multi-line string literals
homepagehttps://github.com/nyarthan/dedent
repositoryhttps://github.com/nyarthan/dedent
max_upload_size
id1433283
size13,485
Jannis Morgenstern (nyarthan)

documentation

https://docs.rs/dedent

README

dedent

A Rust procedural macro for stripping whitespace from multi-line string literals while preserving relative indentation.

Features

  • ๐Ÿ“ Preserves relative indentation between lines
  • ๐Ÿงน Trims leading and trailing empty lines

Usage

use dedent::dedent;

fn main() {
    // Basic usage
    let code = dedent!(r#"
        fn main() {
            println!("Hello, world!");
        }
    "#);
    
    println!("{}", code);
    // Output:
    // fn main() {
    //     println!("Hello, world!");
    // }

    // Works with varying indentation
    let text = dedent!(r#"
        First line
          Indented line
            More indented
        Back to start
    "#);
    
    println!("{}", text);
    // Output:
    // First line
    //   Indented line
    //     More indented
    // Back to start
}

How It Works

The dedent! macro:

  1. Takes a string literal as input
  2. Splits it into lines
  3. Calculates the minimum indentation level across all non-empty lines
  4. Removes that amount of whitespace from the start of each line
  5. Removes leading and trailing empty lines
  6. Preserves relative indentation between lines

License

Licensed under:

Credits

This crate is inspired by the dedent npm package.

Commit count: 5

cargo fmt