Skip to main content

format

Function format 

Source
pub fn format(source: &str) -> Result<String, Vec<Diagnostic>>
Expand description

Formats source into the canonical layout.

The layout indents by two spaces and puts one statement, field, variant or arm on each line. Lists, maps, argument lists and parameter lists stay on one line when they fit in 100 columns, and otherwise put one element on each line with a trailing comma. Runs of consecutive attributes, map entries, let bindings and match arms align their = or =>, and runs of struct fields align their types, unless the longest key of the run is longer than 24 characters. Bodies, struct declarations and sum types that are written on one line stay on one line when they fit.

Formatting never changes what the source means and never reorders anything. Comments are kept, blank lines between elements are kept and collapsed to one, and strings are kept byte for byte, including multi-line strings and the expressions they interpolate. Formatting the result again returns it unchanged.

§Errors

Returns the lexical and syntax errors of source when it has any. A source with errors is not formatted.

§Examples

let source = "system {\n  hostname=\"forge\"\n  init = supervisor, timezone = \"UTC\"\n}\n";
let formatted = stav::format(source).expect("the source has no errors");
assert_eq!(
    formatted,
    "system {\n  hostname = \"forge\"\n  init     = supervisor\n  timezone = \"UTC\"\n}\n",
);