Skip to main content

preview

Function preview 

Source
pub fn preview(
    base_text: &str,
    changes: &[Change],
) -> Result<Preview, PreviewError>
Expand description

Computes what applying changes to base_text would produce.

The function is pure: it reads only its arguments and returns the same bytes for the same input. Changes are applied in schema order, whatever order they are given in. Each edit replaces only the value: comments, blank lines, key order and the decoration around the value (including a trailing comment on the same line) are kept. A field absent from the base is appended to its table, and a missing table is appended to the document. A change that leaves the field’s effective value unchanged (counting defaults) is dropped and does not touch the text.

§Line endings

The edited text uses the base’s line ending: CRLF when the first line break of base_text is CRLF, LF otherwise (including a base with no line break). Every line of the edited text, inserted or kept, ends that way, so a CRLF document stays entirely CRLF and its diff shows only the lines an edit changed. A base that mixes endings is the one case where unedited lines can change: they are rewritten with the ending of its first line break. When no change takes effect the base is returned verbatim, mixed endings included.

The base need not be valid, only parseable, so a preview can repair an invalid document. A change value of the wrong type for its field is written as given and then reported in Preview::errors.

§Editing a list

A list is edited element by element, so the comments inside it stay with what they describe:

  • Ownership. An element owns the comment lines directly above it (everything between the previous element’s line and itself) and the comment ending its own line; when several elements share a line, that comment belongs to the last of them. The list itself owns a comment on the [ line, the comment lines before the closing ], and everything outside the brackets. In an empty list every comment is the list’s.
  • Matching. Old and new elements are matched by value, each new element taking the first old element with the same text that is not yet taken. Reordering therefore moves an element together with its comments, and of two equal elements the earlier keeps its comments.
  • Survivors keep their comments exactly once. A removed element’s comments are removed with it; replacing an element is removing one and adding another. New elements own no comment and take the surrounding style: in a multiline list a line of their own at the existing indentation, in a single-line list the spacing of their position.
  • The list’s own comments are always kept exactly once, including when the list is cleared or filled from empty. A comment line is never joined to another element’s line: one that began a line still does.
  • An unchanged list is left byte-identical.
  • The trailing comma is kept; filling an empty multiline list adds one, and clearing a list removes it and writes [] when only whitespace would remain between the brackets.

§Errors

Returns PreviewError::TooLarge if base_text, or the text the changes produce, is longer than MAX_DOCUMENT_BYTES (checked before parsing the base and before diffing the result), PreviewError::Syntax if base_text is not valid TOML, PreviewError::UnknownPath if a change names a path not in the schema, PreviewError::DuplicatePath if two changes name the same path, and PreviewError::NotEditable if the base holds a non-table where the field’s table belongs, or a table where the field’s value belongs.

§Examples

use stratum_node_config::{preview, ApplyClass, Change, Value};

let base = "schema_version = 1\n\n[limits]\n# Per-request cap.\nmax_body_bytes = 262144\n";
let p = preview(base, &[
    Change::new("limits.max_body_bytes", 65_536),
    Change::new("node.name", "alpha"),
])?;
assert!(p.is_valid());
assert!(p.text().contains("# Per-request cap.\nmax_body_bytes = 65536\n"));
assert_eq!(p.changes()[0].path(), "node.name"); // schema order
assert_eq!(p.changes()[1].old(), Some(&Value::Integer(262_144)));
assert_eq!(p.strictest_apply_class(), Some(ApplyClass::Restart));
assert!(p.diff().contains("-max_body_bytes = 262144\n+max_body_bytes = 65536\n"));