summaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: e8797d2987aea8b849655a7dc754ee835de46d14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
//! Python wrapper for Brave's adblocking library, which is written in Rust.
#![deny(
    future_incompatible,
    nonstandard_style,
    rust_2018_idioms,
    missing_copy_implementations,
    trivial_casts,
    trivial_numeric_casts,
    unsafe_code,
    unused_qualifications,
    deprecated
)]

use adblock::blocker::BlockerResult as RustBlockerResult;
use adblock::blocker::{BlockerError as RustBlockerError, Redirection};
use adblock::cosmetic_filter_cache::UrlSpecificResources as RustUrlSpecificResources;
use adblock::engine::Engine as RustEngine;
use adblock::lists::FilterSet as RustFilterSet;
use adblock::lists::{FilterFormat, ParseOptions, RuleTypes};
use pyo3::create_exception;
use pyo3::exceptions::PyException;
use pyo3::prelude::*;
use pyo3::types::PyBytes;
use pyo3::PyErr;

use adblock::resources::{
    AddResourceError as RustAddResourceError, MimeType, Resource, ResourceType,
};
use std::collections::HashMap;
use std::collections::HashSet;
use std::error::Error;
use std::fmt::{self, Display};
use std::fs;
use std::io::{Read, Write};

/// Brave's adblocking library in Python!
#[pymodule]
fn adblock(py: Python<'_>, m: &PyModule) -> PyResult<()> {
    m.add("__version__", env!("CARGO_PKG_VERSION"))?;
    m.add_class::<Engine>()?;
    m.add_class::<FilterSet>()?;
    m.add_class::<BlockerResult>()?;
    m.add_class::<UrlSpecificResources>()?;
    m.add("AdblockException", py.get_type::<AdblockException>())?;
    m.add("BlockerException", py.get_type::<BlockerException>())?;
    m.add("SerializationError", py.get_type::<SerializationError>())?;
    m.add(
        "DeserializationError",
        py.get_type::<DeserializationError>(),
    )?;
    m.add(
        "OptimizedFilterExistence",
        py.get_type::<OptimizedFilterExistence>(),
    )?;
    m.add(
        "BadFilterAddUnsupported",
        py.get_type::<BadFilterAddUnsupported>(),
    )?;
    m.add("FilterExists", py.get_type::<FilterExists>())?;
    m.add(
        "AddResourceException",
        py.get_type::<AddResourceException>(),
    )?;
    m.add(
        "InvalidBase64ContentError",
        py.get_type::<InvalidBase64ContentError>(),
    )?;
    m.add(
        "InvalidUtf8ContentError",
        py.get_type::<InvalidUtf8ContentError>(),
    )?;
    Ok(())
}

/// The result of an ad-blocking check.
#[pyclass]
pub struct BlockerResult {
    #[pyo3(get)]
    pub matched: bool,
    /// Important is used to signal that a rule with the `important` option
    /// matched. An `important` match means that exceptions should not apply
    /// and no further checking is neccesary--the request should be blocked
    /// (empty body or cancelled).
    ///
    /// Brave Browser keeps seperate instances of Blocker for default lists
    /// and regional ones, so `important` here is used to correct behaviour
    /// between them: checking should stop instead of moving to the next
    /// instance iff an `important` rule matched.
    #[pyo3(get)]
    pub important: bool,
    /// Iff the blocker matches a rule which has the `redirect` option, as per
    /// [uBlock Origin's redirect syntax][1], the `redirect` is not `None`.
    /// The `redirect` field contains the body of the redirect to be injected.
    ///
    /// [1]: https://github.com/gorhill/uBlock/wiki/Static-filter-syntax#redirect
    #[pyo3(get)]
    pub redirect_type: Option<String>,
    /// Exception is not `None` when the blocker matched on an exception rule.
    /// Effectively this means that there was a match, but the request should
    /// not be blocked. It is a non-empty string if the blocker was initialized
    /// from a list of rules with debugging enabled, otherwise the original
    /// string representation is discarded to reduce memory use.
    #[pyo3(get)]
    pub redirect: Option<String>,
    /// Exception is not `None` when the blocker matched on an exception rule.
    /// Effectively this means that there was a match, but the request should
    /// not be blocked. It is a non-empty string if the blocker was initialized
    /// from a list of rules with debugging enabled, otherwise the original
    /// string representation is discarded to reduce memory use.
    #[pyo3(get)]
    pub exception: Option<String>,
    /// Filter--similarly to exception--includes the string representation of
    /// the rule when there is a match and debugging is enabled. Otherwise, on
    /// a match, it is not `None`.
    #[pyo3(get)]
    pub filter: Option<String>,
    /// The `error` field is only used to signal that there was an error in
    /// parsing the provided URLs when using the simpler
    /// `check_network_urls` method.
    #[pyo3(get)]
    pub error: Option<String>,
}

