diff --git a/Cargo.toml b/Cargo.toml index 80a6a0f..ee9d2d1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] diff --git a/src/deser.rs b/src/deser.rs index 3e3cb0c..1d89afd 100644 --- a/src/deser.rs +++ b/src/deser.rs @@ -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 { @@ -42,18 +45,57 @@ impl DeSerializer 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 DeSerializer for Yaml { - type SerError = ::serde_yaml::Error; - type DeError = ::serde_yaml::Error; - fn serialize(&self, val: &T) -> Result { - to_yaml_string(val) - } - fn deserialize(&self, s: R) -> Result { - 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 DeSerializer for Yaml { + type SerError = ::serde_yaml::Error; + type DeError = ::serde_yaml::Error; + fn serialize(&self, val: &T) -> Result { + to_yaml_string(val) + } + fn deserialize(&self, s: R) -> Result { + 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 DeSerializer for Bincode { + type SerError = ::bincode::Error; + type DeError = ::bincode::Error; + fn serialize(&self, val: &T) -> Result { + let res = to_bincode_string(val, ::bincode::Infinite)?; + Ok(encode(&res)) + } + fn deserialize(&self, s: R) -> Result { + let mut string = String::new(); + s.read_to_string(&mut string)?; + Ok(from_bincode_string(String::from_utf8(decode(&string)?)?.as_bytes())?) + } } } diff --git a/src/lib.rs b/src/lib.rs index 044eaba..fdad0ca 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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;