Struct Preprocessor

Source
pub struct Preprocessor { /* private fields */ }
Expand description

The main structure of the preprocessor.

Implementations§

Source§

impl Preprocessor

Source

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);
Source

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());
}
Source

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());
}
Source

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());
Source

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());
}
Source

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);

Trait Implementations§

Source§

impl Debug for Preprocessor

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.