impl From<RustBlockerResult> for BlockerResult {
    fn from(br: RustBlockerResult) -> Self {
        let (redirect, redirect_type) = if let Some(resource) = br.redirect {
            match resource {
                Redirection::Resource(resource) => (Some(resource), Some("resource".to_string())),
                Redirection::Url(url) => (Some(url), Some("url".to_string())),
            }
        } else {
            (None, None)
        };

        Self {
            matched: br.matched,
            important: br.important,
            exception: br.exception,
            filter: br.filter,
            error: br.error,
            redirect_type,
            redirect,
        }
    }
}

#[pymethods]
impl BlockerResult {
    fn __repr__(&self) -> PyResult<String> {
        Ok(format!(
            "BlockerResult(matched={}, important={}, redirect={}, exception={}, filter={}, error={})",
            self.matched.diy_python_repr(),
            self.important.diy_python_repr(),
            self.redirect.diy_python_repr(),
            self.exception.diy_python_repr(),
            self.filter.diy_python_repr(),
            self.error.diy_python_repr(),
        ))
    }
}

#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum BlockerError {
    SerializationError,
    DeserializationError,
    OptimizedFilterExistence,
    BadFilterAddUnsupported,
    FilterExists,
}

impl Error for BlockerError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        None
    }
}

impl Display for BlockerError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Self::SerializationError => "Serialization error",
                Self::DeserializationError => "Deserialization error",
                Self::OptimizedFilterExistence => "Optimized filter exists",
                Self::BadFilterAddUnsupported => "Bad filter add unsupported",
                Self::FilterExists => "Filter exists",
            }
        )
    }
}

create_exception!(adblock, AdblockException, PyException);
create_exception!(adblock, BlockerException, AdblockException);
create_exception!(adblock, AddResourceException, AdblockException);
create_exception!(adblock, InvalidBase64ContentError, AddResourceException);
create_exception!(adblock, InvalidUtf8ContentError, AddResourceException);
create_exception!(adblock, SerializationError, BlockerException);
create_exception!(adblock, DeserializationError, BlockerException);
create_exception!(adblock, OptimizedFilterExistence, BlockerException);
create_exception!(adblock, BadFilterAddUnsupported, BlockerException);
create_exception!(adblock, FilterExists, BlockerException);

impl From<BlockerError> for PyErr {
    fn from(err: BlockerError) -> Self {
        let msg = format!("{:?}", err);
        match err {
            BlockerError::SerializationError => Self::new::<SerializationError, _>(msg),
            BlockerError::DeserializationError => Self::new::<DeserializationError, _>(msg),
            BlockerError::OptimizedFilterExistence => Self::new::<OptimizedFilterExistence, _>(msg),
            BlockerError::BadFilterAddUnsupported => Self::new::<BadFilterAddUnsupported, _>(msg),
            BlockerError::FilterExists => Self::new::<FilterExists, _>(msg),
        }
    }
}

impl From<RustBlockerError> for BlockerError {
    fn from(err: RustBlockerError) -> Self {
        match err {
            RustBlockerError::SerializationError => Self::SerializationError,
            RustBlockerError::DeserializationError => Self::DeserializationError,
            RustBlockerError::OptimizedFilterExistence => Self::OptimizedFilterExistence,
            RustBlockerError::BadFilterAddUnsupported => Self::BadFilterAddUnsupported,
            RustBlockerError::FilterExists => Self::FilterExists,
        }
    }
}

fn filter_format_from_string(filter_format: &str) -> PyResult<FilterFormat> {
    match filter_format {
        "standard" => Ok(FilterFormat::Standard),
        "hosts" => Ok(FilterFormat::Hosts),
        _ => Err(PyErr::new::<AdblockException, _>(
            "Invalid FilterFormat value",
        )),
    }
}

