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::defaultand set fields, or chain thewith_*methods: - Config
Parse Error - Why the text of a
fmt.ronis not a configuration: malformed RON, an unknown field, a value of the wrong type, or a setting the formatter cannot use. ItsDisplayform names the position, as3:5: Unexpected field namedmax_widht``. - File
Config - The settings in a
fmt.ron.Nonemeans “not set here”. Settings may be added in minor releases, so the struct cannot be built with a literal: start fromFileConfig::default. - Parse
Error - Where and why input failed to parse as RON; the error inside
FormatError::Parse.
Enums§
- Blank
Lines - How blank lines in the input are treated.
- File
Config Error - A
fmt.ronthat could not be read or parsed. - Format
Error - The error type returned by
format_ron. Every variant is a property of the input; formatting never panics. - Style
Edition - 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.ronor 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_sizetimes the input’s nesting depth. Input needing more is rejected withFormatError::IndentTooWiderather than allocating absurd indentation (or overflowing and panicking) whenmax_tabandmax_nestingare 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.