diff options
| 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 |
| commit | d075eb00fff60603556a29714c348821a0bdda9a (patch) | |
| tree | 180ee9868613cc2d3dbea05e51ea8a1e3950499b /src | |
| parent | 129bfc10146973ce8a33fcd383c56c4f8071a492 (diff) | |
Add serialization and deserialization methods
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib.rs | 78 |
1 files changed, 76 insertions, 2 deletions
| @@ -10,11 +10,17 @@ | |||
| 10 | unused_qualifications | 10 | unused_qualifications |
| 11 | )] | 11 | )] |
| 12 | 12 | ||
| 13 | use adblock::blocker::BlockerError as RustBlockerError; | ||
| 14 | use adblock::blocker::BlockerResult as RustBlockerResult; | ||
| 15 | use adblock::engine::Engine as RustEngine; | ||
| 16 | use failure::Fail; | ||
| 13 | use pyo3::class::PyObjectProtocol; | 17 | use pyo3::class::PyObjectProtocol; |
| 18 | use pyo3::exceptions::ValueError as PyValueError; | ||
| 14 | use pyo3::prelude::*; | 19 | use pyo3::prelude::*; |
| 20 | use pyo3::PyErr; | ||
| 15 | 21 | ||
| 16 | use adblock::blocker::BlockerResult as RustBlockerResult; | 22 | use std::fs; |
| 17 | use adblock::engine::Engine as RustEngine; | 23 | use 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)] | ||
| 82 | pub 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 | |||
| 95 | impl Into<PyErr> for BlockerError { | ||
| 96 | fn into(self) -> PyErr { | ||
| 97 | PyErr::new::<PyValueError, _>(format!("{:?}", self)) | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | impl 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] |
| 76 | pub struct Engine { | 114 | pub 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 | } |
