Skip to main content

try_format_source

Function try_format_source 

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

Like format_source, but returns the parse errors instead of silently formatting around them.

format_source is intentionally infallible — the canonical formatter must still emit something for a file the parser could only recover from. Tooling that wants to refuse to rewrite a file with parse errors (the rledger format CLI, the LSP format handler) previously had to call parse out-of-band, inspect errors, then call format_source on the SAME input — a contract two functions cooperated on implicitly, and the kind of pairing a future caller could easily forget. This helper makes the contract explicit.

Returns Ok(formatted) if and only if parse(source).errors would be empty. Otherwise returns the parse errors verbatim, in the same order the parser emitted them.

§Errors

Returns Err(Vec<ParseError>) containing every parse error the underlying parse call would surface for source. The caller decides whether to abort, render the errors, or fall back to a non-canonical pass.