Crate mfmt

Source
Expand description

§mfmt

GitHub Action Crate License

Meta formatter library in Rust.

mfmt is a language formatter library written in Rust inspired by go fmt. It’s designed to be configuration-free and generous about styling. It simply focuses on aligning indentations.

This library is used in the following projects.

§Install

cargo add mfmt

§Examples

use bumpalo::Bump;
use indoc::indoc;
use mfmt::{Builder, format, FormatOptions, line};

let allocator = Bump::new();
let builder = Builder::new(&allocator);

let mut string = String::new();

format(
    &builder.sequence([
        "{".into(),
        builder.indent(builder.sequence([line(), "foo".into(), line(), "bar".into()])),
        line(),
        "}".into(),
    ]),
    &mut string,
    FormatOptions::new(4),
)
.unwrap();

assert_eq!(
    string,
    indoc!(
        "
        {
            foo
            bar
        }
        "
    )
    .trim(),
);

§Technical notes

Unlike the Wadler’s algorithm or some other formatters like prettier, mfmt does not search the best format given source codes. For example, we do not have any “group” combinator. Instead, we rather give a formatter information to reconstruct the “best” format that is available in the original source codes.

§References

§License

The Unlicense

Modules§

utility
Utilities.

Structs§

Builder
Document builder.
FormatOptions
Format options.

Enums§

Document
A document.

Functions§

break
Breaks a document into multiple lines.
empty
Creates an empty document.
flatten
Flattens a document.
flatten_if
Flattens a document if a condition is true.
format
Formats a document.
indent
Indents a document.
line
Creates a new line.
line_suffix
Creates a line suffix.
offside
Creates a document indented to a current column.
sequence
Creates a sequence of documents.