Skip to content

Commit 09b7b88

Browse files
committed
Added Serialize trait for engine.
1 parent d2f45e5 commit 09b7b88

18 files changed

+122
-48
lines changed

benches/bench_matching.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use adblock::blocker::{Blocker, BlockerOptions};
66
use adblock::request::Request;
77
use adblock::resources::ResourceStorage;
88
use adblock::url_parser::parse_url;
9-
use adblock::Engine;
9+
use adblock::{Engine, Serialize as _};
1010

1111
#[path = "../tests/test_utils.rs"]
1212
mod test_utils;

examples/deserialization.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use adblock::{request::Request, Engine};
1+
use adblock::{request::Request, Engine, Serialize};
22

33
use serde::Deserialize;
44

examples/example.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,7 @@ use adblock::{
55
};
66

77
fn main() {
8-
let rules = vec![
9-
String::from("-advertisement-icon."),
10-
String::from("-advertisement-management/"),
11-
String::from("-advertisement."),
12-
String::from("-advertisement/script."),
13-
];
8+
let rules = vec![String::from("||yandex.*/clck/$~ping")];
149

1510
let debug_info = true;
1611
let mut filter_set = FilterSet::new(debug_info);
@@ -19,9 +14,9 @@ fn main() {
1914
let engine = Engine::from_filter_set(filter_set, true);
2015

2116
let request = Request::new(
22-
"http://example.com/-advertisement-icon.",
23-
"http://example.com/helloworld",
24-
"image",
17+
"https://yandex.ru/clck/counter",
18+
"https://www.yandex.ru/",
19+
"other",
2520
)
2621
.unwrap();
2722
let blocker_result = engine.check_network_request(&request);

examples/generate-dat.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use adblock::{request::Request, Engine};
1+
use adblock::{request::Request, Engine, Serialize};
22

33
use std::fs::File;
44
use std::io::prelude::*;

examples/use-dat.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use adblock::{request::Request, Engine};
1+
use adblock::{request::Request, Engine, Serialize};
22

33
use std::fs::File;
44
use std::io::prelude::*;

js/Cargo.lock

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::cell::RefCell;
55
use std::sync::Mutex;
66
use std::path::Path;
77
use adblock::Engine as EngineInternal;
8+
use adblock::Serialize as SerializeInternal;
89
use adblock::lists::{RuleTypes, FilterFormat, FilterListMetadata, FilterSet as FilterSetInternal, ParseOptions};
910
use adblock::resources::Resource;
1011
use adblock::resources::resource_assembler::assemble_web_accessible_resources;

src/blocker.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ use std::ops::DerefMut;
88
use thiserror::Error;
99

1010
use crate::filters::network::{NetworkFilter, NetworkFilterMaskHelper};
11-
pub(crate) use crate::network_filter_list::{NetworkFilterList, NetworkFilterListTrait};
11+
pub(crate) use crate::network_filter_list::NetworkFilterListTrait;
12+
13+
#[allow(unused_imports)]
14+
pub(crate) use crate::network_filter_list::NetworkFilterList;
15+
1216
use crate::regex_manager::{RegexManager, RegexManagerDiscardPolicy};
1317
use crate::request::Request;
1418
use crate::resources::ResourceStorage;
@@ -88,7 +92,7 @@ pub enum BlockerError {
8892
static NO_TAGS: Lazy<HashSet<String>> = Lazy::new(HashSet::new);
8993

9094
#[cfg(feature = "flatbuffers")]
91-
pub type Blocker = GenericBlocker<crate::network_filter_list::NetworkFilterList>;
95+
pub type Blocker = GenericBlocker<crate::network_filter_list::FlatNetworkFilterList>;
9296

9397
#[cfg(not(feature = "flatbuffers"))]
9498
pub type Blocker = GenericBlocker<crate::network_filter_list::NetworkFilterList>;

src/cosmetic_filter_cache.rs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
//! cosmetic filters and allows them to be queried efficiently at runtime for any which may be
99
//! relevant to a particular page.
1010
11+
#![allow(dead_code)]
12+
1113
use crate::filters::cosmetic::{
1214
CosmeticFilter, CosmeticFilterAction, CosmeticFilterMask, CosmeticFilterOperator,
1315
};

src/data_format/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
//! serialization/deserialization implementations and can automatically dispatch to the appropriate
66
//! one.
77
8+
#![allow(dead_code)]
9+
810
mod v0;
911

1012
pub(crate) mod utils;

src/engine.rs

+4-28
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use crate::regex_manager::RegexManagerDiscardPolicy;
77
use crate::request::Request;
88
use crate::resources::{Resource, ResourceStorage};
99

10+
pub use crate::engine_serializer::Serialize;
11+
1012
use std::collections::HashSet;
1113

1214
/// Drives high-level blocking logic and is responsible for loading filter lists into an optimized
@@ -43,8 +45,8 @@ use std::collections::HashSet;
4345
/// [`Engine::hidden_class_id_selectors`] on an ongoing basis to determine additional elements that
4446
/// should be hidden dynamically.
4547
pub struct Engine {
46-
blocker: Blocker,
47-
cosmetic_cache: CosmeticFilterCache,
48+
pub(crate) blocker: Blocker,
49+
pub(crate) cosmetic_cache: CosmeticFilterCache,
4850
resources: ResourceStorage,
4951
}
5052

@@ -121,32 +123,6 @@ impl Engine {
121123
}
122124
}
123125

124-
/// Serializes the `Engine` into a binary format so that it can be quickly reloaded later.
125-
pub fn serialize_raw(&self) -> Result<Vec<u8>, crate::data_format::SerializationError> {
126-
use crate::data_format::SerializeFormat;
127-
128-
let serialize_format = SerializeFormat::build(&self.blocker, &self.cosmetic_cache);
129-
130-
serialize_format.serialize()
131-
}
132-
133-
/// Deserialize the `Engine` from the binary format generated by `Engine::serialize_raw`. The
134-
/// method will automatically select the correct deserialization implementation.
135-
pub fn deserialize(
136-
&mut self,
137-
serialized: &[u8],
138-
) -> Result<(), crate::data_format::DeserializationError> {
139-
use crate::data_format::DeserializeFormat;
140-
let current_tags = self.blocker.tags_enabled();
141-
let deserialize_format = DeserializeFormat::deserialize(serialized)?;
142-
let (blocker, cosmetic_cache) = deserialize_format.build();
143-
self.blocker = blocker;
144-
self.blocker
145-
.use_tags(&current_tags.iter().map(|s| &**s).collect::<Vec<_>>());
146-
self.cosmetic_cache = cosmetic_cache;
147-
Ok(())
148-
}
149-
150126
/// Check if a request for a network resource from `url`, of type `request_type`, initiated by
151127
/// `source_url`, should be blocked.
152128
pub fn check_network_request(&self, request: &Request) -> BlockerResult {

src/engine_serializer.rs

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
use crate::engine::Engine;
2+
3+
#[derive(Debug)]
4+
pub enum SerializeError {
5+
#[cfg(not(feature = "flatbuffers"))]
6+
DataFormatError(crate::data_format::SerializationError),
7+
8+
#[cfg(feature = "flatbuffers")]
9+
FlatbuffersError(),
10+
}
11+
12+
#[derive(Debug)]
13+
pub enum DeserializeError {
14+
#[cfg(not(feature = "flatbuffers"))]
15+
DataFormatError(crate::data_format::DeserializationError),
16+
17+
#[cfg(feature = "flatbuffers")]
18+
FlatbuffersError(),
19+
}
20+
21+
#[cfg(not(feature = "flatbuffers"))]
22+
impl From<crate::data_format::SerializationError> for SerializeError {
23+
fn from(value: crate::data_format::SerializationError) -> Self {
24+
SerializeError::DataFormatError(value)
25+
}
26+
}
27+
28+
#[cfg(not(feature = "flatbuffers"))]
29+
impl From<crate::data_format::DeserializationError> for DeserializeError {
30+
fn from(value: crate::data_format::DeserializationError) -> Self {
31+
DeserializeError::DataFormatError(value)
32+
}
33+
}
34+
35+
pub trait Serialize {
36+
fn serialize_raw(&self) -> Result<Vec<u8>, SerializeError>;
37+
fn deserialize(&mut self, serialized: &[u8]) -> Result<(), DeserializeError>;
38+
}
39+
40+
#[cfg(feature = "flatbuffers")]
41+
impl Serialize for Engine {
42+
fn serialize_raw(&self) -> Result<Vec<u8>, SerializeError> {
43+
Err(SerializeError::FlatbuffersError())
44+
}
45+
46+
fn deserialize(&mut self, _serialized: &[u8]) -> Result<(), DeserializeError> {
47+
Err(DeserializeError::FlatbuffersError())
48+
}
49+
}
50+
51+
#[cfg(not(feature = "flatbuffers"))]
52+
impl Serialize for Engine {
53+
/// Serializes the `Engine` into a binary format so that it can be quickly reloaded later.
54+
fn serialize_raw(&self) -> Result<Vec<u8>, SerializeError> {
55+
use crate::data_format::SerializeFormat;
56+
57+
let serialize_format = SerializeFormat::build(&self.blocker, &self.cosmetic_cache);
58+
59+
let result = serialize_format.serialize()?;
60+
Ok(result)
61+
}
62+
63+
/// Deserialize the `Engine` from the binary format generated by `Engine::serialize_raw`. The
64+
/// method will automatically select the correct deserialization implementation.
65+
fn deserialize(&mut self, serialized: &[u8]) -> Result<(), DeserializeError> {
66+
use crate::data_format::DeserializeFormat;
67+
let current_tags = self.blocker.tags_enabled();
68+
let deserialize_format = DeserializeFormat::deserialize(serialized)?;
69+
let (blocker, cosmetic_cache) = deserialize_format.build();
70+
self.blocker = blocker;
71+
self.blocker
72+
.use_tags(&current_tags.iter().map(|s| &**s).collect::<Vec<_>>());
73+
self.cosmetic_cache = cosmetic_cache;
74+
Ok(())
75+
}
76+
}

src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ pub mod blocker;
2020
#[cfg(feature = "content-blocking")]
2121
pub mod content_blocking;
2222
pub mod cosmetic_filter_cache;
23+
#[cfg(not(feature = "flatbuffers"))]
2324
mod data_format;
2425
mod engine;
26+
mod engine_serializer;
2527
pub mod filters;
2628
pub mod lists;
2729
pub mod network_filter_list;
@@ -35,6 +37,7 @@ pub mod utils;
3537

3638
#[doc(inline)]
3739
pub use engine::Engine;
40+
pub use engine_serializer::Serialize;
3841
#[doc(inline)]
3942
pub use lists::FilterSet;
4043

src/network_filter_list.rs

+4
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,12 @@ pub trait NetworkFilterListTrait {
5656

5757
#[derive(Serialize, Deserialize, Default)]
5858
pub struct NetworkFilterList {
59+
#[cfg(not(feature = "flatbuffers"))]
5960
#[serde(serialize_with = "crate::data_format::utils::stabilize_hashmap_serialization")]
6061
pub(crate) filter_map: HashMap<Hash, Vec<Arc<NetworkFilter>>>,
62+
63+
#[cfg(feature = "flatbuffers")]
64+
pub(crate) filter_map: HashMap<Hash, Vec<Arc<NetworkFilter>>>,
6165
}
6266

6367
impl NetworkFilterListTrait for NetworkFilterList {

tests/deserialization.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use adblock::request::Request;
2-
use adblock::Engine;
2+
use adblock::{Engine, Serialize};
33

44
use serde::Deserialize;
55

tests/legacy_harness.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ mod legacy_test_filters {
316316

317317
mod legacy_check_match {
318318
use adblock::request::Request;
319-
use adblock::Engine;
319+
use adblock::{Engine, Serialize};
320320

321321
fn check_match<'a>(
322322
rules: &[&'a str],
@@ -854,7 +854,7 @@ mod legacy_check_options {
854854
mod legacy_misc_tests {
855855
use adblock::filters::network::NetworkFilter;
856856
use adblock::request::Request;
857-
use adblock::Engine;
857+
use adblock::{Engine, Serialize};
858858

859859
#[test]
860860
fn demo_app() {

tests/live.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use adblock::request::Request;
2-
use adblock::Engine;
2+
use adblock::{Engine, Serialize};
33

44
use serde::Deserialize;
55
use tokio::runtime::Runtime;

tests/ublock-coverage.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use adblock::request::Request;
2-
use adblock::Engine;
2+
use adblock::{Engine, Serialize};
33

44
use serde::Deserialize;
55

0 commit comments

Comments
 (0)