Skip to content

Commit 5a0400b

Browse files
committed
#138: Use result instead of unwrapping from_java_cesu8
1 parent df42cff commit 5a0400b

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

rust/src/errors.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use std::sync::mpsc::RecvError;
2121
use std::sync::{PoisonError, TryLockError};
2222
use std::{fmt, result};
2323

24+
use cesu8::Cesu8DecodingError;
2425
use fs_extra;
2526
use serde_json;
2627

@@ -136,3 +137,9 @@ impl From<Canceled> for J4RsError {
136137
J4RsError::RustError(format!("{:?}", err))
137138
}
138139
}
140+
141+
impl From<Cesu8DecodingError> for J4RsError {
142+
fn from(err: Cesu8DecodingError) -> J4RsError {
143+
J4RsError::ParseError(format!("{:?}", err))
144+
}
145+
}

rust/src/jni_utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ pub(crate) unsafe fn string_from_jobject(
533533
} else {
534534
let s = (opt_to_res(cache::get_jni_get_string_utf_chars())?)(jni_env, obj, ptr::null_mut())
535535
as *mut c_char;
536-
let rust_string = utils::to_rust_string(s);
536+
let rust_string = utils::to_rust_string(s)?;
537537

538538
Ok(rust_string)
539539
}
@@ -545,7 +545,7 @@ pub unsafe fn jstring_to_rust_string(jvm: &Jvm, java_string: jstring) -> errors:
545545
java_string,
546546
ptr::null_mut(),
547547
) as *mut c_char;
548-
let rust_string = utils::to_rust_string(s);
548+
let rust_string = utils::to_rust_string(s)?;
549549
(opt_to_res(cache::get_jni_release_string_utf_chars())?)(jvm.jni_env, java_string, s);
550550
Jvm::do_return(jvm.jni_env, rust_string)
551551
}

rust/src/utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ use crate::api::{
2626
};
2727
use crate::{cache, errors, InvocationArg, JavaClass};
2828

29-
pub fn to_rust_string(pointer: *const c_char) -> String {
29+
pub fn to_rust_string(pointer: *const c_char) -> errors::Result<String> {
3030
let slice = unsafe { CStr::from_ptr(pointer).to_bytes() };
31-
from_java_cesu8(slice).unwrap().to_string()
31+
Ok(from_java_cesu8(slice)?.to_string())
3232
}
3333

3434
pub fn to_c_string(string: &str) -> *mut c_char {

0 commit comments

Comments
 (0)