Skip to content

Commit

Permalink
patch(afrim-jni): improve error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
pythonbrad committed Mar 31, 2024
1 parent 288f85c commit f3a8fc7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
22 changes: 10 additions & 12 deletions app/src/main/jni/afrim_jni/src/afrim_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<Self, String> {
pub fn from_config(config_file: &str) -> Result<Self> {
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
Expand Down Expand Up @@ -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);
Expand All @@ -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);

Expand Down Expand Up @@ -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<KeyboardEvent, String> {
pub fn deserialize_event(key: &str, state: &str) -> Result<KeyboardEvent> {
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()
};

Expand Down
18 changes: 10 additions & 8 deletions app/src/main/jni/afrim_jni/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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);
}
}

Expand All @@ -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()
}
Expand All @@ -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);
}
}

Expand All @@ -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()
}
Expand Down Expand Up @@ -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()
}
Expand Down

0 comments on commit f3a8fc7

Please sign in to comment.