Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions rust/src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,14 @@ pub unsafe extern "C" fn cursorFromString(
env.throw_new(
"java/lang/IllegalArgumentException",
"invalid cursor string",
)
.unwrap();
);
return JObject::null().into_raw();
};
let Ok(cursor) = automerge::Cursor::try_from(s) else {
env.throw_new(
"java/lang/IllegalArgumentException",
"invalid cursor string",
)
.unwrap();
);
return JObject::null().into_raw();
};
Cursor::from(cursor).into_raw(&mut env).unwrap()
Expand All @@ -109,8 +107,7 @@ pub unsafe extern "C" fn cursorFromBytes(
let bytes = env.convert_byte_array(&jarr).unwrap();
let Ok(cursor) = automerge::Cursor::try_from(bytes) else {
// throw IllegalArgumentException
env.throw_new("java/lang/IllegalArgumentException", "invalid cursor bytes")
.unwrap();
env.throw_new("java/lang/IllegalArgumentException", "invalid cursor bytes");
return JObject::null().into_raw();
};
Cursor::from(cursor).into_raw(&mut env).unwrap()
Expand Down
12 changes: 6 additions & 6 deletions rust/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ pub unsafe extern "C" fn loadDoc(
let doc = match automerge::Automerge::load(&bytes) {
Ok(d) => d,
Err(e) => {
env.throw_new(AUTOMERGE_EXCEPTION, e.to_string()).unwrap();
env.throw_new(AUTOMERGE_EXCEPTION, e.to_string());
return JObject::null().into_raw();
}
};
Expand Down Expand Up @@ -194,7 +194,7 @@ pub unsafe fn do_fork_at(
let doc = match doc.fork_at(&heads) {
Ok(d) => d,
Err(e) => {
env.throw_new(AUTOMERGE_EXCEPTION, e.to_string()).unwrap();
env.throw_new(AUTOMERGE_EXCEPTION, e.to_string());
return JObject::null().into_raw();
}
};
Expand All @@ -219,7 +219,7 @@ pub unsafe extern "C" fn mergeDoc(
match doc1.merge(other_doc) {
Ok(_) => {}
Err(e) => {
env.throw_new(AUTOMERGE_EXCEPTION, e.to_string()).unwrap();
env.throw_new(AUTOMERGE_EXCEPTION, e.to_string());
}
}
}
Expand All @@ -239,7 +239,7 @@ pub unsafe extern "C" fn mergeDocLogPatches(
match doc1.merge_and_log_patches(other_doc, patch_log) {
Ok(_) => {}
Err(e) => {
env.throw_new(AUTOMERGE_EXCEPTION, e.to_string()).unwrap();
env.throw_new(AUTOMERGE_EXCEPTION, e.to_string());
}
}
}
Expand Down Expand Up @@ -275,7 +275,7 @@ pub unsafe extern "C" fn applyEncodedChanges(
match doc.load_incremental(&changes_bytes) {
Ok(_) => {}
Err(e) => {
env.throw_new(AUTOMERGE_EXCEPTION, e.to_string()).unwrap();
env.throw_new(AUTOMERGE_EXCEPTION, e.to_string());
}
};
}
Expand All @@ -296,7 +296,7 @@ pub unsafe extern "C" fn applyEncodedChangesLogPatches(
match doc.load_incremental_log_patches(&changes_bytes, patchlog) {
Ok(_) => {}
Err(e) => {
env.throw_new(AUTOMERGE_EXCEPTION, e.to_string()).unwrap();
env.throw_new(AUTOMERGE_EXCEPTION, e.to_string());
}
};
}
Expand Down
6 changes: 3 additions & 3 deletions rust/src/interop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@ pub(crate) unsafe fn heads_from_jobject(
Ok(heads)
}

pub(crate) unsafe fn changehash_to_jobject<'a>(
env: &mut jni::JNIEnv<'a>,
pub(crate) fn changehash_to_jobject<'local>(
env: &mut jni::JNIEnv<'local>,
hash: &ChangeHash,
) -> Result<JObject<'a>, jni::errors::Error> {
) -> Result<JObject<'local>, jni::errors::Error> {
let jhash = env.alloc_object(CHANGEHASH_CLASS)?;
let byte_array = env.byte_array_from_slice(hash.as_ref())?;
env.set_field(&jhash, "hash", "[B", (&byte_array).into())
Expand Down
25 changes: 22 additions & 3 deletions rust/src/java_option.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use jni::objects::{JObject, JValue};
use jni::{
objects::{JObject, JValue},
JNIEnv,
};

pub(crate) unsafe fn make_optional<'a>(
pub(crate) fn make_optional<'a>(
env: &mut jni::JNIEnv<'a>,
val: JValue,
) -> Result<JObject<'a>, jni::errors::Error> {
Expand All @@ -13,9 +16,25 @@ pub(crate) unsafe fn make_optional<'a>(
.l()
}

