aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / crates / allen-design-tokens / src / main.rs
| 1 | //! allen-design-tokens: `generate` writes the exports from DESIGN.md; `check` fails if they are stale or a |
| 2 | //! contrast floor is missed. |
| 3 | //! |
| 4 | //! cargo run -p allen-design-tokens -- generate [DESIGN.md] [OUT_DIR] |
| 5 | //! cargo run -p allen-design-tokens -- check [DESIGN.md] [OUT_DIR] |
| 6 | |
| 7 | use std::path::PathBuf; |
| 8 | use std::process::ExitCode; |
| 9 | |
| 10 | fn repo_root() -> PathBuf { |
| 11 | PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../..") |
| 12 | } |
| 13 | |
| 14 | fn main() -> ExitCode { |
| 15 | let args: Vec<String> = std::env::args().skip(1).collect(); |
| 16 | let mode = args.first().map(String::as_str).unwrap_or("check"); |
| 17 | let design = args |
| 18 | .get(1) |
| 19 | .map(PathBuf::from) |
| 20 | .unwrap_or_else(|| repo_root().join("docs/design/DESIGN.md")); |
| 21 | let out_dir = args |
| 22 | .get(2) |
| 23 | .map(PathBuf::from) |
| 24 | .unwrap_or_else(|| repo_root().join("docs/design/tokens")); |
| 25 | if !matches!(mode, "generate" | "check") { |
| 26 | eprintln!("usage: allen-design-tokens generate|check [DESIGN.md] [OUT_DIR]"); |
| 27 | return ExitCode::from(2); |
| 28 | } |
| 29 | let text = match std::fs::read_to_string(&design) { |
| 30 | Ok(t) => t, |
| 31 | Err(e) => { |
| 32 | eprintln!("allen-design-tokens: cannot read {}: {e}", design.display()); |
| 33 | return ExitCode::FAILURE; |
| 34 | } |
| 35 | }; |
| 36 | let tokens = match allen_design_tokens::parse(&text) { |
| 37 | Ok(t) => t, |
| 38 | Err(e) => { |
| 39 | eprintln!("allen-design-tokens: {}: {e}", design.display()); |
| 40 | return ExitCode::FAILURE; |
| 41 | } |
| 42 | }; |
| 43 | let results = match allen_design_tokens::check_contrast(&tokens) { |
| 44 | Ok(r) => r, |
| 45 | Err(e) => { |
| 46 | eprintln!("allen-design-tokens: {e}"); |
| 47 | return ExitCode::FAILURE; |
| 48 | } |
| 49 | }; |
| 50 | print!("{}", allen_design_tokens::contrast_report(&results)); |
| 51 | let mut ok = true; |
| 52 | for r in results.iter().filter(|r| r.gated && !r.passes()) { |
| 53 | eprintln!( |
| 54 | "allen-design-tokens: FAIL contrast {} on {}: {:.2}:1, floor {:.1}", |
| 55 | r.fg, r.bg, r.ratio, r.floor |
| 56 | ); |
| 57 | ok = false; |
| 58 | } |
| 59 | let outputs = match allen_design_tokens::outputs(&tokens) { |
| 60 | Ok(o) => o, |
| 61 | Err(e) => { |
| 62 | eprintln!("allen-design-tokens: {e}"); |
| 63 | return ExitCode::FAILURE; |
| 64 | } |
| 65 | }; |
| 66 | for (name, body) in outputs { |
| 67 | let path = out_dir.join(name); |
| 68 | if mode == "generate" { |
| 69 | if let Err(e) = |
| 70 | std::fs::create_dir_all(&out_dir).and_then(|()| std::fs::write(&path, body)) |
| 71 | { |
| 72 | eprintln!("allen-design-tokens: cannot write {}: {e}", path.display()); |
| 73 | return ExitCode::FAILURE; |
| 74 | } |
| 75 | println!("wrote {}", path.display()); |
| 76 | } else if std::fs::read_to_string(&path).ok().as_deref() != Some(body.as_str()) { |
| 77 | eprintln!( |
| 78 | "allen-design-tokens: {} is stale; run `cargo run -p allen-design-tokens -- generate`", |
| 79 | path.display() |
| 80 | ); |
| 81 | ok = false; |
| 82 | } |
| 83 | } |
| 84 | if ok { |
| 85 | ExitCode::SUCCESS |
| 86 | } else { |
| 87 | ExitCode::FAILURE |
| 88 | } |
| 89 | } |