-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
110 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use crate::engine::Engine; | ||
|
||
#[derive(Debug)] | ||
pub enum SerializeError { | ||
#[cfg(not(feature = "flatbuffers"))] | ||
DataFormatError(crate::data_format::SerializationError), | ||
|
||
#[cfg(feature = "flatbuffers")] | ||
FlatbuffersError(), | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum DeserializeError { | ||
#[cfg(not(feature = "flatbuffers"))] | ||
DataFormatError(crate::data_format::DeserializationError), | ||
|
||
#[cfg(feature = "flatbuffers")] | ||
FlatbuffersError(), | ||
} | ||
|
||
#[cfg(not(feature = "flatbuffers"))] | ||
impl From<crate::data_format::SerializationError> for SerializeError { | ||
fn from(value: crate::data_format::SerializationError) -> Self { | ||
SerializeError::DataFormatError(value) | ||
} | ||
} | ||
|
||
#[cfg(not(feature = "flatbuffers"))] | ||
impl From<crate::data_format::DeserializationError> for DeserializeError { | ||
fn from(value: crate::data_format::DeserializationError) -> Self { | ||
DeserializeError::DataFormatError(value) | ||
} | ||
} | ||
|
||
pub trait Serialize { | ||
fn serialize_raw(&self) -> Result<Vec<u8>, SerializeError>; | ||
fn deserialize(&mut self, serialized: &[u8]) -> Result<(), DeserializeError>; | ||
} | ||
|
||
#[cfg(feature = "flatbuffers")] | ||
impl Serialize for Engine { | ||
fn serialize_raw(&self) -> Result<Vec<u8>, SerializeError> { | ||
Err(SerializeError::FlatbuffersError()) | ||
} | ||
|
||
fn deserialize(&mut self, _serialized: &[u8]) -> Result<(), DeserializeError> { | ||
Err(DeserializeError::FlatbuffersError()) | ||
} | ||
} | ||
|
||
#[cfg(not(feature = "flatbuffers"))] | ||
impl Serialize for Engine { | ||
/// Serializes the `Engine` into a binary format so that it can be quickly reloaded later. | ||
fn serialize_raw(&self) -> Result<Vec<u8>, SerializeError> { | ||
use crate::data_format::SerializeFormat; | ||
|
||
let serialize_format = SerializeFormat::build(&self.blocker, &self.cosmetic_cache); | ||
|
||
let result = serialize_format.serialize()?; | ||
Ok(result) | ||
} | ||
|
||
/// Deserialize the `Engine` from the binary format generated by `Engine::serialize_raw`. The | ||
/// method will automatically select the correct deserialization implementation. | ||
fn deserialize(&mut self, serialized: &[u8]) -> Result<(), DeserializeError> { | ||
use crate::data_format::DeserializeFormat; | ||
let current_tags = self.blocker.tags_enabled(); | ||
let deserialize_format = DeserializeFormat::deserialize(serialized)?; | ||
let (blocker, cosmetic_cache) = deserialize_format.build(); | ||
self.blocker = blocker; | ||
self.blocker | ||
.use_tags(¤t_tags.iter().map(|s| &**s).collect::<Vec<_>>()); | ||
self.cosmetic_cache = cosmetic_cache; | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
use adblock::request::Request; | ||
use adblock::Engine; | ||
use adblock::{Engine, Serialize}; | ||
|
||
use serde::Deserialize; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
use adblock::request::Request; | ||
use adblock::Engine; | ||
use adblock::{Engine, Serialize}; | ||
|
||
use serde::Deserialize; | ||
|
||
|