Skip to main content

canonicalize_directives

Function canonicalize_directives 

Source
pub fn canonicalize_directives<'a, I>(
    directives: I,
    config: &FormatConfig,
) -> Result<String, CanonicalizeError>
Expand description

Render typed Beancount Directives in the canonical form emitted by format_source.

Two-pass pipeline:

  1. Synthesize a source string via the typed-directive emitter in rustledger_core::format::format_directives. That emitter is Directive → text; its output is bean-format- style, parser-clean, and used here purely as an intermediate.
  2. Re-parse the synthesized text. If the legacy emitter produced something the new parser cannot fully accept, return CanonicalizeError::ReparseFailed rather than silently emitting the recoverable subset — that silent-loss failure mode is what the older crates/rustledger/tests/ format_compat.rs (deleted in phase 4.1, distinct from the phase 4.2 file-pair suite at crates/rustledger-parser/ tests/format_compat/) used to guard against. The new file- pair suite exercises format_source, not this two-pass shim; a future change to canonicalize_directives’s error semantics needs its own dedicated regression test.
  3. Run the re-parsed text through format_source for the canonical pass.

Single source of truth for the synthesize → canonicalize shim. Every consumer that builds a typed Directive in memory and wants canonical text — rledger add, rledger extract, the FFI format.entry / format.entries endpoints — should call this function instead of reinventing the pipeline.