2 releases
0.1.1 | Nov 3, 2024 |
---|---|
0.1.0 | Nov 3, 2024 |
#755 in Text processing
1,142 downloads per month
7KB
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:
- Takes a string literal as input
- Splits it into lines
- Calculates the minimum indentation level across all non-empty lines
- Removes that amount of whitespace from the start of each line
- Removes leading and trailing empty lines
- Preserves relative indentation between lines
License
Licensed under:
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
Credits
This crate is inspired by the dedent
npm package.
Dependencies
~220โ660KB
~16K SLoC