fn rule_types_from_string(rule_types: &str) -> PyResult<RuleTypes> {
    match rule_types {
        "all" => Ok(RuleTypes::All),
        "networkonly" => Ok(RuleTypes::NetworkOnly),
        "cosmeticonly" => Ok(RuleTypes::CosmeticOnly),
        _ => Err(PyErr::new::<AdblockException, _>("Invalid RuleTypes value")),
    }
}

/// Manages a set of rules to be added to an Engine.
///
/// To be able to efficiently handle special options like $badfilter, and to
/// allow optimizations, all rules must be available when the Engine is first
/// created. FilterSet allows assembling a compound list from multiple
/// different sources before compiling the rules into an Engine.
#[pyclass]
#[pyo3(text_signature = "($self, debug)")]
#[derive(Clone)]
pub struct FilterSet {
    filter_set: RustFilterSet,
    debug: bool,
}

#[pymethods]
impl FilterSet {
    /// Creates a new `FilterSet`. The `debug` argument specifies whether or
    /// not to save information about the original raw filter rules alongside
    /// the more compact internal representation. If enabled, this information
    /// will be passed to the corresponding Engine.
    #[new]
    #[args(debug = false)]
    pub fn new(debug: bool) -> Self {
        Self {
            filter_set: RustFilterSet::new(debug),
            debug,
        }
    }

    /// Adds the contents of an entire filter list to this FilterSet. Filters
    /// that cannot be parsed successfully are ignored.
    ///
    /// The format is a string containing either "standard" (ABP/uBO-style)
    /// or "hosts".
    #[pyo3(text_signature = "($self, filter_list, format, include_redirect_urls, rule_types)")]
    #[args(
        filter_list,
        format = "\"standard\"",
        include_redirect_urls = "false",
        rule_types = "\"all\""
    )]
    pub fn add_filter_list(
        &mut self,
        filter_list: &str,
        format: &str,
        include_redirect_urls: bool,
        rule_types: &str,
    ) -> PyResult<()> {
        let filter_format = filter_format_from_string(format)?;
        let rule_types = rule_types_from_string(rule_types)?;
        self.filter_set.add_filter_list(
            filter_list,
            ParseOptions {
                format: filter_format,
                include_redirect_urls,
                rule_types,
            },
        );
        Ok(())
    }

    /// Adds a collection of filter rules to this FilterSet. Filters that
    /// cannot be parsed successfully are ignored.
    ///
    /// The format is a string containing either "standard" (ABP/uBO-style)
    /// or "hosts".
    #[pyo3(text_signature = "($self, filters, format, include_redirect_urls, rule_types)")]
    #[args(
        filters,
        format = "\"standard\"",
        include_redirect_urls = "false",
        rule_types = "\"all\""
    )]
    pub fn add_filters(
        &mut self,
        filters: Vec<String>,
        format: &str,
        include_redirect_urls: bool,
        rule_types: &str,
    ) -> PyResult<()> {
        let filter_format = filter_format_from_string(format)?;
        let rule_types = rule_types_from_string(rule_types)?;
        self.filter_set.add_filters(
            &filters,
            ParseOptions {
                format: filter_format,
                include_redirect_urls,
                rule_types,
            },
        );
        Ok(())
    }

    fn __repr__(&self) -> PyResult<String> {
        Ok(format!("FilterSet(debug={})", self.debug.diy_python_repr()))
    }
}

/// Contains cosmetic filter information intended to be injected into a
/// particular hostname.
#[pyclass]
pub struct UrlSpecificResources {
    /// A set of any CSS selector on the page that should be hidden, i.e.
    /// styled as `{ display: none !important; }`.
    #[pyo3(get)]
    pub hide_selectors: HashSet<String>,
    /// A map of CSS selectors on the page to respective non-hide style rules,
    /// i.e. any required styles other than `display: none`.
    #[pyo3(get)]
    pub style_selectors: HashMap<String, Vec<String>>,
    /// A set of any class or id CSS selectors that should not have generic
    /// rules applied.
    // In practice, these should be passed to `class_id_stylesheet` and not
    // used otherwise.
    #[pyo3(get)]
    pub exceptions: HashSet<String>,
    /// Javascript code for any scriptlets that should be injected into the
    /// page.
    #[pyo3(get)]
    pub injected_script: String,
    /// `generichide` is set to `True` if there is a corresponding
    /// `$generichide` exception network filter. If so, the page should not
    /// query for additional generic rules using hidden_class_id_selectors.
    #[pyo3(get)]
    pub generichide: bool,
}

