Skip to content

Commit

Permalink
Merge pull request #1 from breadrock1/test/test-pr-request
Browse files Browse the repository at this point in the history
Improve: Updated README.md file
  • Loading branch information
breadrock1 authored Jul 5, 2024
2 parents 722a0cd + de9ba9b commit a01f74a
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 85 deletions.
13 changes: 7 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,21 @@ 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
runs-on: ubuntu-latest
needs: formatting
steps:
- uses: actions/checkout@v4
- uses: actions-rust-lang/setup-rust-toolchain@v1
- name: Build rust library
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'
Expand All @@ -36,23 +35,25 @@ 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
- name: Build rust library
run: cargo test --all
run: cargo test --all --manifest-path adblock-rs/Cargo.toml

mvn-test:
name: mvn test
runs-on: ubuntu-latest
needs: build
steps:
- uses: actions/checkout@v4
- uses: actions-rust-lang/setup-rust-toolchain@v1
- name: Build rust library
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'
Expand Down
80 changes: 26 additions & 54 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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<String> rules = new ArrayList<>(List.of(
"-advertisement-icon.",
"-advertisement-management/",
"-advertisement.",
"-advertisement/script."
));

AdvtBlocker blocker = AdvtBlocker.createInstance(rules);
}
}
```
Expand All @@ -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<String> 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);
}
}
```
2 changes: 1 addition & 1 deletion adblock-rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "adblock-coffee"
version = "0.1.0"
version = "0.1.3"
edition = "2021"
authors = ["Bread White <[email protected]>"]

Expand Down
14 changes: 8 additions & 6 deletions adblock-rs/src/adblock.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use adblock::{Engine, FilterSet};
use adblock::lists::ParseOptions;
use adblock::request::Request;
use adblock::{Engine, FilterSet};

use crate::errors::RustException;

Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion adblock-rs/src/errors.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
12 changes: 5 additions & 7 deletions adblock-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

Expand Down Expand Up @@ -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
})
}

20 changes: 11 additions & 9 deletions adblock-rs/src/wrapper.rs
Original file line number Diff line number Diff line change
@@ -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<AdvtBlocker, RustException> {
let conv_rules = extract_list_str(env, rules)?;
Ok(AdvtBlocker::new(conv_rules))
Expand All @@ -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)
}
Expand All @@ -52,7 +51,10 @@ fn extract_str<'a>(env: &'a mut JNIEnv, j_obj: &'a JString) -> Result<String, Ru
Ok(str_obj.to_string())
}

fn extract_list_str<'a>(env: &'a mut JNIEnv, j_obj_arr: &'a JObjectArray) -> Result<Vec<String>, RustException> {
fn extract_list_str<'a>(
env: &'a mut JNIEnv,
j_obj_arr: &'a JObjectArray,
) -> Result<Vec<String>, RustException> {
let j_list = env
.get_list(&j_obj_arr)
.map_err(|err| RustException::ExtractParameter(err.to_string()))?;
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.example.adblock</groupId>
<artifactId>adblock-coffee</artifactId>
<version>1.0.2</version>
<version>1.0.3</version>
<name>adblock-coffee</name>

<properties>
Expand Down

0 comments on commit a01f74a

Please sign in to comment.