Skip to main content

normal_form

Function normal_form 

Source
pub fn normal_form(text: &str) -> String
Expand description

The normal form of a file in the layer’s YAML subset, which the policy hash covers in place of its bytes: trailing spaces, tabs and carriage returns dropped; blank lines and whole-line comments dropped; a trailing comment (a # after a space or tab) dropped from a line that holds no quote, since inside a quoted scalar # may be text. Indentation, order and every value are kept, and every line ends in a newline.

It is the shell tool’s mj_yaml_normal (lib/common.sh), line for line: the two must agree, or each renderer would call the other’s bootstraps stale. The subset refuses block scalars, so no # line is ever content.

use majordomus_cli::policy::normal_form;

let edited = "# a new comment\nversion: 1   # the schema\n\ncontext:\n  name: \"a #b\"\n";
assert_eq!(normal_form(edited), "version: 1\ncontext:\n  name: \"a #b\"\n");
assert_eq!(normal_form(edited), normal_form("version: 1\ncontext:\n  name: \"a #b\"\n"));
assert_ne!(normal_form("a: 1\n"), normal_form("a: 2\n"));