summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorÁrni Dagur <agudmundsson@fc-md.umd.edu>2020-03-29 23:23:03 -0400
committerÁrni Dagur <agudmundsson@fc-md.umd.edu>2020-03-29 23:23:03 -0400
commitd075eb00fff60603556a29714c348821a0bdda9a (patch)
tree180ee9868613cc2d3dbea05e51ea8a1e3950499b
parent129bfc10146973ce8a33fcd383c56c4f8071a492 (diff)
Add serialization and deserialization methods
-rw-r--r--Cargo.toml3
-rw-r--r--src/lib.rs78
2 files changed, 78 insertions, 3 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 334176d..c245655 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -7,8 +7,9 @@ edition = "2018"
7# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8 8
9[dependencies] 9[dependencies]
10adblock = "0.2.6"
10pyo3 = "0.9" 11pyo3 = "0.9"
11adblock = "0.2" 12failure = "0.1"
12 13
13[lib] 14[lib]
14name="adblock" 15name="adblock"
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 }