content_blocker/lib.rs
1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5//! A library for parsing [Safari-style content blocking lists](https://developer.apple.com/library/ios/documentation/Extensions/Conceptual/ContentBlockingRules/CreatingRules/CreatingRules.html)
6//! and evaluating them against network requests.
7
8#![deny(missing_docs)]
9
10extern crate regex;
11extern crate serde_json;
12extern crate url;
13
14mod parse;
15mod repr;
16
17pub use parse::Error;
18use parse::parse_list_impl;
19pub use repr::{ResourceType, LoadType, Request, Reaction};
20use repr::{Rule, process_rules_for_request_impl};
21
22#[cfg(test)]
23mod tests;
24
25/// An encapsulation of a list of parsed rules.
26pub struct RuleList(Vec<Rule>);
27
28/// Attempt to match the given request against the provided rules. Returns a list
29/// of actions to take in response; an empty list means that the request should
30/// continue unmodified.
31pub fn process_rules_for_request(rules: &RuleList, request: &Request) -> Vec<Reaction> {
32 process_rules_for_request_impl(&rules.0, request)
33}
34
35/// Parse a string containing a JSON representation of a content blocker list.
36/// Returns a vector of parsed rules, or an error representing the nature of
37/// the invalid input. Any rules missing required fields will be silently ignored.
38pub fn parse_list(body: &str) -> Result<RuleList, Error> {
39 parse_list_impl(body).map(|r| RuleList(r))
40}