Fuck off silly error handling

Signed-off-by: GG Bond <whatever@tagkong.com>
This commit is contained in:
GG Bond authored and Vladimir Dostoyevskiy committed 2024-02-25 23:04:35 +08:00
1 parent 60532e2622
commit 962f9bfff5
2 files changed
+35 -17

No files matched your search

+9 -16
View File
@@ -1,12 +1,12 @@
use anyhow::Result;
use anyhow::{Ok, Result};
use base16ct::lower::encode_str;
use jni::objects::JClass;
use jni::sys::{jint, jstring};
use jni::JNIEnv;
use jni_fn::jni_fn;
use jni_throwing;
use sha1::{Digest, Sha1};
use std::{fs::File, io::copy, os::fd::BorrowedFd};
use throw_msg;
pub fn file_sha1(mut file: File, digest: &mut [u8; 40]) -> Result<&str> {
let mut sha = Sha1::new();
@@ -18,18 +18,11 @@ pub fn file_sha1(mut file: File, digest: &mut [u8; 40]) -> Result<&str> {
#[allow(non_snake_case)]
#[jni_fn("com.hippo.ehviewer.jni.HashKt")]
pub fn sha1(mut env: JNIEnv, _class: JClass, fd: jint) -> jstring {
let borrowed_fd = unsafe { BorrowedFd::borrow_raw(fd) };
let owned_fd = match borrowed_fd.try_clone_to_owned() {
Ok(fd) => fd,
Err(err) => return throw_msg(&mut env, &format!("{err}")) as jstring,
};
let mut hex_digest = [0u8; 40];
let hex = match file_sha1(File::from(owned_fd), &mut hex_digest) {
Ok(str) => str,
Err(err) => return throw_msg(&mut env, &format!("{err}")) as jstring,
};
match env.new_string(hex) {
Ok(obj) => obj.into_raw(),
Err(err) => throw_msg(&mut env, &format!("{err}")) as jstring,
}
jni_throwing(&mut env, |env| {
let borrowed_fd = unsafe { BorrowedFd::borrow_raw(fd) };
let owned_fd = borrowed_fd.try_clone_to_owned()?;
let mut hex_digest = [0u8; 40];
let hex = file_sha1(File::from(owned_fd), &mut hex_digest)?;
Ok(env.new_string(hex)?.into_raw())
})
}
+26 -1
View File
@@ -15,8 +15,9 @@ extern crate sha1;
extern crate tl;
use android_logger::Config;
use anyhow::Result;
use jni::objects::JByteBuffer;
use jni::sys::{jint, JavaVM, JNI_VERSION_1_6};
use jni::sys::{jint, jobject, JavaVM, JNI_VERSION_1_6};
use jni::JNIEnv;
use log::LevelFilter;
use serde::Serialize;
@@ -77,6 +78,30 @@ fn throw_msg(env: &mut JNIEnv, msg: &str) -> i32 {
0
}
trait ThrowingHasDefault {
fn default() -> Self;
}
impl ThrowingHasDefault for jobject {
fn default() -> Self {
0 as jobject
}
}
fn jni_throwing<F, R>(env: &mut JNIEnv, f: F) -> R
where
F: FnOnce(&mut JNIEnv) -> Result<R>,
R: ThrowingHasDefault,
{
match f(env) {
Ok(value) => value,
Err(err) => {
throw_msg(env, &format!("{err}"));
R::default()
}
}
}
fn parse_marshal_inplace<F, R>(env: &mut JNIEnv, str: JByteBuffer, limit: jint, f: F) -> i32
where
F: Fn(&VDom, &str) -> Result<R, &'static str>,