diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 43e27cc..d3eb0b9 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -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 diff --git a/engine/translator/src/lib.rs b/engine/translator/src/lib.rs index 874460e..05f06a0 100644 --- a/engine/translator/src/lib.rs +++ b/engine/translator/src/lib.rs @@ -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#" @@ -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"), @@ -65,19 +60,27 @@ pub struct Translator { impl Translator { /// Initiate a new translator. - pub fn new( - dictionary: HashMap>, - #[cfg(feature = "rhai")] translators: HashMap, - auto_commit: bool, - ) -> Self { + pub fn new(dictionary: HashMap>, 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, bool)> { #[cfg(feature = "rhai")] @@ -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(); @@ -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!( diff --git a/service/src/lib.rs b/service/src/lib.rs index b7c9e7f..2a23651 100644 --- a/service/src/lib.rs +++ b/service/src/lib.rs @@ -31,12 +31,16 @@ pub fn run(config: Config, mut frontend: impl Frontend) -> Result<(), Box