summaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
index dbed144..fbe8e37 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -34,20 +34,49 @@ fn adblock(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
34 Ok(()) 34 Ok(())
35} 35}
36 36
37/// The result of an ad-blocking check.
37#[pyclass] 38#[pyclass]
38pub struct BlockerResult { 39pub struct BlockerResult {
39 #[pyo3(get)] 40 #[pyo3(get)]
40 pub matched: bool, 41 pub matched: bool,
42 /// Normally, Brave Browser returns `200 OK` with an empty body when
43 /// `matched` is `True`, except if `explicit_cancel` is also `True`, in
44 /// which case the request is cancelled.
41 #[pyo3(get)] 45 #[pyo3(get)]
42 pub explicit_cancel: bool, 46 pub explicit_cancel: bool,
47 /// Important is used to signal that a rule with the `important` option
48 /// matched. An `important` match means that exceptions should not apply
49 /// and no further checking is neccesary--the request should be blocked
50 /// (empty body or cancelled).
51 ///
52 /// Brave Browser keeps seperate instances of Blocker for default lists
53 /// and regional ones, so `important` here is used to correct behaviour
54 /// between them: checking should stop instead of moving to the next
55 /// instance iff an `important` rule matched.
43 #[pyo3(get)] 56 #[pyo3(get)]
44 pub important: bool, 57 pub important: bool,
58 /// Iff the blocker matches a rule which has the `redirect` option, as per
59 /// [uBlock Origin's redirect syntax][1], the `redirect` is not `None`.
60 /// The `redirect` field contains the body of the redirect to be injected.
61 ///
62 /// [1]: https://github.com/gorhill/uBlock/wiki/Static-filter-syntax#redirect
45 #[pyo3(get)] 63 #[pyo3(get)]
46 pub redirect: Option<String>, 64 pub redirect: Option<String>,
65 /// Exception is not `None` when the blocker matched on an exception rule.
66 /// Effectively this means that there was a match, but the request should
67 /// not be blocked. It is a non-empty string if the blocker was initialized
68 /// from a list of rules with debugging enabled, otherwise the original
69 /// string representation is discarded to reduce memory use.
47 #[pyo3(get)] 70 #[pyo3(get)]
48 pub exception: Option<String>, 71 pub exception: Option<String>,
72 /// Filter--similarly to exception--includes the string representation of
73 /// the rule when there is a match and debugging is enabled. Otherwise, on
74 /// a match, it is not `None`.
49 #[pyo3(get)] 75 #[pyo3(get)]
50 pub filter: Option<String>, 76 pub filter: Option<String>,
77 /// The `error` field is only used to signal that there was an error in
78 /// parsing the provided URLs when using the simpler
79 /// `check_network_urls` method.
51 #[pyo3(get)] 80 #[pyo3(get)]
52 pub error: Option<String>, 81 pub error: Option<String>,
53} 82}
@@ -241,6 +270,8 @@ impl Engine {
241 blocker_result.into() 270 blocker_result.into()
242 } 271 }
243 272
273 /// Serialize this blocking engine to bytes. They can then be deserialized
274 /// using `deserialize()` to get the same engine again.
244 pub fn serialize(&mut self) -> PyResult<Vec<u8>> { 275 pub fn serialize(&mut self) -> PyResult<Vec<u8>> {
245 let result = self.engine.serialize(); 276 let result = self.engine.serialize();
246 match result { 277 match result {
@@ -252,6 +283,9 @@ impl Engine {
252 } 283 }
253 } 284 }
254 285
286 /// Serialize this blocking engine to a file. The file can then be
287 /// deserialized using `deserialize_from_file()` to get the same engine
288 /// again.
255 pub fn serialize_to_file(&mut self, file: &str) -> PyResult<()> { 289 pub fn serialize_to_file(&mut self, file: &str) -> PyResult<()> {
256 let data = self.serialize()?; 290 let data = self.serialize()?;
257 let mut fd = fs::OpenOptions::new() 291 let mut fd = fs::OpenOptions::new()
@@ -263,6 +297,7 @@ impl Engine {
263 Ok(()) 297 Ok(())
264 } 298 }
265 299
300 /// Deserialize a blocking engine from bytes produced with `serialize()`.
266 pub fn deserialize(&mut self, serialized: &[u8]) -> PyResult<()> { 301 pub fn deserialize(&mut self, serialized: &[u8]) -> PyResult<()> {
267 let result = self.engine.deserialize(serialized); 302 let result = self.engine.deserialize(serialized);
268 match result { 303 match result {
@@ -274,6 +309,8 @@ impl Engine {
274 } 309 }
275 } 310 }
276 311
312 /// Deserialize a blocking engine from file produced with
313 /// `serialize_to_file()`.
277 pub fn deserialize_from_file(&mut self, file: &str) -> PyResult<()> { 314 pub fn deserialize_from_file(&mut self, file: &str) -> PyResult<()> {
278 let mut fd = fs::File::open(file)?; 315 let mut fd = fs::File::open(file)?;
279 let mut data: Vec<u8> = Vec::new(); 316 let mut data: Vec<u8> = Vec::new();
@@ -281,10 +318,12 @@ impl Engine {
281 self.deserialize(&data) 318 self.deserialize(&data)
282 } 319 }
283 320
321 /// Add the contents of a block list file to the blocking engine.
284 pub fn add_filter_list(&mut self, filter_list: &str) { 322 pub fn add_filter_list(&mut self, filter_list: &str) {
285 self.engine.add_filter_list(filter_list); 323 self.engine.add_filter_list(filter_list);
286 } 324 }
287 325
326 /// Checks if the given filter exists in the blocking engine.
288 pub fn filter_exists(&self, filter: &str) -> bool { 327 pub fn filter_exists(&self, filter: &str) -> bool {
289 self.engine.filter_exists(filter) 328 self.engine.filter_exists(filter)
290 } 329 }