diff --git a/Cargo.lock b/Cargo.lock index 1b779bf9..a1964393 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3431,6 +3431,10 @@ dependencies = [ "windows-icons 0.1.0 (git+https://github.com/LGUG2Z/windows-icons?rev=d67cc9920aa9b4883393e411fb4fa2ddd4c498b5)", ] +[[package]] +name = "komorebi-bar-core" +version = "0.1.42" + [[package]] name = "komorebi-client" version = "0.1.42" diff --git a/Cargo.toml b/Cargo.toml index c4cd9097..290284f0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ members = [ "komorebi-themes", "crates/komorebi-protocol", "crates/komorebi-transport", + "crates/komorebi-bar-core", "komorebi-shortcuts", "e2e/guest/agent", "e2e/guest/testwin", diff --git a/crates/komorebi-bar-core/Cargo.toml b/crates/komorebi-bar-core/Cargo.toml new file mode 100644 index 00000000..f5edd858 --- /dev/null +++ b/crates/komorebi-bar-core/Cargo.toml @@ -0,0 +1,5 @@ +[package] +name = "komorebi-bar-core" +version = "0.1.42" +description = "The status bar shared by the Windows and macOS komorebi bars: configuration, widgets and the platform seam" +edition = "2024" diff --git a/crates/komorebi-bar-core/src/lib.rs b/crates/komorebi-bar-core/src/lib.rs new file mode 100644 index 00000000..fa91b405 --- /dev/null +++ b/crates/komorebi-bar-core/src/lib.rs @@ -0,0 +1,6 @@ +//! The code the Windows and the macOS `komorebi-bar` share. +//! +//! The crate is being filled in by pure moves, one file at a time, each held to the schema +//! oracle in `tests/schema_oracle.rs`: the checked-in `schema.bar.json` of both bars must not +//! change unless a reviewed revision says so. +#![warn(clippy::all)] diff --git a/crates/komorebi-bar-core/tests/schema_oracle.rs b/crates/komorebi-bar-core/tests/schema_oracle.rs new file mode 100644 index 00000000..48380b84 --- /dev/null +++ b/crates/komorebi-bar-core/tests/schema_oracle.rs @@ -0,0 +1,78 @@ +//! The schema oracle of the shared bar. +//! +//! `schema.bar.json` (Windows, at the root) and `komorebi-for-mac/schema.bar.json` are what +//! `komorebi-bar --schema` prints. They are pinned here by size and a FNV-1a hash of their bytes, +//! so that a move or a convergence of bar code cannot change them unnoticed. A reviewed change +//! to either file updates the matching pin in the same revision. +//! +//! The bar binaries cannot run on Linux (Windows and AppKit APIs), so this test cannot +//! regenerate the schema until the configuration types live in this crate; from then on it +//! compares the generated text with the file instead of a hash. + +use std::path::PathBuf; + +struct Pin { + file: &'static str, + lines: usize, + bytes: usize, + fnv1a: u64, +} + +const PINS: [Pin; 2] = [ + Pin { + file: "schema.bar.json", + lines: 9_090, + bytes: 248_529, + fnv1a: 0x5573_0134_1968_bff9, + }, + Pin { + file: "komorebi-for-mac/schema.bar.json", + lines: 3_774, + bytes: 116_925, + fnv1a: 0xc99e_0294_9424_e5d7, + }, +]; + +fn fnv1a(bytes: &[u8]) -> u64 { + bytes.iter().fold(0xcbf2_9ce4_8422_2325, |hash, byte| { + (hash ^ u64::from(*byte)).wrapping_mul(0x0100_0000_01b3) + }) +} + +fn read(file: &str) -> Vec { + let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("../..") + .join(file); + std::fs::read(&path).unwrap_or_else(|error| panic!("{}: {error}", path.display())) +} + +#[test] +fn the_checked_in_bar_schemas_are_the_pinned_ones() { + for pin in PINS { + let bytes = read(pin.file); + let lines = bytes.iter().filter(|byte| **byte == b'\n').count(); + assert_eq!(lines, pin.lines, "{}: line count", pin.file); + assert_eq!(bytes.len(), pin.bytes, "{}: size", pin.file); + assert_eq!( + fnv1a(&bytes), + pin.fnv1a, + "{}: FNV-1a (is {:#018x})", + pin.file, + fnv1a(&bytes) + ); + } +} + +#[test] +fn the_checked_in_bar_schemas_are_json_schemas_of_komobar_config() { + for pin in PINS { + let text = String::from_utf8(read(pin.file)).expect("utf-8"); + assert!(text.starts_with("{\n \"$schema\""), "{}", pin.file); + assert!( + text.contains("\"title\": \"KomobarConfig\""), + "{}", + pin.file + ); + assert!(text.ends_with("}\n"), "{}", pin.file); + } +}