impl From<RustUrlSpecificResources> for UrlSpecificResources {
    fn from(r: RustUrlSpecificResources) -> Self {
        Self {
            hide_selectors: r.hide_selectors,
            style_selectors: r.style_selectors,
            exceptions: r.exceptions,
            injected_script: r.injected_script,
            generichide: r.generichide,
        }
    }
}

#[pymethods]
impl UrlSpecificResources {
    fn __repr__(&self) -> PyResult<String> {
        Ok(format!(
            "UrlSpecificResources<{} hide selectors, {} style selectors, {} exceptions, injected_javascript={}, generichide={}>",
            self.hide_selectors.len(),
            self.style_selectors.len(),
            self.exceptions.len(),
            self.injected_script.diy_python_repr(),
            self.generichide.diy_python_repr(),
        ))
    }
}

/// The main object featured in this library. This object holds the adblocker's
/// state, and can be queried to see if a given request should be blocked or
/// not.
///
/// # Request types
/// A few of `Engine`'s methods have a field specifying a "resource type",
/// valid examples are:
/// * `beacon`
/// * `csp_report`
/// * `document`
/// * `font`
/// * `media`
/// * `object`
/// * `script`
/// * `stylesheet`
/// * and et cetera...
/// See the [Mozilla Web Documentation][1] for more info.
///
/// [1]: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webRequest/ResourceType
#[pyclass]
#[pyo3(text_signature = "($self, filter_set, optimize)")]
pub struct Engine {
    engine: RustEngine,
    optimize: bool,
}

#[pymethods]
impl Engine {
    /// Create a new adblocking engine
    #[new]
    #[args(filter_set, optimize = true)]
    pub fn new(filter_set: FilterSet, optimize: bool) -> Self {
        let engine = RustEngine::from_filter_set(filter_set.filter_set, optimize);
        Self { engine, optimize }
    }

    /// Check if the given `url`—pointing to a resource of type `request_type`—
    /// is blocked, assuming the request is made from the given `source_url`.
    /// Returns an object of type `BlockerResult`.
    ///
    /// # Arguments
    /// * `url` - The URL of the request to check
    /// * `source_url` - The URL from where the request is made
    /// * `request_type` - The resource type that the request points to
    #[pyo3(text_signature = "($self, url, source_url, request_type)")]
    pub fn check_network_urls(
        &self,
        url: &str,
        source_url: &str,
        request_type: &str,
    ) -> BlockerResult {
        let blocker_result = self
            .engine
            .check_network_urls(url, source_url, request_type);
        blocker_result.into()
    }

