# Hex keyboard core The keyboard's platform-independent logic in Rust, shared by the iOS keyboard extension (`ios/HEXKeyboard`, through `ios/HEXShared`) and the Android IME (`android/app`). Each OS keeps its own layer on top: views, touches, the text field, haptics, dictation, storage, and anything only the OS can answer (Apple's spelling checker, the system lexicon). - `core/` (`hex-keyboard-core`): safe Rust. It never reads a clock, a file or the OS; the host passes in times, key geometry and data. - `ffi/` (`hex-keyboard-ffi`, library `hex_keyboard`): the C interface in `include/hex_keyboard.h` (Swift imports it as `HexKeyboardCore`), and with the `jni` feature the Android entry points. All `unsafe` code is here; every entry point catches panics. This is its own Cargo workspace with its own `Cargo.lock` and pinned toolchain (`rust-toolchain.toml`), so the desktop workspace never builds it. Pieces move here one at a time from the Swift keyboard (`ios/HEXShared/FullKeyboard`), each proven identical to the Swift it replaces before iOS switches to it. Today: the typing model reader and Swipe typing's decoder. Tune those here, not in Swift. ## Setup Install rustup ([rustup.rs](https://rustup.rs) or `brew install rustup`): in this folder it uses the toolchain, components and targets `rust-toolchain.toml` pins (`rustup toolchain install` fetches them ahead). iOS's build (`ios/scripts/build-keyboard-core.sh`, HEXKeyboard's first build phase), Xcode Cloud's `ci_post_clone.sh` and Android's debug build find it the same way (`tools/rustup-env.sh`): on PATH, in `CARGO_HOME` (by default `~/.cargo`), or Homebrew's (`opt/rustup/bin` or `bin` under `HOMEBREW_PREFIX`), taking only a cargo that runs the pinned toolchain here. Android also needs `cargo install cargo-ndk`. ```sh cd crates/keyboard cargo test --all-features cargo clippy --all-targets --all-features -- -D warnings ``` `--all-features` includes the `jni` feature, so the Android entry points (`ffi/src/jni.rs`) are checked too. Tune Swipe typing here, in `core/src/swipe`: iOS's `SwipeDecoder` is a wrapper of it. The decoder was ported from the Swift one it replaced, and proven to read every swipe as it did (the same candidates in the same order, costs equal bit for bit) on 200,000 generated swipes, in a separate run and in one process, and on recorded FUTO swipes typed through the keyboard. Since then it computes exp, log, atan2, cos and hypot with the pure-Rust `libm` crate rather than each platform's own, so iOS and Android read every swipe alike, bit for bit; against Apple's libm, 2 of those 200,000 readings changed order (1-ulp near-ties) and about 2% of costs moved by 1 or 2 ulps. The committed `core/tests/fixtures/swipe-parity.jsonl` holds 800 of those swipes per language with the Swift decoder's words, order and `scored`, and the core's costs (its header's `"costs"`): `core/tests/swipe_parity.rs` checks the core against it bit for bit (and, with `HEX_SWIPE_PARITY=`, a larger export; on one the Swift decoder recorded, changed costs are counted rather than failed), iOS's `SwipeFixtureTests` checks the shipped path through the C interface, and Android's tests the JNI path on the host and on an emulator. A change that reads any swipe differently fails both: if it's intended, refresh the fixture from the app's decoder (the simulator writes to the Mac's `/tmp`) and commit it with the change: ```sh cd ios && xcodegen generate TEST_RUNNER_HEX_SWIPE_PARITY_OUT=/tmp/swipe-parity.jsonl TEST_RUNNER_HEX_SWIPE_PARITY_COUNT=800 \ xcodebuild test -project HEXMobile.xcodeproj -scheme HEXMobile \ -destination 'platform=iOS Simulator,name=' \ -only-testing:HEXMobileTests/SwipeParityExportTests cp /tmp/swipe-parity.jsonl ../crates/keyboard/core/tests/fixtures/swipe-parity.jsonl cd ../crates/keyboard && cargo test --release --test swipe_parity -- --nocapture ``` `TEST_RUNNER_HEX_SWIPE_PARITY_TIMING=1` adds each read's time; a larger count makes a larger file for `HEX_SWIPE_PARITY`. What a swipe types is the core's too (`core/src/swipe/typing.rs`): the space before the word and the capital it brings, the word's casing (caps lock, shift, a name the dictionary knows only capitalised, your own words' capitals, Auto-capitalization off), the strip's alternatives with text replacements, the rare-name ranking and the slid tap. The host answers what only it knows: whether the dictionary knows a word (a callback), your text replacements and words, and the field, shift and auto-capitalization state with the text before the cursor; which readings may be offered at all stays its filter. Ported from iOS's Swift and proven alike on 1,000,000 generated cases in one process, it's held to `core/tests/fixtures/swipe-rules.jsonl` (5,000 of those cases) by `core/tests/swipe_rules.rs` and iOS's `SwipeRulesFixtureTests`. Refresh it for an intended change the same way: ```sh TEST_RUNNER_HEX_SWIPE_RULES_OUT=/tmp/swipe-rules.jsonl TEST_RUNNER_HEX_SWIPE_RULES_COUNT=5000 \ xcodebuild test -project HEXMobile.xcodeproj -scheme HEXMobile \ -destination 'platform=iOS Simulator,name=' \ -only-testing:HEXMobileTests/SwipeRulesFixtureTests/testExportSwipeRulesFixture cp /tmp/swipe-rules.jsonl ../crates/keyboard/core/tests/fixtures/swipe-rules.jsonl ```