From fa1261f8d5f520287513bfd8ee16e291daea6c8a Mon Sep 17 00:00:00 2001 From: breadrock Date: Fri, 5 Jul 2024 19:36:24 +0300 Subject: [PATCH 1/6] chore(readme): Updated README.md file --- README.md | 80 ++++++++++++++++++------------------------------------- 1 file changed, 26 insertions(+), 54 deletions(-) diff --git a/README.md b/README.md index 0dcf583..174c667 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,6 @@ This project is a simplest Java wrapper for the `adblock-rust` library, allowing - [Features](#features) - [Installation](#installation) - [Usage](#usage) -- [Examples](#examples) ## Introduction @@ -33,17 +32,23 @@ First, you need to build the `adblock-rust` library and generate the shared libr 1. Clone the `adblock-rust` repository: 2. Build the library: -```sh cargo build --release``` +```sh +cargo build --release --manifest-path adblock-rs/Cargo.toml +mvn package +``` 3. Locate the generated shared library file in the `target/release` directory. ```java -public class Main { - static { - System.loadLibrary("adblock_rust"); // Replace with the actual library name - } - +public class Main { public static void main(String[] args) { - // Your code here + List rules = new ArrayList<>(List.of( + "-advertisement-icon.", + "-advertisement-management/", + "-advertisement.", + "-advertisement/script." + )); + + AdvtBlocker blocker = AdvtBlocker.createInstance(rules); } } ``` @@ -69,54 +74,21 @@ import com.example.adblock.AdblockEngine; public class Main { public static void main(String[] args) { - AdblockEngine engine = new AdblockEngine(); - - // Load filter lists - engine.loadFilterList("path/to/easylist.txt"); - - // Check if a URL is blocked - boolean isBlocked = engine.isBlocked("http://example.com/ad"); - System.out.println("Is blocked: " + isBlocked); - } -} - -``` - -## Examples - -### Example 1: Blocking Ads - -```java -import com.example.adblock.AdblockEngine; - -public class AdblockExample { - public static void main(String[] args) { - AdblockEngine engine = new AdblockEngine(); - - // Load filter lists - engine.loadFilterList("path/to/easylist.txt"); - - // Check if a URL is blocked - boolean isBlocked = engine.isBlocked("http://example.com/ad"); - System.out.println("Is blocked: " + isBlocked); - } -} -``` - -### Example 2: Custom Filter Rules -```java -import com.example.adblock.AdblockEngine; - -public class CustomFilterExample { - public static void main(String[] args) { - AdblockEngine engine = new AdblockEngine(); + List rules = new ArrayList<>(List.of( + "-advertisement-icon.", + "-advertisement-management/", + "-advertisement.", + "-advertisement/script." + )); - // Add custom filter rule - engine.addCustomFilterRule("||example.com^"); + AdvtBlocker blocker = AdvtBlocker.createInstance(rules); + boolean result = blocker.checkUrls( + "http://example.com/-advertisement-icon.", + "http://example.com/helloworld", + "image" + ); - // Check if a URL is blocked - boolean isBlocked = engine.isBlocked("http://example.com/ad"); - System.out.println("Is blocked: " + isBlocked); + System.out.println(result); } } ``` From da9c676bbae2f8708a5feff0485f628164d049e3 Mon Sep 17 00:00:00 2001 From: breadrock Date: Fri, 5 Jul 2024 19:40:46 +0300 Subject: [PATCH 2/6] fix(fmt): Fixed fmt warnings --- adblock-rs/src/adblock.rs | 14 ++++++++------ adblock-rs/src/errors.rs | 2 +- adblock-rs/src/lib.rs | 12 +++++------- adblock-rs/src/wrapper.rs | 20 +++++++++++--------- 4 files changed, 25 insertions(+), 23 deletions(-) diff --git a/adblock-rs/src/adblock.rs b/adblock-rs/src/adblock.rs index d6c0a9f..ddd3352 100644 --- a/adblock-rs/src/adblock.rs +++ b/adblock-rs/src/adblock.rs @@ -1,6 +1,6 @@ -use adblock::{Engine, FilterSet}; use adblock::lists::ParseOptions; use adblock::request::Request; +use adblock::{Engine, FilterSet}; use crate::errors::RustException; @@ -55,11 +55,13 @@ mod adblock_test { ]; let advt_blocker = AdvtBlocker::new(rules); - let check_result = advt_blocker.check_network_urls( - "http://example.com/-advertisement-icon.", - "http://example.com/helloworld", - "image" - ).unwrap(); + let check_result = advt_blocker + .check_network_urls( + "http://example.com/-advertisement-icon.", + "http://example.com/helloworld", + "image", + ) + .unwrap(); assert_eq!(check_result, true); } diff --git a/adblock-rs/src/errors.rs b/adblock-rs/src/errors.rs index 132e7b2..1d1c060 100644 --- a/adblock-rs/src/errors.rs +++ b/adblock-rs/src/errors.rs @@ -1,7 +1,7 @@ use jni::errors::{Exception, ToException}; -use std::fmt::Debug; use adblock::request::RequestError; +use std::fmt::Debug; use thiserror::Error; #[derive(Debug, Error)] diff --git a/adblock-rs/src/lib.rs b/adblock-rs/src/lib.rs index 3f2a3c9..925095b 100644 --- a/adblock-rs/src/lib.rs +++ b/adblock-rs/src/lib.rs @@ -2,9 +2,9 @@ mod adblock; mod errors; mod wrapper; -use jni::JNIEnv; use jni::objects::{JObject, JObjectArray, JString}; use jni::sys::{jboolean, jlong}; +use jni::JNIEnv; use crate::wrapper::*; @@ -32,10 +32,8 @@ pub extern "system" fn Java_com_example_adblock_AdvtBlocker_checkNetworkUrls( src_url: JString, req_type: JString, ) -> jboolean { - check_net_urls_wrapped(&mut env, ptr, &url, &src_url, &req_type) - .unwrap_or_else(|err| { - log::error!("{:?}", err); - false as jboolean - }) + check_net_urls_wrapped(&mut env, ptr, &url, &src_url, &req_type).unwrap_or_else(|err| { + log::error!("{:?}", err); + false as jboolean + }) } - diff --git a/adblock-rs/src/wrapper.rs b/adblock-rs/src/wrapper.rs index 555fdec..724e36e 100644 --- a/adblock-rs/src/wrapper.rs +++ b/adblock-rs/src/wrapper.rs @@ -1,13 +1,13 @@ -use jni::JNIEnv; use jni::objects::{JObjectArray, JString}; use jni::sys::{jboolean, jlong}; +use jni::JNIEnv; use crate::adblock::AdvtBlocker; use crate::errors::RustException; pub(crate) fn init_object_wrapped( env: &mut JNIEnv, - rules: &JObjectArray + rules: &JObjectArray, ) -> Result { let conv_rules = extract_list_str(env, rules)?; Ok(AdvtBlocker::new(conv_rules)) @@ -29,12 +29,11 @@ pub(crate) fn check_net_urls_wrapped( let req_type_str = extract_str(env, req_type)?; - let check_result = advt_blocker - .check_network_urls( - url_str.as_str(), - src_url_str.as_str(), - req_type_str.as_str(), - )?; + let check_result = advt_blocker.check_network_urls( + url_str.as_str(), + src_url_str.as_str(), + req_type_str.as_str(), + )?; Ok(check_result as jboolean) } @@ -52,7 +51,10 @@ fn extract_str<'a>(env: &'a mut JNIEnv, j_obj: &'a JString) -> Result(env: &'a mut JNIEnv, j_obj_arr: &'a JObjectArray) -> Result, RustException> { +fn extract_list_str<'a>( + env: &'a mut JNIEnv, + j_obj_arr: &'a JObjectArray, +) -> Result, RustException> { let j_list = env .get_list(&j_obj_arr) .map_err(|err| RustException::ExtractParameter(err.to_string()))?; From a967e9c01eb8417ebfdcb547cbc0f2aeb04c5d27 Mon Sep 17 00:00:00 2001 From: breadrock Date: Fri, 5 Jul 2024 19:40:56 +0300 Subject: [PATCH 3/6] fix(fmt): Fixed actions --- .github/workflows/build.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index d2d44a1..1d55405 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -17,6 +17,7 @@ jobs: build: name: build project runs-on: ubuntu-latest + needs: formatting steps: - uses: actions/checkout@v4 - uses: actions-rust-lang/setup-rust-toolchain@v1 @@ -36,6 +37,7 @@ jobs: cargo-test: name: cargo test runs-on: ubuntu-latest + needs: build steps: - uses: actions/checkout@v4 - uses: actions-rust-lang/setup-rust-toolchain@v1 @@ -45,6 +47,7 @@ jobs: mvn-test: name: mvn test runs-on: ubuntu-latest + needs: build steps: - uses: actions/checkout@v4 - uses: actions-rust-lang/setup-rust-toolchain@v1 From 2ae419295a86e3b9c7e9ec051b409e4cc8ed25f6 Mon Sep 17 00:00:00 2001 From: breadrock Date: Fri, 5 Jul 2024 19:42:29 +0300 Subject: [PATCH 4/6] fix(fmt): Fixed actions --- .github/workflows/build.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 1d55405..37de09d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -9,10 +9,8 @@ jobs: steps: - uses: actions/checkout@v4 - uses: actions-rust-lang/setup-rust-toolchain@v1 - with: - components: rustfmt - name: Rustfmt Check - uses: actions-rust-lang/rustfmt@v1 + run: cargo fmt --check --manifest-path adblock-rs/Cargo.toml build: name: build project From fa141f827e86d99b75a11578bc505625e04b485a Mon Sep 17 00:00:00 2001 From: breadrock Date: Fri, 5 Jul 2024 19:44:02 +0300 Subject: [PATCH 5/6] fix(fmt): Fixed actions --- .github/workflows/build.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 37de09d..863e4e9 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -23,7 +23,7 @@ jobs: run: | cargo build --manifest-path adblock-rs/Cargo.toml mkdir -p ./target/lib - cp -r ./adblock-rs/target/release/libadblock_coffee.* ./target/lib/ + cp -r ./adblock-rs/target/debug/libadblock_coffee.* ./target/lib/ - uses: actions/setup-java@v4 with: java-version: '11' @@ -40,7 +40,7 @@ jobs: - uses: actions/checkout@v4 - uses: actions-rust-lang/setup-rust-toolchain@v1 - name: Build rust library - run: cargo test --all + run: cargo test --all --manifest-path adblock-rs/Cargo.toml mvn-test: name: mvn test @@ -53,7 +53,7 @@ jobs: run: | cargo build --manifest-path adblock-rs/Cargo.toml mkdir -p ./target/lib - cp -r ./adblock-rs/target/release/libadblock_coffee.* ./target/lib/ + cp -r ./adblock-rs/target/debug/libadblock_coffee.* ./target/lib/ - uses: actions/setup-java@v4 with: java-version: '11' From de9ba9bb89d19f215e36cf951a9e383394b726c9 Mon Sep 17 00:00:00 2001 From: breadrock Date: Fri, 5 Jul 2024 19:47:05 +0300 Subject: [PATCH 6/6] chore(version): Updated project version --- adblock-rs/Cargo.toml | 2 +- pom.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/adblock-rs/Cargo.toml b/adblock-rs/Cargo.toml index 1776304..b90b1eb 100644 --- a/adblock-rs/Cargo.toml +++ b/adblock-rs/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "adblock-coffee" -version = "0.1.0" +version = "0.1.3" edition = "2021" authors = ["Bread White "] diff --git a/pom.xml b/pom.xml index ac039a7..63f0b62 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.example.adblock adblock-coffee - 1.0.2 + 1.0.3 adblock-coffee