diff --git a/app/src/main/rust/src/img/border.rs b/app/src/main/rust/src/img/border.rs index 7e3157b93..a7e7fbb35 100644 --- a/app/src/main/rust/src/img/border.rs +++ b/app/src/main/rust/src/img/border.rs @@ -1,5 +1,5 @@ use crate::{jni_throwing, with_bitmap_content}; -use anyhow::anyhow; +use anyhow::Context; use image::{ImageBuffer, Luma, Pixel, Rgba}; use jni::objects::JClass; use jni::sys::{jintArray, jobject}; @@ -89,7 +89,7 @@ pub fn detectBorder(mut env: JNIEnv, _class: JClass, object: jobject) -> jintArr jni_throwing(&mut env, |env| { let slice = with_bitmap_content(env, object, |img| Ok(detect_border(&img)))?; let array = env.new_int_array(4)?; - env.set_int_array_region(&array, 0, &slice.ok_or(anyhow!("Image too small!"))?)?; + env.set_int_array_region(&array, 0, &slice.context("Image too small!")?)?; Ok(array.into_raw()) }) } diff --git a/app/src/main/rust/src/img/copy_ahb.rs b/app/src/main/rust/src/img/copy_ahb.rs index 93291c548..9b85f0c48 100644 --- a/app/src/main/rust/src/img/copy_ahb.rs +++ b/app/src/main/rust/src/img/copy_ahb.rs @@ -1,7 +1,7 @@ #![cfg(feature = "api-level-26")] use crate::{jni_throwing, with_bitmap_content}; -use anyhow::anyhow; +use anyhow::Context; use image::{GenericImage, GenericImageView, ImageBuffer}; use jni::objects::JClass; use jni::sys::{jint, jobject}; @@ -22,7 +22,7 @@ pub fn copyBitmapToAHB(mut env: JNIEnv, _: JClass, bm: jobject, ahb: jobject, x: let view = src.view(x as u32, y as u32, w, h); let ptr = ahb.lock(HardwareBufferUsage::CPU_WRITE_RARELY, None, None)? as *mut u8; let s = unsafe { &mut *slice_from_raw_parts_mut(ptr, (stride * h * 4) as usize) }; - let dst = ImageBuffer::from_raw(stride, h, s).ok_or(anyhow!("Illegal AHB format!")); + let dst = ImageBuffer::from_raw(stride, h, s).context("Illegal AHB format!"); let result = dst.map(|mut dst| dst.copy_from(view.deref(), 0, 0)); ahb.unlock()?; result??; diff --git a/app/src/main/rust/src/lib.rs b/app/src/main/rust/src/lib.rs index fbffd0262..55a4f1dfa 100644 --- a/app/src/main/rust/src/lib.rs +++ b/app/src/main/rust/src/lib.rs @@ -2,7 +2,7 @@ mod img; mod parser; use android_logger::Config; -use anyhow::{anyhow, ensure, Result}; +use anyhow::{ensure, Context, Result}; use image::{ImageBuffer, Rgba}; use jni::objects::JByteBuffer; use jni::sys::{jboolean, jint, jobject, JavaVM, JNI_VERSION_1_6}; @@ -120,7 +120,7 @@ where // SAFETY: ktor client ensure html content is valid utf-8. let html = unsafe { from_utf8_unchecked(&buffer[..limit as usize]) }; - let dom = tl::parse(html, ParserOptions::default()).map_err(|e| anyhow!(e))?; + let dom = tl::parse(html, ParserOptions::default())?; ensure!(dom.version().is_some(), "{html}"); f(&dom, html)? }; @@ -168,9 +168,7 @@ where let buffer = unsafe { &*slice_from_raw_parts(ptr, (width * height * 4) as usize) }; let image = ImageBuffer::from_raw(width, height, buffer); - let result = image - .ok_or(anyhow!("Image buffer not RGBA8888!!!")) - .and_then(f); + let result = image.context("Image buffer not RGBA8888!!!").and_then(f); handle.unlock_pixels()?; result } diff --git a/app/src/main/rust/src/parser/home.rs b/app/src/main/rust/src/parser/home.rs index c44eba865..568782726 100644 --- a/app/src/main/rust/src/parser/home.rs +++ b/app/src/main/rust/src/parser/home.rs @@ -1,5 +1,5 @@ use crate::{get_vdom_first_element_by_class_name, parse_marshal_inplace}; -use anyhow::anyhow; +use anyhow::Context; use jni::objects::{JByteBuffer, JClass}; use jni::sys::jint; use jni::JNIEnv; @@ -52,6 +52,6 @@ fn parse_limit(dom: &VDom, parser: &Parser) -> Option { #[jni_fn("com.hippo.ehviewer.client.parser.HomeParserKt")] pub fn parseLimit(mut env: JNIEnv, _class: JClass, input: JByteBuffer, limit: jint) -> jint { parse_marshal_inplace(&mut env, input, limit, |dom, _| { - parse_limit(dom, dom.parser()).ok_or(anyhow!("Can't parse Limit")) + parse_limit(dom, dom.parser()).context("Can't parse Limit") }) } diff --git a/app/src/main/rust/src/parser/list.rs b/app/src/main/rust/src/parser/list.rs index 195173f51..b02590232 100644 --- a/app/src/main/rust/src/parser/list.rs +++ b/app/src/main/rust/src/parser/list.rs @@ -3,7 +3,7 @@ use crate::{get_element_by_id, get_vdom_first_element_by_class_name}; use crate::{get_first_element_by_class_name, query_childs_first_match_attr}; use crate::{get_node_attr, get_node_handle_attr, regex}; use crate::{EHGT_PREFIX, EX_PREFIX}; -use anyhow::{anyhow, bail, Result}; +use anyhow::{bail, Context, Result}; use jni::objects::{JByteBuffer, JClass}; use jni::sys::jint; use jni::JNIEnv; @@ -235,7 +235,7 @@ pub fn parse_info_list(dom: &VDom, parser: &Parser, str: &str) -> Result Result> { - let list = dom.query_selector("table").ok_or(anyhow!("No Table"))?.filter_map(|e| { + let list = dom.query_selector("table").context("No Table")?.filter_map(|e| { let html = e.get(parser)?.inner_html(parser); if html.contains("Expunged") { None