Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
pythonbrad committed Oct 21, 2023
1 parent 807afb0 commit 0e6c648
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:
cargo fmt -- --check
- name: Clippy
run: cargo clippy --all-targets -- -D warnings
run: cargo clippy --all-features --all-targets -- -D warnings

- name: Build
run: cargo build --all-features --verbose
Expand Down
54 changes: 28 additions & 26 deletions engine/translator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
//! dictionary.insert("halo".to_string(), ["hello".to_string()].to_vec());
//! dictionary.insert("nihao".to_string(), ["hello".to_string()].to_vec());
//!
//! // We build the translator.
//! let mut translator = Translator::new(dictionary, true);
//!
//! // Translation via scripting
//! #[cfg(feature = "rhai")]
//! let mut translators = HashMap::new();
//! #[cfg(feature = "rhai")]
//! {
//! let engine = Engine::new();
//! let hi = engine.compile(r#"
Expand All @@ -25,15 +26,9 @@
//! }
//! }
//! "#).unwrap();
//! translators.insert("hi".to_string(), hi);
//! translator.register("hi".to_string(), hi);
//! }
//!
//! // We build the translator.
//! #[cfg(not(feature = "rhai"))]
//! let translator = Translator::new(dictionary, true);
//! #[cfg(feature = "rhai")]
//! let translator = Translator::new(dictionary, translators, true);
//!
//! #[cfg(feature = "rhai")]
//! assert_eq!(
//! translator.translate("hi"),
Expand Down Expand Up @@ -65,19 +60,27 @@ pub struct Translator {

impl Translator {
/// Initiate a new translator.
pub fn new(
dictionary: HashMap<String, Vec<String>>,
#[cfg(feature = "rhai")] translators: HashMap<String, AST>,
auto_commit: bool,
) -> Self {
pub fn new(dictionary: HashMap<String, Vec<String>>, auto_commit: bool) -> Self {
Self {
dictionary,
#[cfg(feature = "rhai")]
translators,
auto_commit,
#[cfg(feature = "rhai")]
translators: HashMap::default()
}
}

#[cfg(feature = "rhai")]
/// Register a translator
pub fn register(&mut self, name: String, ast: AST) {
self.translators.insert(name, ast);
}

#[cfg(feature = "rhai")]
/// Unregister a translator
pub fn unregister(&mut self, name: &str) {
self.translators.remove(name);
}

/// Generate a list of predicates based on the input.
pub fn translate(&self, input: &str) -> Vec<(String, String, Vec<String>, bool)> {
#[cfg(feature = "rhai")]
Expand Down Expand Up @@ -143,9 +146,13 @@ mod tests {
let mut dictionary = HashMap::new();
dictionary.insert("halo".to_string(), ["hello".to_string()].to_vec());

//
// We config the translator
#[cfg(not(feature = "rhai"))]
let translator = Translator::new(dictionary, true);
#[cfg(feature = "rhai")]
let mut translators = HashMap::new();
let mut translator = Translator::new(dictionary, true);

//
#[cfg(feature = "rhai")]
{
let engine = Engine::new();
Expand All @@ -161,16 +168,11 @@ mod tests {
"#,
)
.unwrap();
translators.insert("none".to_string(), ast1);
translators.insert("some".to_string(), ast2);
translator.register("none".to_string(), ast1);
translator.unregister("none");
translator.register("some".to_string(), ast2);
}

// We config the translator
#[cfg(not(feature = "rhai"))]
let translator = Translator::new(dictionary, true);
#[cfg(feature = "rhai")]
let translator = Translator::new(dictionary, translators, true);

assert_eq!(translator.translate("h"), vec![]);
#[cfg(feature = "rhai")]
assert_eq!(
Expand Down
16 changes: 10 additions & 6 deletions service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,16 @@ pub fn run(config: Config, mut frontend: impl Frontend) -> Result<(), Box<dyn er
.unwrap_or((32, false, 10));
let mut keyboard = Enigo::new();
let mut preprocessor = Preprocessor::new(map, buffer_size);
let translator = Translator::new(
config.extract_translation(),
#[cfg(feature = "rhai")]
config.extract_translators()?,
auto_commit,
);
#[cfg(not(feature = "rhai"))]
let translator = Translator::new(config.extract_translation(), auto_commit);
#[cfg(feature = "rhai")]
let mut translator = Translator::new(config.extract_translation(), auto_commit);
#[cfg(feature = "rhai")]
config
.extract_translators()?
.into_iter()
.for_each(|(name, ast)| translator.register(name, ast));

let mut is_special_pressed = false;

frontend.set_page_size(page_size);
Expand Down

0 comments on commit 0e6c648

Please sign in to comment.