    /// Check if a request should be blocked based on the given parameters.
    ///
    /// # Arguments
    /// * `url` - The URL of the request to check
    /// * `hostname` - The given `url`'s hostname
    /// * `source_hostname` - The hostname of the source URL.
    /// * `request_type` - The resource type that the request points to
    /// * `third_party_request` - Is the given request to a third-party? Here,
    ///   `None` can be given and the engine will figure it out based on the
    ///   `hostname` and `source_hostname`.
    #[pyo3(
        text_signature = "($self, url, hostname, source_hostname, requsest_type, third_party_request)"
    )]
    pub fn check_network_urls_with_hostnames(
        &self,
        url: &str,
        hostname: &str,
        source_hostname: &str,
        request_type: &str,
        third_party_request: Option<bool>,
    ) -> BlockerResult {
        let blocker_result = self.engine.check_network_urls_with_hostnames(
            url,
            hostname,
            source_hostname,
            request_type,
            third_party_request,
        );
        blocker_result.into()
    }

    /// Check if a request should be blocked based on the given parameters.
    ///
    /// # Arguments
    /// * `url` - The URL of the request to check
    /// * `hostname` - The given `url`'s hostname
    /// * `source_hostname` - The hostname of the source URL.
    /// * `request_type` - The resource type that the request points to
    /// * `third_party_request` - Is the given request to a third-party? Here,
    ///   `None` can be given and the engine will figure it out based on the
    ///   `hostname` and `source_hostname`.
    /// * `previously_matched_rule` - Return a match as long as there are no
    ///    exceptions
    /// * `force_check_exceptions` - Check exceptions even if no other rule matches
    #[pyo3(
        text_signature = "($self, url, hostname, source_hostname, request_type, \
        third_party_request, previously_matched_rule, force_check_exceptions)"
    )]
    #[allow(clippy::too_many_arguments)]
    pub fn check_network_urls_with_hostnames_subset(
        &self,
        url: &str,
        hostname: &str,
        source_hostname: &str,
        request_type: &str,
        third_party_request: Option<bool>,
        previously_matched_rule: bool,
        force_check_exceptions: bool,
    ) -> BlockerResult {
        let blocker_result = self.engine.check_network_urls_with_hostnames_subset(
            url,
            hostname,
            source_hostname,
            request_type,
            third_party_request,
            previously_matched_rule,
            force_check_exceptions,
        );
        blocker_result.into()
    }

    /// Sets this engine's resources to additionally include `resource`.
    ///
    /// # Arguments
    /// * `name`: Represents the primary name of the resource, often a filename
    /// * `content_type`: How to interpret the resource data within `content`.
    ///   Use `"template"` if wanting to specify a template resource type.
    /// * `content`: The resource data, encoded using standard base64 configuration
    /// * `aliases`: List of aliases for the resource
    #[pyo3(text_signature = "($self, name, content_type, content, aliases)")]
    pub fn add_resource(
        &mut self,
        name: &str,
        content_type: &str,
        content: &str,
        aliases: Option<Vec<String>>,
    ) -> PyResult<()> {
        let result = self.engine.add_resource(Resource {
            name: name.to_string(),
            aliases: aliases.unwrap_or_default(),
            kind: match content_type {
                "template" => ResourceType::Template,
                _ => ResourceType::Mime(MimeType::from(std::borrow::Cow::from(
                    content_type.to_string(),
                ))),
            },
            content: content.to_string(),
        });

        match result {
            Ok(_) => Ok(()),
            Err(err) => match err {
                RustAddResourceError::InvalidBase64Content => Err(
                    InvalidBase64ContentError::new_err("invalid base64 content".to_string()),
                ),
                RustAddResourceError::InvalidUtf8Content => Err(InvalidUtf8ContentError::new_err(
                    "invalid utf content".to_string(),
                )),
            },
        }
    }

    /// Serialize this blocking engine to bytes. They can then be deserialized
    /// using `deserialize()` to get the same engine again.
    #[pyo3(text_signature = "($self)")]
    pub fn serialize<'p>(&mut self, py: Python<'p>) -> PyResult<&'p PyBytes> {
        let bytes = self.serialize_inner()?;
        let py_bytes = PyBytes::new(py, &bytes);
        Ok(py_bytes)
    }

    fn serialize_inner(&mut self) -> PyResult<Vec<u8>> {
        let result = self.engine.serialize_raw();
        match result {
            Ok(x) => Ok(x),
            Err(error) => {
                let my_blocker_error: BlockerError = error.into();
                Err(my_blocker_error.into())
            }
        }
    }

    /// Serialize this blocking engine to a file. The file can then be
    /// deserialized using `deserialize_from_file()` to get the same engine
    /// again.
    #[pyo3(text_signature = "($self, file)")]
    pub fn serialize_to_file(&mut self, file: &str) -> PyResult<()> {
        let data = self.serialize_inner()?;
        let mut fd = fs::OpenOptions::new()
            .create(true)
            .truncate(true)
            .write(true)
            .open(file)?;
        fd.write_all(&data)?;
        Ok(())
    }

    /// Deserialize a blocking engine from bytes produced with `serialize()`.
    #[pyo3(text_signature = "($self, serialized)")]
    pub fn deserialize(&mut self, serialized: &[u8]) -> PyResult<()> {
        let result = self.engine.deserialize(serialized);
        match result {
            Ok(_) => Ok(()),
            Err(error) => {
                let my_blocker_error: BlockerError = error.into();
                Err(my_blocker_error.into())
            }
        }
    }

    /// Deserialize a blocking engine from file produced with
    /// `serialize_to_file()`.
    #[pyo3(text_signature = "($self, file)")]
    pub fn deserialize_from_file(&mut self, file: &str) -> PyResult<()> {
        let mut fd = fs::File::open(file)?;
        let mut data: Vec<u8> = Vec::new();
        fd.read_to_end(&mut data)?;
        self.deserialize(&data)
    }

    /// Checks if the given filter exists in the blocking engine.
    #[pyo3(text_signature = "($self, filter)")]
    pub fn filter_exists(&self, filter: &str) -> bool {
        self.engine.filter_exists(filter)
    }

    /// Sets this engine's tags to be _only_ the ones provided in tags.
    ///
    /// Tags can be used to cheaply enable or disable network rules with a
    /// corresponding $tag option.
    #[pyo3(text_signature = "($self, tags)")]
    pub fn use_tags(&mut self, tags: Vec<&str>) {
        self.engine.use_tags(&tags);
    }

    /// Sets this engine's tags to additionally include the ones provided in
    /// tags.
    ///
    /// Tags can be used to cheaply enable or disable network rules with a
    /// corresponding $tag option.
    #[pyo3(text_signature = "($self, tags)")]
    pub fn enable_tags(&mut self, tags: Vec<&str>) {
        self.engine.enable_tags(&tags);
    }

    /// Sets this engine's tags to no longer include the ones provided in
    /// tags.
    ///
    /// Tags can be used to cheaply enable or disable network rules with a
    /// corresponding $tag option.
    #[pyo3(text_signature = "($self, tags)")]
    pub fn disable_tags(&mut self, tags: Vec<&str>) {
        self.engine.disable_tags(&tags);
    }

    /// Checks if a given tag exists in this engine.
    ///
    /// Tags can be used to cheaply enable or disable network rules with a
    /// corresponding $tag option.
    #[pyo3(text_signature = "($self, tag)")]
    pub fn tag_exists(&self, tag: &str) -> bool {
        self.engine.tag_exists(tag)
    }

    /// Returns a set of cosmetic filter resources required for a particular
    /// url. Once this has been called, all CSS ids and classes on a
    /// page should be passed to hidden_class_id_selectors to obtain any
    /// stylesheets consisting of generic rules.
    #[pyo3(text_signature = "($self, url)")]
    pub fn url_cosmetic_resources(&self, url: &str) -> UrlSpecificResources {
        self.engine.url_cosmetic_resources(url).into()
    }

    /// If any of the provided CSS classes or ids could cause a certain generic
    /// CSS hide rule (i.e. `{ display: none !important; }`) to be required, this
    /// method will return a list of CSS selectors corresponding to rules
    /// referencing those classes or ids, provided that the corresponding rules
    /// are not excepted.
    ///
    /// Exceptions should be passed directly from UrlSpecificResources.
    #[pyo3(text_signature = "($self, classes, ids, exceptions)")]
    pub fn hidden_class_id_selectors(
        &self,
        classes: Vec<String>,
        ids: Vec<String>,
        exceptions: HashSet<String>,
    ) -> PyResult<Vec<String>> {
        Ok(self
            .engine
            .hidden_class_id_selectors(&classes, &ids, &exceptions))
    }

    fn __repr__(&self) -> PyResult<String> {
        Ok(format!(
            "Engine<optimize={}>",
            self.optimize.diy_python_repr()
        ))
    }
}

/// PyO3 doesn't offer the ability to get the Python representation of a Rust
/// object, so we make our own trait.
trait DiyPythonRepr {
    fn diy_python_repr(&self) -> String;
}

impl<T> DiyPythonRepr for Option<T>
where
    T: DiyPythonRepr,
{
    fn diy_python_repr(&self) -> String {
        match self {
            None => "None".to_owned(),
            Some(x) => x.diy_python_repr(),
        }
    }
}

impl DiyPythonRepr for String {
    fn diy_python_repr(&self) -> String {
        let mut res = format!("{:?}", self);
        // This is safe to do since we know that `res` will always be of
        // length >= 2.
        res.replace_range(0..1, "'");
        res.replace_range(res.len() - 1..res.len(), "'");
        res
    }
}

impl DiyPythonRepr for bool {
    fn diy_python_repr(&self) -> String {
        if *self {
            "True".to_owned()
        } else {
            "False".to_owned()
        }
    }
}