summaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs78
1 files changed, 76 insertions, 2 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 86512f7..5ba8d8a 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -10,11 +10,17 @@
10 unused_qualifications 10 unused_qualifications
11)] 11)]
12 12
13use adblock::blocker::BlockerError as RustBlockerError;
14use adblock::blocker::BlockerResult as RustBlockerResult;
15use adblock::engine::Engine as RustEngine;
16use failure::Fail;
13use pyo3::class::PyObjectProtocol; 17use pyo3::class::PyObjectProtocol;
18use pyo3::exceptions::ValueError as PyValueError;
14use pyo3::prelude::*; 19use pyo3::prelude::*;
20use pyo3::PyErr;
15 21
16use adblock::blocker::BlockerResult as RustBlockerResult; 22use std::fs;
17use adblock::engine::Engine as RustEngine; 23use std::io::{Read, Write};
18 24
19/// Brave's adblocking library in Python! 25/// Brave's adblocking library in Python!
20#[pymodule] 26#[pymodule]
@@ -72,6 +78,38 @@ impl PyObjectProtocol for BlockerResult {
72 } 78 }
73} 79}
74 80
81#[derive(Fail, Debug, PartialEq, Copy, Clone)]
82pub enum BlockerError {
83 #[fail(display = "Serialization error")]
84 SerializationError,
85 #[fail(display = "Deserialization error")]
86 DeserializationError,
87 #[fail(display = "Optimized filter exists")]
88 OptimizedFilterExistence,
89 #[fail(display = "Bad filter add unsupported")]
90 BadFilterAddUnsupported,
91 #[fail(display = "Filter exists")]
92 FilterExists,
93}
94
95impl Into<PyErr> for BlockerError {
96 fn into(self) -> PyErr {
97 PyErr::new::<PyValueError, _>(format!("{:?}", self))
98 }
99}
100
101impl Into<BlockerError> for RustBlockerError {
102 fn into(self) -> BlockerError {
103 match self {
104 Self::SerializationError => BlockerError::SerializationError,
105 Self::DeserializationError => BlockerError::DeserializationError,
106 Self::OptimizedFilterExistence => BlockerError::OptimizedFilterExistence,
107 Self::BadFilterAddUnsupported => BlockerError::BadFilterAddUnsupported,
108 Self::FilterExists => BlockerError::FilterExists,
109 }
110 }
111}
112
75#[pyclass] 113#[pyclass]
76pub struct Engine { 114pub struct Engine {
77 engine: RustEngine, 115 engine: RustEngine,
@@ -148,6 +186,42 @@ impl Engine {
148 blocker_result.into() 186 blocker_result.into()
149 } 187 }
150 188
189 pub fn serialize(&mut self) -> PyResult<Vec<u8>> {
190 let result = self.engine.serialize();
191 match result {
192 Ok(x) => Ok(x),
193 Err(error) => {
194 let my_blocker_error: BlockerError = error.into();
195 Err(my_blocker_error.into())
196 }
197 }
198 }
199
200 pub fn serialize_to_file(&mut self, file: &str) -> PyResult<()> {
201 let mut fd = fs::File::open(file)?;
202 let data = self.serialize()?;
203 fd.write_all(&data)?;
204 Ok(())
205 }
206
207 pub fn deserialize(&mut self, serialized: &[u8]) -> PyResult<()> {
208 let result = self.engine.deserialize(serialized);
209 match result {
210 Ok(x) => Ok(x),
211 Err(error) => {
212 let my_blocker_error: BlockerError = error.into();
213 Err(my_blocker_error.into())
214 }
215 }
216 }
217
218 pub fn deserialize_from_file(&mut self, file: &str) -> PyResult<()> {
219 let mut fd = fs::File::open(file)?;
220 let mut data: Vec<u8> = Vec::new();
221 fd.read_to_end(&mut data)?;
222 self.deserialize(&data)
223 }
224
151 pub fn filter_exists(&self, filter: &str) -> bool { 225 pub fn filter_exists(&self, filter: &str) -> bool {
152 self.engine.filter_exists(filter) 226 self.engine.filter_exists(filter)
153 } 227 }