pub struct Preprocessor { /* private fields */ }
Expand description
The main structure of the preprocessor.
Implementations§
Source§impl Preprocessor
impl Preprocessor
Sourcepub fn new(memory: Rc<Node>, buffer_size: usize) -> Self
pub fn new(memory: Rc<Node>, buffer_size: usize) -> Self
Initializes a new preprocessor.
The preprocessor needs a memory to operate. You have two options to build this memory.
- Use the [
afrim-memory
] crate. - Use the
utils
module.
It also needs you set the capacity of his cursor. We recommend to set a capacity equal or greater than N times the maximun sequence length that you want to handle. Where N is the number of sequences that you want track in the cursor.
Note that the cursor is the internal memory of the afrim_preprocessor
.
§Example
use afrim_preprocessor::{Preprocessor, utils};
use std::rc::Rc;
// We prepare the memory.
let data = utils::load_data("uuaf3 ʉ̄ɑ̄");
let text_buffer = utils::build_map(data);
let memory = Rc::new(text_buffer);
// We initialize our preprocessor.
let preprocessor = Preprocessor::new(memory, 8);
Sourcepub fn process(&mut self, event: KeyboardEvent) -> (bool, bool)
pub fn process(&mut self, event: KeyboardEvent) -> (bool, bool)
Preprocess the keyboard input event and returns infos on his internal changes (change on the cursor and/or something to commit).
It’s useful when you process keyboard input events in bulk. Whether there is something that you want to do based on this information, you can decide how to continue.
§Example
use afrim_preprocessor::{Command, Preprocessor, utils};
use keyboard_types::{Key::Character, KeyboardEvent};
use std::{collections::VecDeque, rc::Rc};
// We prepare the memory.
let data = utils::load_data("i3 ī");
let text_buffer = utils::build_map(data);
let memory = Rc::new(text_buffer);
let mut preprocessor = Preprocessor::new(memory, 8);
// We process the input.
// let input = "si3";
let info = preprocessor.process(KeyboardEvent {
key: Character("s".to_string()),
..Default::default()
});
assert_eq!(info, (true, false));
let info = preprocessor.process(KeyboardEvent {
key: Character("i".to_string()),
..Default::default()
});
assert_eq!(info, (true, false));
let info = preprocessor.process(KeyboardEvent {
key: Character("3".to_string()),
..Default::default()
});
assert_eq!(info, (true, true));
// The input inside the preprocessor.
assert_eq!(preprocessor.get_input(), "si3".to_owned());
// The generated commands.
// The expected results without inhibit feature.
#[cfg(not(feature = "inhibit"))]
let mut expecteds = VecDeque::from(vec![
Command::Pause,
Command::Delete,
Command::Delete,
Command::CommitText("ī".to_owned()),
Command::Resume,
]);
// The expected results with inhibit feature.
#[cfg(feature = "inhibit")]
let mut expecteds = VecDeque::from(vec![
Command::Pause,
Command::Delete,
Command::Resume,
Command::Pause,
Command::Delete,
Command::Resume,
Command::Pause,
Command::Delete,
Command::CommitText("ī".to_owned()),
Command::Resume,
]);
// Verification.
while let Some(command) = preprocessor.pop_queue() {
assert_eq!(command, expecteds.pop_front().unwrap());
}
Sourcepub fn commit(&mut self, text: String)
pub fn commit(&mut self, text: String)
Commit a text.
Generate a command to ensure the commitment of this text. Useful when you want deal with auto-completion.
Note: Before any commitment, the preprocessor make sure to discard the current input.
§Example
use afrim_preprocessor::{Command, Preprocessor, utils};
use keyboard_types::{Key::Character, KeyboardEvent};
use std::{collections::VecDeque, rc::Rc};
// We prepare the memory.
let data = utils::load_data("i3 ī");
let text_buffer = utils::build_map(data);
let memory = Rc::new(text_buffer);
let mut preprocessor = Preprocessor::new(memory, 8);
// We process the input.
// let input = "si3";
preprocessor.process(KeyboardEvent {
key: Character("s".to_string()),
..Default::default()
});
preprocessor.commit("sī".to_owned());
// The generated commands.
// The expected results without inhibit feature.
#[cfg(not(feature = "inhibit"))]
let mut expecteds = VecDeque::from(vec![
Command::Pause,
Command::Delete,
Command::CommitText("sī".to_owned()),
Command::Resume,
]);
// The expected results with inhibit feature.
#[cfg(feature = "inhibit")]
let mut expecteds = VecDeque::from(vec![
Command::Pause,
Command::Delete,
Command::Resume,
Command::Pause,
Command::CleanDelete,
Command::CommitText("sī".to_owned()),
Command::Resume,
]);
// Verification.
while let Some(command) = preprocessor.pop_queue() {
assert_eq!(command, expecteds.pop_front().unwrap());
}
Sourcepub fn get_input(&self) -> String
pub fn get_input(&self) -> String
Returns the input present in the internal memory.
It’s always useful to know what is inside the memory of the preprocessor for debugging. Note: The input inside the preprocessor is not always the same than the original because of the limited capacity of his internal cursor.
§Example
use afrim_preprocessor::{Command, Preprocessor, utils};
use keyboard_types::webdriver::{self, Event};
use std::{collections::VecDeque, rc::Rc};
// We prepare the memory.
let data = utils::load_data("i3 ī");
let text_buffer = utils::build_map(data);
let memory = Rc::new(text_buffer);
let mut preprocessor = Preprocessor::new(memory, 4);
// We process the input.
let input = "si3";
webdriver::send_keys(input)
.into_iter()
.for_each(|event| {
match event {
// Triggers the generated keyboard input event.
Event::Keyboard(event) => preprocessor.process(event),
_ => unimplemented!(),
};
});
// The input inside the processor.
assert_eq!(preprocessor.get_input(), "si3".to_owned());
Sourcepub fn pop_queue(&mut self) -> Option<Command>
pub fn pop_queue(&mut self) -> Option<Command>
Returns the next command to be executed.
The next command is dropped from the queue and can’t be returned anymore.
§Example
use afrim_preprocessor::{Command, Preprocessor, utils};
use std::{collections::VecDeque, rc::Rc};
// We prepare the memory.
let text_buffer = utils::build_map(vec![]);
let memory = Rc::new(text_buffer);
let mut preprocessor = Preprocessor::new(memory, 8);
preprocessor.commit("hello".to_owned());
// The expected results.
let mut expecteds = VecDeque::from(vec![
Command::Pause,
Command::CommitText("hello".to_owned()),
Command::Resume,
]);
// Verification.
while let Some(command) = preprocessor.pop_queue() {
assert_eq!(command, expecteds.pop_front().unwrap());
}
Sourcepub fn clear_queue(&mut self)
pub fn clear_queue(&mut self)
Clears the queue.
§Example
use afrim_preprocessor::{Preprocessor, utils};
use std::rc::Rc;
let data =
utils::load_data("n* ŋ");
let text_buffer = utils::build_map(data);
let memory = Rc::new(text_buffer);
let mut preprocessor = Preprocessor::new(memory, 8);
preprocessor.commit("hi".to_owned());
preprocessor.clear_queue();
assert_eq!(preprocessor.pop_queue(), None);