Skip to main content

keyrill_core/
lib.rs

1//! Portable engine for correcting English/Russian keyboard-layout mistakes.
2//!
3//! A layout mistake is a word typed with the wrong input mode active: the keys of `привет`
4//! pressed in English mode give `ghbdtn`, and the keys of `hello` pressed in Russian mode give
5//! `руддщ`. The keys are right and only their reading is wrong, so the fix is to read the same
6//! key sequence in the other layout, and the hard part is deciding when to do it. While a word
7//! is typed, the native adapter keeps it in the host as an owned composition, marked text that
8//! Keyrill inserted and can still replace; at Space the engine decides whether the word reads
9//! better in the other layout.
10//!
11//! The engine decides and proposes; it never reads or edits a document. A native input-method
12//! adapter owns the text, performs at most one write per [`session::Plan`], verifies the result
13//! and acknowledges it. `ARCHITECTURE.md` at the repository root describes that contract and the
14//! safety invariants around it.
15//!
16//! | Module | Role |
17//! |---|---|
18//! | [`mapping`] | EN/RU key-position maps over UTF-16 |
19//! | [`model`] | Embedded word frequencies and character chains |
20//! | [`inference`] | Whether a word was typed in the wrong layout |
21//! | [`vocabulary`] | Vim `.add` word lists and immutable snapshots |
22//! | [`session`] | Word state machine with acknowledged proposals |
23//!
24//! # Example
25//!
26//! Type `ghbdtn` and Space in English mode, acting as an adapter that verifies every write:
27//!
28//! ```
29//! use keyrill_core::inference::{Dictionaries, Policy};
30//! use keyrill_core::model::Model;
31//! use keyrill_core::session::{Context, Event, Mode, Outcome, Session};
32//! use keyrill_core::vocabulary::Vocabulary;
33//!
34//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
35//! let model = Model::embedded()?;
36//! let empty = Vocabulary::parse(b"")?;
37//! let dictionaries = Dictionaries { en: Some(&empty), ru: Some(&empty), learned: &[] };
38//! let mut session = Session::default();
39//!
40//! let mut marked = Vec::new();
41//! for unit in "ghbdtn ".encode_utf16() {
42//!     let event = Event::Text(vec![unit]);
43//!     let plan = session.propose_with_inference(
44//!         &event, Mode::En, Context::Boundary, &model, dictionaries, Policy::default(),
45//!     )?;
46//!     // An adapter checks that its composition holds `plan.expected`, writes `plan.text`,
47//!     // reads it back, and only then acknowledges.
48//!     marked = plan.text.clone();
49//!     session.acknowledge(plan.id, Outcome::Verified)?;
50//! }
51//! assert_eq!(String::from_utf16(&marked)?, "привет ");
52//! # Ok(())
53//! # }
54//! ```
55#![forbid(unsafe_code)]
56
57pub mod inference;
58pub mod mapping;
59pub mod model;
60pub mod session;
61pub mod vocabulary;