pub(crate) unsafe fn make_empty_option<'a>(
pub(crate) fn make_empty_option<'a>(
env: &mut jni::JNIEnv<'a>,
) -> Result<JObject<'a>, jni::errors::Error> {
env.call_static_method("java/util/Optional", "empty", "()Ljava/util/Optional;", &[])?
.l()
}

pub(crate) fn make_optional_of<'local, T, F>(
env: &mut JNIEnv<'local>,
opt: &Option<T>,
func: F,
) -> Result<JObject<'local>, jni::errors::Error>
where
F: for<'a> FnOnce(&mut JNIEnv<'a>, &T) -> Result<JObject<'a>, jni::errors::Error>,
{
if let Some(val) = opt {
let val_obj = func(env, val)?;
make_optional(env, (&val_obj).into())
} else {
make_empty_option(env)
}
}
9 changes: 3 additions & 6 deletions rust/src/obj_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,13 @@ macro_rules! obj_id_or_throw {
$env.throw_new(
"java/lang/IllegalArgumentException",
"Object ID cannot be null",
)
.unwrap();
);
#[allow(clippy::unused_unit)]
return $returning;
}
Err(e) => {
use crate::AUTOMERGE_EXCEPTION;
$env.throw_new(AUTOMERGE_EXCEPTION, format!("{}", e))
.unwrap();
$env.throw_new(AUTOMERGE_EXCEPTION, format!("{}", e));
#[allow(clippy::unused_unit)]
return $returning;
}
Expand Down Expand Up @@ -179,8 +177,7 @@ pub unsafe extern "C" fn objectIdsEqual(
env.throw_new(
"java/lang/IllegalArgumentException",
"Object ID cannot be null",
)
.unwrap();
);
false.into()
}
(Some(left), Some(right)) => (left.as_ref() == right.as_ref()).into(),
Expand Down
25 changes: 12 additions & 13 deletions rust/src/read_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ macro_rules! catch {
match $e {
Ok(r) => r,
Err(e) => {
$env.throw_new(AUTOMERGE_EXCEPTION, e.to_string()).unwrap();
$env.throw_new(AUTOMERGE_EXCEPTION, e.to_string());
return JObject::null().into_raw();
}
}
Expand Down Expand Up @@ -127,7 +127,7 @@ impl SomeReadPointer {
Ok(Some(c)) => make_optional(&mut env, (&c).into()).unwrap().into_raw(),
Ok(None) => make_empty_option(&mut env).unwrap().into_raw(),
Err(e) => {
env.throw_new(AUTOMERGE_EXCEPTION, e.to_string()).unwrap();
env.throw_new(AUTOMERGE_EXCEPTION, e.to_string());
JObject::null().into_raw()
}
}
Expand Down Expand Up @@ -164,7 +164,7 @@ impl SomeReadPointer {
},
Ok(_) => return make_empty_option(env).unwrap().into_raw(),
Err(e) => {
env.throw_new(AUTOMERGE_EXCEPTION, e.to_string()).unwrap();
env.throw_new(AUTOMERGE_EXCEPTION, e.to_string());
return JObject::null().into_raw();
}
};
Expand Down Expand Up @@ -215,7 +215,7 @@ impl SomeReadPointer {
return make_empty_option(&mut env).unwrap().into_raw()
}
Err(e) => {
env.throw_new(AUTOMERGE_EXCEPTION, e.to_string()).unwrap();
env.throw_new(AUTOMERGE_EXCEPTION, e.to_string());
return JObject::null().into_raw();
}
};
Expand Down Expand Up @@ -258,7 +258,7 @@ impl SomeReadPointer {
return make_empty_option(&mut env).unwrap().into_raw()
}
Err(e) => {
env.throw_new(AUTOMERGE_EXCEPTION, e.to_string()).unwrap();
env.throw_new(AUTOMERGE_EXCEPTION, e.to_string());
return JObject::null().into_raw();
}
};
Expand Down Expand Up @@ -314,7 +314,7 @@ impl SomeReadPointer {
return make_empty_option(&mut env).unwrap().into_raw()
}
Err(e) => {
env.throw_new(AUTOMERGE_EXCEPTION, e.to_string()).unwrap();
env.throw_new(AUTOMERGE_EXCEPTION, e.to_string());
return JObject::null().into_raw();
}
};
Expand All @@ -340,7 +340,7 @@ impl SomeReadPointer {
let marks = match marks {
Ok(m) => m,
Err(e) => {
env.throw_new(AUTOMERGE_EXCEPTION, e.to_string()).unwrap();
env.throw_new(AUTOMERGE_EXCEPTION, e.to_string());
return JObject::null().into_raw();
}
};
Expand Down Expand Up @@ -376,7 +376,7 @@ impl SomeReadPointer {
let marks = match marks {
Ok(m) => m,
Err(e) => {
env.throw_new(AUTOMERGE_EXCEPTION, e.to_string()).unwrap();
env.throw_new(AUTOMERGE_EXCEPTION, e.to_string());
return JObject::null().into_raw();
}
};
Expand Down Expand Up @@ -406,15 +406,14 @@ impl SomeReadPointer {
let heads = maybe_heads(&mut env, maybe_heads_pointer).unwrap();
let read = SomeRead::from_pointer(&mut env, self);
if index < 0 {
env.throw_new(AUTOMERGE_EXCEPTION, "Index must be >= 0")
.unwrap();
env.throw_new(AUTOMERGE_EXCEPTION, "Index must be >= 0");
return JObject::null().into_raw();
}
let cursor = read.get_cursor(obj, index as usize, heads.as_deref());
let cursor = match cursor {
Ok(c) => c,
Err(e) => {
env.throw_new(AUTOMERGE_EXCEPTION, e.to_string()).unwrap();
env.throw_new(AUTOMERGE_EXCEPTION, e.to_string());
return JObject::null().into_raw();
}
};
Expand All @@ -437,7 +436,7 @@ impl SomeReadPointer {
let index = match index {
Ok(i) => i,
Err(e) => {
env.throw_new(AUTOMERGE_EXCEPTION, e.to_string()).unwrap();
env.throw_new(AUTOMERGE_EXCEPTION, e.to_string());
return 0;
}
};
Expand All @@ -453,7 +452,7 @@ impl SomeReadPointer {
return make_empty_option(&mut env).unwrap().into_raw();
}
Err(e) => {
env.throw_new(AUTOMERGE_EXCEPTION, e.to_string()).unwrap();
env.throw_new(AUTOMERGE_EXCEPTION, e.to_string());
return JObject::null().into_raw();
}
};
Expand Down
10 changes: 5 additions & 5 deletions rust/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ pub unsafe extern "C" fn receiveSyncMessage(
let message = match Message::decode(&message_bytes) {
Ok(m) => m,
Err(e) => {
env.throw_new(AUTOMERGE_EXCEPTION, e.to_string()).unwrap();
env.throw_new(AUTOMERGE_EXCEPTION, e.to_string());
return;
}
};
match doc.receive_sync_message(state, message) {
Ok(()) => {}
Err(e) => {
env.throw_new(AUTOMERGE_EXCEPTION, e.to_string()).unwrap();
env.throw_new(AUTOMERGE_EXCEPTION, e.to_string());
}
}
}
Expand All @@ -90,14 +90,14 @@ pub unsafe extern "C" fn receiveSyncMessageLogPatches(
let message = match Message::decode(&message_bytes) {
Ok(m) => m,
Err(e) => {
env.throw_new(AUTOMERGE_EXCEPTION, e.to_string()).unwrap();
env.throw_new(AUTOMERGE_EXCEPTION, e.to_string());
return;
}
};
match doc.receive_sync_message_log_patches(state, message, patch_log) {
Ok(_) => {}
Err(e) => {
env.throw_new(AUTOMERGE_EXCEPTION, e.to_string()).unwrap();
env.throw_new(AUTOMERGE_EXCEPTION, e.to_string());
}
}
}
Expand Down Expand Up @@ -128,7 +128,7 @@ pub unsafe extern "C" fn decodeSyncState(
match SyncState::decode(&bytes) {
Ok(state) => state.to_pointer_obj(&mut env).unwrap().into_raw(),
Err(e) => {
env.throw_new(AUTOMERGE_EXCEPTION, e.to_string()).unwrap();
env.throw_new(AUTOMERGE_EXCEPTION, e.to_string());
JObject::null().into_raw()
}
}
Expand Down
Loading