Skip to main content

render_lossless

Function render_lossless 

Source
pub fn render_lossless(
    source: &str,
    tokens: &[Token],
    trivia: &[Trivia],
) -> Result<String, RenderLosslessError>
Expand description

Reconstruct the source from token and trivia spans.

Ok only when the spans tile the file — every non-whitespace byte inside exactly one token or comment span — in which case the result is byte-identical to source. This is the lossless-trivia oracle for the formatter.

§Errors

Returns RenderLosslessError when token/trivia spans overlap, leave gaps, or extend past the end of source.

use daml_parser::lexer::{lex_with_trivia, render_lossless};

let src = "x = 1 -- comment\n";
let lexed = lex_with_trivia(src);
assert!(render_lossless(src, &lexed.tokens, &lexed.trivia).is_ok());

let mut broken_trivia = lexed.trivia.clone();
broken_trivia.clear();
assert!(render_lossless(src, &lexed.tokens, &broken_trivia).is_err());