Thanks to visit codestin.com
Credit goes to lib.rs

10 releases

0.3.4 Jan 31, 2025
0.3.3 Dec 5, 2024
0.2.3 Dec 3, 2024
0.1.0 Jul 9, 2024

#219 in Value formatting

Codestin Search App Codestin Search App Codestin Search App Codestin Search App Codestin Search App Codestin Search App Codestin Search App Codestin Search App Codestin Search App Codestin Search App Codestin Search App Codestin Search App Codestin Search App

52 downloads per month
Used in 5 crates (2 directly)

LGPL-3.0-or-later

26KB
224 lines

indent formatting, everywhere

CI Badge Crates.io Version docs.rs Crates.io License unsafe forbidden

inform gives you

  • A std::fmt::Formatter drop-in replacement designed for formatting structured data such as AST nodes.
  • More generally, an API for formatting any type implementing std::io::Write or std::fmt::Write with indentation.

The format and I/O implementations are behind Cargo features "fmt" and "io" respectively, both of which are enabled by default.

Contents

Examples

Here's how you can use fmt::IndentFormatter:

use std::fmt::{self, Write};
use inform::common::IndentWriterCommon, fmt::IndentFormatter;

struct TestIndentFormatter;

impl fmt::Display for TestIndentFormatter {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut f = IndentFormatter::new(f, 2);
        writeln!(f, "hello\ngoodbye")?;
        f.increase_indent();
        writeln!(f, "hello\ngoodbye")?;
        f.decrease_indent();
        writeln!(f, "hello\ngoodbye")
    }
}

#[test]
fn test_indent_formatter() {
    assert_eq!(
        "hello\ngoodbye\n  hello\n  goodbye\nhello\ngoodbye\n",
        TestIndentFormatter.to_string()
    );
}

Here's how you can use fmt::IndentWriter:

use std::fmt::{self, Write};
use inform::common::IndentWriterCommon, fmt::IndentWriter;

fn write_text(str: &mut String) -> fmt::Result {
    let mut f = IndentWriter::new(str, 2);
    writeln!(f, "hello\ngoodbye")?;
    f.increase_indent();
    writeln!(f, "hello\ngoodbye")?;
    f.decrease_indent();
    writeln!(f, "hello\ngoodbye")
}

#[test]
fn test_indent_writer() {
    let mut buffer = String::new();
    write_text(&mut buffer).expect("failed to format");
    assert_eq!(
        "hello\ngoodbye\n  hello\n  goodbye\nhello\ngoodbye\n",
        buffer
    );
}

Projects using inform

Alternatives

The following crates are alternatives that I found did not fit my use case.

License

A copy of the LGPL License is provided in the LICENSE file.

No runtime deps

~130KB