diff --git a/app/src/main/jni/afrim_jni/src/afrim_api.rs b/app/src/main/jni/afrim_jni/src/afrim_api.rs index b3e7fe8f..3414228e 100644 --- a/app/src/main/jni/afrim_jni/src/afrim_api.rs +++ b/app/src/main/jni/afrim_jni/src/afrim_api.rs @@ -4,6 +4,7 @@ use afrim_config::Config; use afrim_preprocessor::Preprocessor; use afrim_translator::Translator; +use anyhow::Result; use once_cell::sync::Lazy; use std::path::Path; @@ -14,9 +15,9 @@ pub struct Afrim { impl Afrim { /// Initializes an Afrim instance based on the provided configuration file. - pub fn from_config(config_file: &str) -> Result { + pub fn from_config(config_file: &str) -> Result { let config_file = Path::new(&config_file); - let config = Config::from_file(config_file).map_err(|err| err.to_string())?; + let config = Config::from_file(config_file)?; // Core let auto_commit = config @@ -49,8 +50,7 @@ impl Afrim { // Translators #[cfg(feature = "rhai")] config - .extract_translators() - .map_err(|err| err.to_string())? + .extract_translators()? .into_iter() .for_each(|(name, ast)| { translator.register(name, ast); @@ -72,7 +72,7 @@ impl Afrim { } /// Process the keyboard event. - pub fn process_key(&mut self, key: &str, state: &str) -> Result<(bool, bool), String> { + pub fn process_key(&mut self, key: &str, state: &str) -> Result<(bool, bool)> { let key_event = utils::deserialize_event(key, state)?; let status = self.preprocessor.process(key_event); @@ -149,18 +149,16 @@ impl Singleton { mod utils { pub use afrim_preprocessor::utils::*; use afrim_preprocessor::{Command, Key, KeyboardEvent}; + use anyhow::{Context, Result}; use serde_json::{self}; use std::str::FromStr; /// Deserializes the KeyboardEvent. - pub fn deserialize_event(key: &str, state: &str) -> Result { + pub fn deserialize_event(key: &str, state: &str) -> Result { let event = KeyboardEvent { - key: Key::from_str(key).map_err(|err| { - format!("[preprocessor] Unrecognized key `{key}`.\nCaused by:\n\t{err}.") - })?, - state: serde_json::from_str(state).map_err(|err| { - format!("[preprocessor] Unrecognized state `{state}`.\nCaused by:\n\t{err}.") - })?, + key: Key::from_str(key).with_context(|| format!("Unrecognized key `{key}`."))?, + state: serde_json::from_str(state) + .with_context(|| format!("Unrecognized state `{state}`."))?, ..Default::default() }; diff --git a/app/src/main/jni/afrim_jni/src/lib.rs b/app/src/main/jni/afrim_jni/src/lib.rs index a8f8ed00..1fa46b1a 100644 --- a/app/src/main/jni/afrim_jni/src/lib.rs +++ b/app/src/main/jni/afrim_jni/src/lib.rs @@ -11,6 +11,8 @@ mod android { JNIEnv, }; + const AFRIM_NOT_CONFIGURED: &str = "Afrim singleton is not yet configured."; + // Singleton #[no_mangle] pub unsafe extern "C" fn Java_cm_pythonbrad_afrim_core_Afrim_nativeInit( @@ -57,7 +59,7 @@ mod android { } Err(err) => { log.e(&format!( - "Error while the updating of the afrim singleton: {err}", + "Error while the updating of the afrim singleton: {err:?}", )); 0 @@ -105,11 +107,11 @@ mod android { return status.into_raw(); } Err(err) => { - log.e(format!("Error while key processing: {err}")); + log.e(format!("Error while key processing: {err:?}")); } } } else { - log.w("Afrim singleton is not yet configured."); + log.w(AFRIM_NOT_CONFIGURED); }; JObject::null().into_raw() @@ -131,7 +133,7 @@ mod android { afrim.commit_text(text); log.i("Text committed!"); } else { - log.w("Afrim singleton is not yet configured."); + log.w(AFRIM_NOT_CONFIGURED); } } @@ -151,7 +153,7 @@ mod android { cmd.into_raw() } else { - log.w("Afrim singleton is not yet configured."); + log.w(AFRIM_NOT_CONFIGURED); JObject::null().into_raw() } @@ -170,7 +172,7 @@ mod android { afrim.clear(); log.i("Afrim memory cleared!"); } else { - log.w("Afrim singleton is not yet configured."); + log.w(AFRIM_NOT_CONFIGURED); } } @@ -190,7 +192,7 @@ mod android { input.into_raw() } else { - log.w("Afrim singleton is not yet configured."); + log.w(AFRIM_NOT_CONFIGURED); JObject::null().into_raw() } @@ -239,7 +241,7 @@ mod android { array.into_raw() } else { - log.w("Afrim singleton is not yet configured."); + log.w(AFRIM_NOT_CONFIGURED); JObject::null().into_raw() }