Skip to main content

Crate fmtron

Crate fmtron 

Source
Expand description

A formatter for RON files.

format_ron turns RON text into fmtron’s layout, keeping every comment and the value’s meaning. The fmtron binary (the crate’s cli feature, on by default) wraps it for files, directories, --check and editors; this library is the same formatter for programs of your own, such as an editor plugin or a web page that formats RON. Without the cli feature it depends on nothing from the file system or the terminal and builds for wasm32.

use fmtron::{Config, FormatError, format_ron};

let config = Config::default().with_max_width(40);
let formatted = format_ron("Config(name: \"demo\", retries: [1, 2, 3], verbose: true)", &config)?;
assert_eq!(
    formatted,
    "Config(\n    name: \"demo\",\n    retries: [1, 2, 3],\n    verbose: true,\n)"
);

// A parse error says where and what, in RON terms.
let Err(FormatError::Parse(e)) = format_ron("(a: 1 b: 2)", &config) else {
    unreachable!()
};
assert_eq!((e.line(), e.column()), (1, 7));
assert_eq!(e.message(), "expected `,` or `)`, found `b`");

The output has no trailing line break; add line_ending(input) to write a file. Input from an untrusted source is safe to format: Config::max_nesting bounds the recursion, and the limits are errors, never panics.

The layout rules are described in the README, and what may change between releases in STABILITY.md.

Structs§

Config
Formatting settings. Start from Config::default and set fields, or chain the with_* methods:
ConfigParseError
Why the text of a fmt.ron is not a configuration: malformed RON, an unknown field, a value of the wrong type, or a setting the formatter cannot use. Its Display form names the position, as 3:5: Unexpected field named max_widht``.
FileConfig
The settings in a fmt.ron. None means “not set here”. Settings may be added in minor releases, so the struct cannot be built with a literal: start from FileConfig::default.
ParseError
Where and why input failed to parse as RON; the error inside FormatError::Parse.

Enums§

BlankLines
How blank lines in the input are treated.
FileConfigError
A fmt.ron that could not be read or parsed.
FormatError
The error type returned by format_ron. Every variant is a property of the input; formatting never panics.
StyleEdition
A set of layout rules, named by the year it was introduced, like rustfmt’s style editions. The rules of an edition do not change once it is released, apart from bug fixes; a change of style is a new edition, which stays opt-in until the next major version of fmtron. There is one edition so far. In a fmt.ron or on the command line it is written as the year: style_edition: 2026, --style-edition 2026.

Constants§

CONFIG_FILE_NAMES
The file names looked for in each directory, in order of preference.
DEFAULT_WIDTH
Default Config::max_width: rustfmt’s default, and close to how wide RON is written in practice (a sample of real-world files had a median longest line of 56 columns and a 75th percentile of 89).
MAX_INDENT
Upper bound on the indentation any output line may need: tab_size times the input’s nesting depth. Input needing more is rejected with FormatError::IndentTooWide rather than allocating absurd indentation (or overflowing and panicking) when max_tab and max_nesting are raised.
MAX_NESTING
Default maximum container-nesting depth. Guards against the stack overflow that pest’s recursive descent would otherwise hit on deeply nested input.
MAX_TAB
Default upper bound on Config::tab_size. Anything larger would emit pathological indentation; the CLI overrides this with --max-tab.

Functions§

format_ron
Formats RON text.
line_ending
The line ending of input: "\r\n" if its first line break is CRLF, otherwise "\n". The formatter emits this between output lines, including those inside block comments. A line break inside a string or char literal is part of its value, not of the layout: it is skipped here and kept as written.