mirror of
https://github.com/Drop-OSS/dropbreak.git
synced 2026-07-18 09:57:53 -04:00
Add bincode
This commit is contained in:
+14
-2
@@ -14,14 +14,26 @@ version = "2.0.0"
|
||||
fs2 = "0.4"
|
||||
quick-error = "1.1.0"
|
||||
ron = "0.1.2"
|
||||
serde_yaml = { version = "0.7", optional = true }
|
||||
serde = "1"
|
||||
|
||||
[dependencies.bincode]
|
||||
optional = true
|
||||
version = "0.8.1"
|
||||
|
||||
[dependencies.base64]
|
||||
optional = true
|
||||
version = "0.7.0"
|
||||
|
||||
[dependencies.serde_yaml]
|
||||
optional = true
|
||||
version = "0.7"
|
||||
|
||||
[dev-dependencies]
|
||||
lazy_static = "0.2.1"
|
||||
tempfile = "2.1"
|
||||
serde_derive = "1"
|
||||
tempfile = "2.1"
|
||||
|
||||
[features]
|
||||
bin = ["bincode", "base64"]
|
||||
yaml = ["serde_yaml"]
|
||||
ron_enc = ["ron"]
|
||||
|
||||
+55
-13
@@ -11,7 +11,10 @@ use ron::ser::pretty::to_string as to_ron_string;
|
||||
use ron::de::from_reader as from_ron_string;
|
||||
|
||||
#[cfg(feature = "yaml")]
|
||||
use serde_yaml::{to_string as to_yaml_string, from_reader as from_yaml_string};
|
||||
pub use yaml::Yaml;
|
||||
|
||||
#[cfg(feature = "bin")]
|
||||
pub use bincode::Bincode;
|
||||
|
||||
/// A trait to bundle serializer and deserializer
|
||||
pub trait DeSerializer<T: Serialize + DeserializeOwned> {
|
||||
@@ -42,18 +45,57 @@ impl<T: Serialize + DeserializeOwned> DeSerializer<T> for Ron {
|
||||
}
|
||||
|
||||
#[cfg(feature = "yaml")]
|
||||
/// The struct that allows you to use yaml
|
||||
#[derive(Debug)]
|
||||
pub struct Yaml;
|
||||
mod yaml {
|
||||
use std::io::Read;
|
||||
|
||||
#[cfg(feature = "yaml")]
|
||||
impl<T: Serialize + DeserializeOwned> DeSerializer<T> for Yaml {
|
||||
type SerError = ::serde_yaml::Error;
|
||||
type DeError = ::serde_yaml::Error;
|
||||
fn serialize(&self, val: &T) -> Result<String, Self::SerError> {
|
||||
to_yaml_string(val)
|
||||
}
|
||||
fn deserialize<R: Read>(&self, s: R) -> Result<T, Self::DeError> {
|
||||
from_yaml_string(s)
|
||||
use serde_yaml::{to_string as to_yaml_string, from_reader as from_yaml_string};
|
||||
use serde::Serialize;
|
||||
use serde::de::DeserializeOwned;
|
||||
|
||||
use deser::DeSerializer;
|
||||
|
||||
/// The struct that allows you to use yaml
|
||||
#[derive(Debug)]
|
||||
pub struct Yaml;
|
||||
|
||||
impl<T: Serialize + DeserializeOwned> DeSerializer<T> for Yaml {
|
||||
type SerError = ::serde_yaml::Error;
|
||||
type DeError = ::serde_yaml::Error;
|
||||
fn serialize(&self, val: &T) -> Result<String, Self::SerError> {
|
||||
to_yaml_string(val)
|
||||
}
|
||||
fn deserialize<R: Read>(&self, s: R) -> Result<T, Self::DeError> {
|
||||
from_yaml_string(s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "bin")]
|
||||
mod bincode {
|
||||
use std::io::Read;
|
||||
|
||||
use bincode::{serialize as to_bincode_string, deserialize as from_bincode_string};
|
||||
use base64::{encode, decode};
|
||||
use serde::Serialize;
|
||||
use serde::de::DeserializeOwned;
|
||||
|
||||
use deser::DeSerializer;
|
||||
|
||||
/// The struct that allows you to use bincode
|
||||
#[derive(Debug)]
|
||||
pub struct Bincode;
|
||||
|
||||
impl<T: Serialize + DeserializeOwned> DeSerializer<T> for Bincode {
|
||||
type SerError = ::bincode::Error;
|
||||
type DeError = ::bincode::Error;
|
||||
fn serialize(&self, val: &T) -> Result<String, Self::SerError> {
|
||||
let res = to_bincode_string(val, ::bincode::Infinite)?;
|
||||
Ok(encode(&res))
|
||||
}
|
||||
fn deserialize<R: Read>(&self, s: R) -> Result<T, Self::DeError> {
|
||||
let mut string = String::new();
|
||||
s.read_to_string(&mut string)?;
|
||||
Ok(from_bincode_string(String::from_utf8(decode(&string)?)?.as_bytes())?)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,6 +32,10 @@ extern crate serde;
|
||||
extern crate ron;
|
||||
#[cfg(feature = "yaml")]
|
||||
extern crate serde_yaml;
|
||||
#[cfg(feature = "bin")]
|
||||
extern crate bincode;
|
||||
#[cfg(feature = "bin")]
|
||||
extern crate base64;
|
||||
#[macro_use] extern crate quick_error;
|
||||
|
||||
mod error;
|
||||
|
||||
Reference in New Issue
Block a user