Skip to main content

normalize

Function normalize 

Source
pub fn normalize(root: &Root) -> Result<NormalizeTable, NormalizeError>
Expand description

Normalize every binding group in a parsed tree.

Returns the plans for groups that actually need one. A group with no duplicate static key and no dotted path is NOT recorded — consumers keep their existing path for it, which is what bounds this pass’s blast radius.

§Errors

Returns the first NormalizeError in source order, matching nix’s parse-time rejection.

Examples found in repository?
examples/scan.rs (line 76)
49fn main() {
50    let roots: Vec<PathBuf> = std::env::args().skip(1).map(PathBuf::from).collect();
51    if roots.is_empty() {
52        eprintln!("usage: scan <dir> [<dir>…]");
53        std::process::exit(2);
54    }
55
56    let mut files = Vec::new();
57    for r in &roots {
58        nix_files(r, &mut files);
59    }
60
61    let (mut unparseable, mut clean, mut planned, mut rejected) = (0usize, 0usize, 0usize, 0usize);
62    let mut groups = 0usize;
63    let mut rejects: Vec<(PathBuf, String)> = Vec::new();
64
65    for f in &files {
66        let Ok(src) = std::fs::read_to_string(f) else {
67            continue;
68        };
69        let parse = rnix::Root::parse(&src);
70        if !parse.errors().is_empty() {
71            // rnix could not parse it — not this pass's business, and counted
72            // separately so it can never be mistaken for a clean result.
73            unparseable += 1;
74            continue;
75        }
76        match sui_normalize::normalize(&parse.tree()) {
77            Ok(table) if table.is_empty() => clean += 1,
78            Ok(table) => {
79                planned += 1;
80                groups += table.len();
81            }
82            Err(e) => {
83                rejected += 1;
84                rejects.push((f.clone(), e.to_string()));
85            }
86        }
87    }
88
89    println!("scanned    {}", files.len());
90    println!("  clean      {clean}   (no duplicate key, no dotted path — untouched)");
91    println!("  planned    {planned}   ({groups} binding groups — THE BLAST RADIUS)");
92    println!("  rejected   {rejected}   (must be 0 before the rejection tier flips)");
93    println!("  unparseable {unparseable}   (rnix could not read; not this pass's business)");
94
95    for (p, e) in rejects.iter().take(25) {
96        println!("  REJECT {}: {e}", p.display());
97    }
98    if rejects.len() > 25 {
99        println!("  … and {} more", rejects.len() - 25);
100    }
101}