summaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs54
1 files changed, 37 insertions, 17 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 9117254..2d60293 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -16,8 +16,7 @@ use adblock::blocker::{BlockerError as RustBlockerError, Redirection};
16use adblock::cosmetic_filter_cache::UrlSpecificResources as RustUrlSpecificResources; 16use adblock::cosmetic_filter_cache::UrlSpecificResources as RustUrlSpecificResources;
17use adblock::engine::Engine as RustEngine; 17use adblock::engine::Engine as RustEngine;
18use adblock::lists::FilterSet as RustFilterSet; 18use adblock::lists::FilterSet as RustFilterSet;
19use adblock::lists::{FilterFormat, ParseOptions}; 19use adblock::lists::{FilterFormat, ParseOptions, RuleTypes};
20use pyo3::class::PyObjectProtocol;
21use pyo3::create_exception; 20use pyo3::create_exception;
22use pyo3::exceptions::PyException; 21use pyo3::exceptions::PyException;
23use pyo3::prelude::*; 22use pyo3::prelude::*;
@@ -145,8 +144,8 @@ impl From<RustBlockerResult> for BlockerResult {
145 } 144 }
146} 145}
147 146
148#[pyproto] 147#[pymethods]
149impl PyObjectProtocol for BlockerResult { 148impl BlockerResult {
150 fn __repr__(&self) -> PyResult<String> { 149 fn __repr__(&self) -> PyResult<String> {
151 Ok(format!( 150 Ok(format!(
152 "BlockerResult(matched={}, important={}, redirect={}, exception={}, filter={}, error={})", 151 "BlockerResult(matched={}, important={}, redirect={}, exception={}, filter={}, error={})",
@@ -231,7 +230,18 @@ fn filter_format_from_string(filter_format: &str) -> PyResult<FilterFormat> {
231 match filter_format { 230 match filter_format {
232 "standard" => Ok(FilterFormat::Standard), 231 "standard" => Ok(FilterFormat::Standard),
233 "hosts" => Ok(FilterFormat::Hosts), 232 "hosts" => Ok(FilterFormat::Hosts),
234 _ => Err(PyErr::new::<AdblockException, _>("Invalid format value")), 233 _ => Err(PyErr::new::<AdblockException, _>(
234 "Invalid FilterFormat value",
235 )),
236 }
237}
238
239fn rule_types_from_string(rule_types: &str) -> PyResult<RuleTypes> {
240 match rule_types {
241 "all" => Ok(RuleTypes::All),
242 "networkonly" => Ok(RuleTypes::NetworkOnly),
243 "cosmeticonly" => Ok(RuleTypes::CosmeticOnly),
244 _ => Err(PyErr::new::<AdblockException, _>("Invalid RuleTypes value")),
235 } 245 }
236} 246}
237 247
@@ -269,20 +279,28 @@ impl FilterSet {
269 /// 279 ///
270 /// The format is a string containing either "standard" (ABP/uBO-style) 280 /// The format is a string containing either "standard" (ABP/uBO-style)
271 /// or "hosts". 281 /// or "hosts".
272 #[pyo3(text_signature = "($self, filter_list, format, include_redirect_urls)")] 282 #[pyo3(text_signature = "($self, filter_list, format, include_redirect_urls, rule_types)")]
273 #[args(filter_list, format = "\"standard\"", include_redirect_urls = "false")] 283 #[args(
284 filter_list,
285 format = "\"standard\"",
286 include_redirect_urls = "false",
287 rule_types = "\"all\""
288 )]
274 pub fn add_filter_list( 289 pub fn add_filter_list(
275 &mut self, 290 &mut self,
276 filter_list: &str, 291 filter_list: &str,
277 format: &str, 292 format: &str,
278 include_redirect_urls: bool, 293 include_redirect_urls: bool,
294 rule_types: &str,
279 ) -> PyResult<()> { 295 ) -> PyResult<()> {
280 let filter_format = filter_format_from_string(format)?; 296 let filter_format = filter_format_from_string(format)?;
297 let rule_types = rule_types_from_string(rule_types)?;
281 self.filter_set.add_filter_list( 298 self.filter_set.add_filter_list(
282 filter_list, 299 filter_list,
283 ParseOptions { 300 ParseOptions {
284 format: filter_format, 301 format: filter_format,
285 include_redirect_urls, 302 include_redirect_urls,
303 rule_types,
286 }, 304 },
287 ); 305 );
288 Ok(()) 306 Ok(())
@@ -293,28 +311,33 @@ impl FilterSet {
293 /// 311 ///
294 /// The format is a string containing either "standard" (ABP/uBO-style) 312 /// The format is a string containing either "standard" (ABP/uBO-style)
295 /// or "hosts". 313 /// or "hosts".
296 #[pyo3(text_signature = "($self, filters, format, include_redirect_urls)")] 314 #[pyo3(text_signature = "($self, filters, format, include_redirect_urls, rule_types)")]
297 #[args(filters, format = "\"standard\"", include_redirect_urls = "false")] 315 #[args(
316 filters,
317 format = "\"standard\"",
318 include_redirect_urls = "false",
319 rule_types = "\"all\""
320 )]
298 pub fn add_filters( 321 pub fn add_filters(
299 &mut self, 322 &mut self,
300 filters: Vec<String>, 323 filters: Vec<String>,
301 format: &str, 324 format: &str,
302 include_redirect_urls: bool, 325 include_redirect_urls: bool,
326 rule_types: &str,
303 ) -> PyResult<()> { 327 ) -> PyResult<()> {
304 let filter_format = filter_format_from_string(format)?; 328 let filter_format = filter_format_from_string(format)?;
329 let rule_types = rule_types_from_string(rule_types)?;
305 self.filter_set.add_filters( 330 self.filter_set.add_filters(
306 &filters, 331 &filters,
307 ParseOptions { 332 ParseOptions {
308 format: filter_format, 333 format: filter_format,
309 include_redirect_urls, 334 include_redirect_urls,
335 rule_types,
310 }, 336 },
311 ); 337 );
312 Ok(()) 338 Ok(())
313 } 339 }
314}
315 340
316#[pyproto]
317impl PyObjectProtocol for FilterSet {
318 fn __repr__(&self) -> PyResult<String> { 341 fn __repr__(&self) -> PyResult<String> {
319 Ok(format!("FilterSet(debug={})", self.debug.diy_python_repr())) 342 Ok(format!("FilterSet(debug={})", self.debug.diy_python_repr()))
320 } 343 }
@@ -361,8 +384,8 @@ impl From<RustUrlSpecificResources> for UrlSpecificResources {
361 } 384 }
362} 385}
363 386
364#[pyproto] 387#[pymethods]
365impl PyObjectProtocol for UrlSpecificResources { 388impl UrlSpecificResources {
366 fn __repr__(&self) -> PyResult<String> { 389 fn __repr__(&self) -> PyResult<String> {
367 Ok(format!( 390 Ok(format!(
368 "UrlSpecificResources<{} hide selectors, {} style selectors, {} exceptions, injected_javascript={}, generichide={}>", 391 "UrlSpecificResources<{} hide selectors, {} style selectors, {} exceptions, injected_javascript={}, generichide={}>",
@@ -662,10 +685,7 @@ impl Engine {
662 .engine 685 .engine
663 .hidden_class_id_selectors(&classes, &ids, &exceptions)) 686 .hidden_class_id_selectors(&classes, &ids, &exceptions))
664 } 687 }
665}
666 688
667#[pyproto]
668impl PyObjectProtocol for Engine {
669 fn __repr__(&self) -> PyResult<String> { 689 fn __repr__(&self) -> PyResult<String> {
670 Ok(format!( 690 Ok(format!(
671 "Engine<optimize={}>", 691 "Engine<optimize={}>",