Skip to main content

jsslint_core/bib/
debug.rs

1//! Debug dump matching `tools/dump_bib_entries.py`'s format exactly —
2//! see that script's module doc for the comparison methodology.
3
4use super::model::Library;
5
6fn entry_line(entry: &super::model::Entry) -> String {
7    let mut fields: Vec<(String, String)> = entry
8        .fields
9        .iter()
10        .map(|f| (f.key.to_ascii_lowercase(), f.value.trim().to_string()))
11        .collect();
12    fields.sort();
13    let field_str = fields
14        .iter()
15        .map(|(k, v)| format!("{k}={v}"))
16        .collect::<Vec<_>>()
17        .join("|");
18    format!(
19        "ENTRY\t{}\t{}\t{}\t{}",
20        entry.key,
21        entry.entry_type.to_ascii_lowercase(),
22        entry.start_line,
23        field_str
24    )
25}
26
27pub fn dump(library: &Library) -> String {
28    let mut lines: Vec<String> = library.entries.iter().map(entry_line).collect();
29
30    let mut dup_keys: Vec<String> = library
31        .duplicate_block_keys
32        .iter()
33        .map(|b| format!("DUPKEY\t{}\t{}", b.key, b.start_line))
34        .collect();
35    dup_keys.sort();
36    lines.extend(dup_keys);
37
38    let mut dup_fields: Vec<String> = library
39        .duplicate_field_keys
40        .iter()
41        .map(|b| format!("DUPFIELD\t{}\t{}", b.start_line, b.duplicate_keys.join(",")))
42        .collect();
43    dup_fields.sort();
44    lines.extend(dup_fields);
45
46    if lines.is_empty() {
47        String::new()
48    } else {
49        lines.join("\n") + "\n"
50    }
51}