oxedyne/fe2o3/fe2o3_jdat/src/lint.rs
4.0 KiB, 1 run
created by r1870400018:75197, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! The `.jdat` file of the mutable-literal gate's ratchet baseline (`fe2o3_text::lint::mutable`). |
| 2 | //! The gate lives in fe2o3_text, which this crate depends on, so the file is read and written here. |
| 3 | //! |
| 4 | //! ```text |
| 5 | //! { |
| 6 | //! "entries": [ { "file": "fe2o3_net/src/llm.rs", "class": "host", "text": "…", "count": 1 } ], |
| 7 | //! "allow": [ { "file": "…", "class": "named", "text": "…", "why": "Generated tables." } ], |
| 8 | //! } |
| 9 | //! ``` |
| 10 | |
| 11 | use crate::prelude::*; |
| 12 | |
| 13 | use oxedyne_fe2o3_core::prelude::*; |
| 14 | use oxedyne_fe2o3_text::{ |
| 15 | lint::mutable::{ |
| 16 | self, |
| 17 | Allow, |
| 18 | Baseline, |
| 19 | Class, |
| 20 | Key, |
| 21 | Report, |
| 22 | }, |
| 23 | }; |
| 24 | |
| 25 | use std::{ |
| 26 | fs, |
| 27 | path::Path, |
| 28 | }; |
| 29 | |
| 30 | |
| 31 | /// Reads a baseline file. An absent file is an empty baseline, so a new tree starts at zero debt. |
| 32 | pub fn load_baseline(path: &Path) -> Outcome<Baseline> { |
| 33 | if !path.exists() { |
| 34 | return Ok(Baseline::default()); |
| 35 | } |
| 36 | let s = match fs::read_to_string(path) { |
| 37 | Ok(s) => s, |
| 38 | Err(e) => return Err(err!(e, "While reading baseline {}.", path.display(); IO, File, Read)), |
| 39 | }; |
| 40 | let dat = res!(Dat::decode_string(s)); |
| 41 | baseline_from_dat(&dat) |
| 42 | } |
| 43 | |
| 44 | /// Writes a baseline file, sorted by key so a migration's diff is only its own lines. |
| 45 | pub fn save_baseline(base: &Baseline, path: &Path) -> Outcome<()> { |
| 46 | let mut text = res!(baseline_to_dat(base).jdat_to_lines("\t")); |
| 47 | text.push('\n'); |
| 48 | match fs::write(path, text) { |
| 49 | Ok(()) => Ok(()), |
| 50 | Err(e) => Err(err!(e, "While writing baseline {}.", path.display(); IO, File, Write)), |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /// Scans the tree under `root` and checks it against the baseline file at `baseline_path`. |
| 55 | pub fn check_tree(root: &Path, baseline_path: &Path) -> Outcome<Report> { |
| 56 | let base = res!(load_baseline(baseline_path)); |
| 57 | mutable::check_tree(root, &base) |
| 58 | } |
| 59 | |
| 60 | pub fn baseline_to_dat(base: &Baseline) -> Dat { |
| 61 | let entries: Vec<Dat> = base.entries.iter().map(|(k, n)| mapdat!{ |
| 62 | "file" => k.file.clone(), |
| 63 | "class" => k.class.name(), |
| 64 | "text" => k.text.clone(), |
| 65 | "count" => *n as u64, |
| 66 | }).collect(); |
| 67 | let allow: Vec<Dat> = base.allow.iter().map(|a| mapdat!{ |
| 68 | "file" => a.file.clone(), |
| 69 | "class" => a.class.name(), |
| 70 | "text" => a.text.clone(), |
| 71 | "why" => a.why.clone(), |
| 72 | }).collect(); |
| 73 | mapdat!{ |
| 74 | "entries" => Dat::List(entries), |
| 75 | "allow" => Dat::List(allow), |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | pub fn baseline_from_dat(dat: &Dat) -> Outcome<Baseline> { |
| 80 | let mut base = Baseline::default(); |
| 81 | for (i, e) in res!(dat.map_get_list(&dat!("entries"))).iter().enumerate() { |
| 82 | let (key, n) = match key_from_dat(e).and_then(|k| e.map_get_u64(&dat!("count")).map(|n| (k, n))) { |
| 83 | Ok(v) => v, |
| 84 | Err(e) => return Err(err!(e, "In baseline entry {}.", i; Invalid, Input)), |
| 85 | }; |
| 86 | *base.entries.entry(key).or_insert(0) += n as usize; |
| 87 | } |
| 88 | if let Some(list) = res!(dat.map_get(&dat!("allow"))) { |
| 89 | let list = match list { |
| 90 | Dat::List(v) => v, |
| 91 | _ => return Err(err!( |
| 92 | "Baseline 'allow' is a {:?}, expected a list.", list.kind(); Invalid, Input)), |
| 93 | }; |
| 94 | for (i, a) in list.iter().enumerate() { |
| 95 | let key = match key_from_dat(a) { |
| 96 | Ok(k) => k, |
| 97 | Err(e) => return Err(err!(e, "In baseline allow entry {}.", i; Invalid, Input)), |
| 98 | }; |
| 99 | // A missing why loads as empty, so the gate reports it rather than the load failing. |
| 100 | let why = match res!(a.map_get(&dat!("why"))) { |
| 101 | Some(Dat::Str(s)) => s.clone(), |
| 102 | _ => String::new(), |
| 103 | }; |
| 104 | base.allow.push(Allow { file: key.file, class: key.class, text: key.text, why }); |
| 105 | } |
| 106 | } |
| 107 | Ok(base) |
| 108 | } |
| 109 | |
| 110 | fn key_from_dat(dat: &Dat) -> Outcome<Key> { |
| 111 | Ok(Key { |
| 112 | file: res!(dat.map_get_string(&dat!("file"))), |
| 113 | class: res!(Class::from_name(&res!(dat.map_get_string(&dat!("class"))))), |
| 114 | text: res!(dat.map_get_string(&dat!("text"))), |
| 115 | }) |
| 116 | } |