summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.github/workflows/ci.yml2
-rw-r--r--src/lib.rs113
2 files changed, 60 insertions, 55 deletions
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index da43d52..f6a2b85 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -30,7 +30,7 @@ jobs:
30 uses: actions-rs/cargo@v1 30 uses: actions-rs/cargo@v1
31 with: 31 with:
32 command: clippy 32 command: clippy
33 args: --all-targets --all-features 33 args: --all-targets --all-features -- -D clippy::all
34 34
35 - name: Lint with Black 35 - name: Lint with Black
36 run: pip install black && black --check . 36 run: pip install black && black --check .
diff --git a/src/lib.rs b/src/lib.rs
index 172f025..01b105c 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -7,7 +7,8 @@
7 trivial_casts, 7 trivial_casts,
8 trivial_numeric_casts, 8 trivial_numeric_casts,
9 unsafe_code, 9 unsafe_code,
10 unused_qualifications 10 unused_qualifications,
11 deprecated
11)] 12)]
12 13
13use adblock::blocker::BlockerError as RustBlockerError; 14use adblock::blocker::BlockerError as RustBlockerError;
@@ -99,15 +100,15 @@ pub struct BlockerResult {
99 pub error: Option<String>, 100 pub error: Option<String>,
100} 101}
101 102
102impl Into<BlockerResult> for RustBlockerResult { 103impl From<RustBlockerResult> for BlockerResult {
103 fn into(self) -> BlockerResult { 104 fn from(br: RustBlockerResult) -> Self {
104 BlockerResult { 105 Self {
105 matched: self.matched, 106 matched: br.matched,
106 important: self.important, 107 important: br.important,
107 redirect: self.redirect, 108 redirect: br.redirect,
108 exception: self.exception, 109 exception: br.exception,
109 filter: self.filter, 110 filter: br.filter,
110 error: self.error, 111 error: br.error,
111 } 112 }
112 } 113 }
113} 114}
@@ -166,27 +167,27 @@ create_exception!(adblock, OptimizedFilterExistence, BlockerException);
166create_exception!(adblock, BadFilterAddUnsupported, BlockerException); 167create_exception!(adblock, BadFilterAddUnsupported, BlockerException);
167create_exception!(adblock, FilterExists, BlockerException); 168create_exception!(adblock, FilterExists, BlockerException);
168 169
169impl Into<PyErr> for BlockerError { 170impl From<BlockerError> for PyErr {
170 fn into(self) -> PyErr { 171 fn from(err: BlockerError) -> Self {
171 let msg = format!("{:?}", self); 172 let msg = format!("{:?}", err);
172 match self { 173 match err {
173 Self::SerializationError => PyErr::new::<SerializationError, _>(msg), 174 BlockerError::SerializationError => Self::new::<SerializationError, _>(msg),
174 Self::DeserializationError => PyErr::new::<DeserializationError, _>(msg), 175 BlockerError::DeserializationError => Self::new::<DeserializationError, _>(msg),
175 Self::OptimizedFilterExistence => PyErr::new::<OptimizedFilterExistence, _>(msg), 176 BlockerError::OptimizedFilterExistence => Self::new::<OptimizedFilterExistence, _>(msg),
176 Self::BadFilterAddUnsupported => PyErr::new::<BadFilterAddUnsupported, _>(msg), 177 BlockerError::BadFilterAddUnsupported => Self::new::<BadFilterAddUnsupported, _>(msg),
177 Self::FilterExists => PyErr::new::<FilterExists, _>(msg), 178 BlockerError::FilterExists => Self::new::<FilterExists, _>(msg),
178 } 179 }
179 } 180 }
180} 181}
181 182
182impl Into<BlockerError> for RustBlockerError { 183impl From<RustBlockerError> for BlockerError {
183 fn into(self) -> BlockerError { 184 fn from(err: RustBlockerError) -> Self {
184 match self { 185 match err {
185 Self::SerializationError => BlockerError::SerializationError, 186 RustBlockerError::SerializationError => Self::SerializationError,
186 Self::DeserializationError => BlockerError::DeserializationError, 187 RustBlockerError::DeserializationError => Self::DeserializationError,
187 Self::OptimizedFilterExistence => BlockerError::OptimizedFilterExistence, 188 RustBlockerError::OptimizedFilterExistence => Self::OptimizedFilterExistence,
188 Self::BadFilterAddUnsupported => BlockerError::BadFilterAddUnsupported, 189 RustBlockerError::BadFilterAddUnsupported => Self::BadFilterAddUnsupported,
189 Self::FilterExists => BlockerError::FilterExists, 190 RustBlockerError::FilterExists => Self::FilterExists,
190 } 191 }
191 } 192 }
192} 193}
@@ -206,7 +207,7 @@ fn filter_format_from_string(filter_format: &str) -> PyResult<FilterFormat> {
206/// created. FilterSet allows assembling a compound list from multiple 207/// created. FilterSet allows assembling a compound list from multiple
207/// different sources before compiling the rules into an Engine. 208/// different sources before compiling the rules into an Engine.
208#[pyclass] 209#[pyclass]
209#[text_signature = "($self, debug)"] 210#[pyo3(text_signature = "($self, debug)")]
210#[derive(Clone)] 211#[derive(Clone)]
211pub struct FilterSet { 212pub struct FilterSet {
212 filter_set: RustFilterSet, 213 filter_set: RustFilterSet,
@@ -233,7 +234,7 @@ impl FilterSet {
233 /// 234 ///
234 /// The format is a string containing either "standard" (ABP/uBO-style) 235 /// The format is a string containing either "standard" (ABP/uBO-style)
235 /// or "hosts". 236 /// or "hosts".
236 #[text_signature = "($self, filter_list, format)"] 237 #[pyo3(text_signature = "($self, filter_list, format)")]
237 #[args(filter_list, format = "\"standard\"")] 238 #[args(filter_list, format = "\"standard\"")]
238 pub fn add_filter_list(&mut self, filter_list: &str, format: &str) -> PyResult<()> { 239 pub fn add_filter_list(&mut self, filter_list: &str, format: &str) -> PyResult<()> {
239 let filter_format = filter_format_from_string(format)?; 240 let filter_format = filter_format_from_string(format)?;
@@ -246,7 +247,7 @@ impl FilterSet {
246 /// 247 ///
247 /// The format is a string containing either "standard" (ABP/uBO-style) 248 /// The format is a string containing either "standard" (ABP/uBO-style)
248 /// or "hosts". 249 /// or "hosts".
249 #[text_signature = "($self, filters, format)"] 250 #[pyo3(text_signature = "($self, filters, format)")]
250 #[args(filters, format = "\"standard\"")] 251 #[args(filters, format = "\"standard\"")]
251 pub fn add_filters(&mut self, filters: Vec<String>, format: &str) -> PyResult<()> { 252 pub fn add_filters(&mut self, filters: Vec<String>, format: &str) -> PyResult<()> {
252 let filter_format = filter_format_from_string(format)?; 253 let filter_format = filter_format_from_string(format)?;
@@ -291,14 +292,14 @@ pub struct UrlSpecificResources {
291 pub generichide: bool, 292 pub generichide: bool,
292} 293}
293 294
294impl Into<UrlSpecificResources> for RustUrlSpecificResources { 295impl From<RustUrlSpecificResources> for UrlSpecificResources {
295 fn into(self) -> UrlSpecificResources { 296 fn from(r: RustUrlSpecificResources) -> Self {
296 UrlSpecificResources { 297 Self {
297 hide_selectors: self.hide_selectors, 298 hide_selectors: r.hide_selectors,
298 style_selectors: self.style_selectors, 299 style_selectors: r.style_selectors,
299 exceptions: self.exceptions, 300 exceptions: r.exceptions,
300 injected_script: self.injected_script, 301 injected_script: r.injected_script,
301 generichide: self.generichide, 302 generichide: r.generichide,
302 } 303 }
303 } 304 }
304} 305}
@@ -337,7 +338,7 @@ impl PyObjectProtocol for UrlSpecificResources {
337/// 338///
338/// [1]: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webRequest/ResourceType 339/// [1]: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webRequest/ResourceType
339#[pyclass] 340#[pyclass]
340#[text_signature = "($self, filter_set, optimize)"] 341#[pyo3(text_signature = "($self, filter_set, optimize)")]
341pub struct Engine { 342pub struct Engine {
342 engine: RustEngine, 343 engine: RustEngine,
343 optimize: bool, 344 optimize: bool,
@@ -361,7 +362,7 @@ impl Engine {
361 /// * `url` - The URL of the request to check 362 /// * `url` - The URL of the request to check
362 /// * `source_url` - The URL from where the request is made 363 /// * `source_url` - The URL from where the request is made
363 /// * `request_type` - The resource type that the request points to 364 /// * `request_type` - The resource type that the request points to
364 #[text_signature = "($self, url, source_url, request_type)"] 365 #[pyo3(text_signature = "($self, url, source_url, request_type)")]
365 pub fn check_network_urls( 366 pub fn check_network_urls(
366 &self, 367 &self,
367 url: &str, 368 url: &str,
@@ -384,7 +385,9 @@ impl Engine {
384 /// * `third_party_request` - Is the given request to a third-party? Here, 385 /// * `third_party_request` - Is the given request to a third-party? Here,
385 /// `None` can be given and the engine will figure it out based on the 386 /// `None` can be given and the engine will figure it out based on the
386 /// `hostname` and `source_hostname`. 387 /// `hostname` and `source_hostname`.
387 #[text_signature = "($self, url, hostname, source_hostname, requsest_type, third_party_request)"] 388 #[pyo3(
389 text_signature = "($self, url, hostname, source_hostname, requsest_type, third_party_request)"
390 )]
388 pub fn check_network_urls_with_hostnames( 391 pub fn check_network_urls_with_hostnames(
389 &self, 392 &self,
390 url: &str, 393 url: &str,
@@ -416,8 +419,10 @@ impl Engine {
416 /// * `previously_matched_rule` - Return a match as long as there are no 419 /// * `previously_matched_rule` - Return a match as long as there are no
417 /// exceptions 420 /// exceptions
418 /// * `force_check_exceptions` - Check exceptions even if no other rule matches 421 /// * `force_check_exceptions` - Check exceptions even if no other rule matches
419 #[text_signature = "($self, url, hostname, source_hostname, request_type, \ 422 #[pyo3(
420 third_party_request, previously_matched_rule, force_check_exceptions)"] 423 text_signature = "($self, url, hostname, source_hostname, request_type, \
424 third_party_request, previously_matched_rule, force_check_exceptions)"
425 )]
421 #[allow(clippy::too_many_arguments)] 426 #[allow(clippy::too_many_arguments)]
422 pub fn check_network_urls_with_hostnames_subset( 427 pub fn check_network_urls_with_hostnames_subset(
423 &self, 428 &self,
@@ -443,7 +448,7 @@ impl Engine {
443 448
444 /// Serialize this blocking engine to bytes. They can then be deserialized 449 /// Serialize this blocking engine to bytes. They can then be deserialized
445 /// using `deserialize()` to get the same engine again. 450 /// using `deserialize()` to get the same engine again.
446 #[text_signature = "($self)"] 451 #[pyo3(text_signature = "($self)")]
447 pub fn serialize<'p>(&mut self, py: Python<'p>) -> PyResult<&'p PyBytes> { 452 pub fn serialize<'p>(&mut self, py: Python<'p>) -> PyResult<&'p PyBytes> {
448 let bytes = self.serialize_inner()?; 453 let bytes = self.serialize_inner()?;
449 let py_bytes = PyBytes::new(py, &bytes); 454 let py_bytes = PyBytes::new(py, &bytes);
@@ -464,7 +469,7 @@ impl Engine {
464 /// Serialize this blocking engine to a file. The file can then be 469 /// Serialize this blocking engine to a file. The file can then be
465 /// deserialized using `deserialize_from_file()` to get the same engine 470 /// deserialized using `deserialize_from_file()` to get the same engine
466 /// again. 471 /// again.
467 #[text_signature = "($self, file)"] 472 #[pyo3(text_signature = "($self, file)")]
468 pub fn serialize_to_file(&mut self, file: &str) -> PyResult<()> { 473 pub fn serialize_to_file(&mut self, file: &str) -> PyResult<()> {
469 let data = self.serialize_inner()?; 474 let data = self.serialize_inner()?;
470 let mut fd = fs::OpenOptions::new() 475 let mut fd = fs::OpenOptions::new()
@@ -477,7 +482,7 @@ impl Engine {
477 } 482 }
478 483
479 /// Deserialize a blocking engine from bytes produced with `serialize()`. 484 /// Deserialize a blocking engine from bytes produced with `serialize()`.
480 #[text_signature = "($self, serialized)"] 485 #[pyo3(text_signature = "($self, serialized)")]
481 pub fn deserialize(&mut self, serialized: &[u8]) -> PyResult<()> { 486 pub fn deserialize(&mut self, serialized: &[u8]) -> PyResult<()> {
482 let result = self.engine.deserialize(serialized); 487 let result = self.engine.deserialize(serialized);
483 match result { 488 match result {
@@ -491,7 +496,7 @@ impl Engine {
491 496
492 /// Deserialize a blocking engine from file produced with 497 /// Deserialize a blocking engine from file produced with
493 /// `serialize_to_file()`. 498 /// `serialize_to_file()`.
494 #[text_signature = "($self, file)"] 499 #[pyo3(text_signature = "($self, file)")]
495 pub fn deserialize_from_file(&mut self, file: &str) -> PyResult<()> { 500 pub fn deserialize_from_file(&mut self, file: &str) -> PyResult<()> {
496 let mut fd = fs::File::open(file)?; 501 let mut fd = fs::File::open(file)?;
497 let mut data: Vec<u8> = Vec::new(); 502 let mut data: Vec<u8> = Vec::new();
@@ -500,7 +505,7 @@ impl Engine {
500 } 505 }
501 506
502 /// Checks if the given filter exists in the blocking engine. 507 /// Checks if the given filter exists in the blocking engine.
503 #[text_signature = "($self, filter)"] 508 #[pyo3(text_signature = "($self, filter)")]
504 pub fn filter_exists(&self, filter: &str) -> bool { 509 pub fn filter_exists(&self, filter: &str) -> bool {
505 self.engine.filter_exists(filter) 510 self.engine.filter_exists(filter)
506 } 511 }
@@ -509,7 +514,7 @@ impl Engine {
509 /// 514 ///
510 /// Tags can be used to cheaply enable or disable network rules with a 515 /// Tags can be used to cheaply enable or disable network rules with a
511 /// corresponding $tag option. 516 /// corresponding $tag option.
512 #[text_signature = "($self, tags)"] 517 #[pyo3(text_signature = "($self, tags)")]
513 pub fn use_tags(&mut self, tags: Vec<&str>) { 518 pub fn use_tags(&mut self, tags: Vec<&str>) {
514 self.engine.use_tags(&tags); 519 self.engine.use_tags(&tags);
515 } 520 }
@@ -519,7 +524,7 @@ impl Engine {
519 /// 524 ///
520 /// Tags can be used to cheaply enable or disable network rules with a 525 /// Tags can be used to cheaply enable or disable network rules with a
521 /// corresponding $tag option. 526 /// corresponding $tag option.
522 #[text_signature = "($self, tags)"] 527 #[pyo3(text_signature = "($self, tags)")]
523 pub fn enable_tags(&mut self, tags: Vec<&str>) { 528 pub fn enable_tags(&mut self, tags: Vec<&str>) {
524 self.engine.enable_tags(&tags); 529 self.engine.enable_tags(&tags);
525 } 530 }
@@ -529,7 +534,7 @@ impl Engine {
529 /// 534 ///
530 /// Tags can be used to cheaply enable or disable network rules with a 535 /// Tags can be used to cheaply enable or disable network rules with a
531 /// corresponding $tag option. 536 /// corresponding $tag option.
532 #[text_signature = "($self, tags)"] 537 #[pyo3(text_signature = "($self, tags)")]
533 pub fn disable_tags(&mut self, tags: Vec<&str>) { 538 pub fn disable_tags(&mut self, tags: Vec<&str>) {
534 self.engine.disable_tags(&tags); 539 self.engine.disable_tags(&tags);
535 } 540 }
@@ -538,7 +543,7 @@ impl Engine {
538 /// 543 ///
539 /// Tags can be used to cheaply enable or disable network rules with a 544 /// Tags can be used to cheaply enable or disable network rules with a
540 /// corresponding $tag option. 545 /// corresponding $tag option.
541 #[text_signature = "($self, tag)"] 546 #[pyo3(text_signature = "($self, tag)")]
542 pub fn tag_exists(&self, tag: &str) -> bool { 547 pub fn tag_exists(&self, tag: &str) -> bool {
543 self.engine.tag_exists(tag) 548 self.engine.tag_exists(tag)
544 } 549 }
@@ -547,7 +552,7 @@ impl Engine {
547 /// url. Once this has been called, all CSS ids and classes on a 552 /// url. Once this has been called, all CSS ids and classes on a
548 /// page should be passed to hidden_class_id_selectors to obtain any 553 /// page should be passed to hidden_class_id_selectors to obtain any
549 /// stylesheets consisting of generic rules. 554 /// stylesheets consisting of generic rules.
550 #[text_signature = "($self, url)"] 555 #[pyo3(text_signature = "($self, url)")]
551 pub fn url_cosmetic_resources(&self, url: &str) -> UrlSpecificResources { 556 pub fn url_cosmetic_resources(&self, url: &str) -> UrlSpecificResources {
552 self.engine.url_cosmetic_resources(url).into() 557 self.engine.url_cosmetic_resources(url).into()
553 } 558 }
@@ -559,7 +564,7 @@ impl Engine {
559 /// are not excepted. 564 /// are not excepted.
560 /// 565 ///
561 /// Exceptions should be passed directly from UrlSpecificResources. 566 /// Exceptions should be passed directly from UrlSpecificResources.
562 #[text_signature = "($self, classes, ids, exceptions)"] 567 #[pyo3(text_signature = "($self, classes, ids, exceptions)")]
563 pub fn hidden_class_id_selectors( 568 pub fn hidden_class_id_selectors(
564 &self, 569 &self,
565 classes: Vec<String>, 570 classes: Vec<String>,