add support for ron serialization

This commit is contained in:
Ryan
2017-10-10 18:36:33 -07:00
parent 561c2031b2
commit b9ea2d1514
3 changed files with 26 additions and 0 deletions
+2
View File
@@ -13,6 +13,7 @@ version = "1.3.0"
[dependencies]
bincode = { version = "0.8", optional = true }
serde_yaml = { version = "0.7", optional = true }
ron = { version = "0.1.3", optional = true }
fs2 = "0.4"
quick-error = "1.1.0"
serde = "1"
@@ -25,3 +26,4 @@ lazy_static = "0.2.1"
default = ["bin"]
bin = ["bincode"]
yaml = ["serde_yaml"]
ron_enc = ["ron"]
+3
View File
@@ -68,15 +68,18 @@ extern crate serde;
extern crate fs2;
#[cfg(feature = "bin")] extern crate bincode;
#[cfg(feature = "yaml")] extern crate serde_yaml;
#[cfg(feature = "ron_enc")] extern crate ron;
#[cfg(test)] extern crate tempfile;
mod error;
#[cfg(feature = "bin")] mod bincode_enc;
#[cfg(feature = "yaml")] mod yaml_enc;
#[cfg(feature = "ron_enc")] mod ron_enc;
mod enc {
#[cfg(feature = "bin")] pub use bincode_enc::*;
#[cfg(feature = "yaml")] pub use yaml_enc::*;
#[cfg(feature = "ron_enc")] pub use ron_enc::*;
}
use std::collections::HashMap;
+21
View File
@@ -0,0 +1,21 @@
use serde::Serialize;
use serde::de::DeserializeOwned;
use ron::{ser, de};
pub type Repr = String;
pub type SerializeError = ser::Error;
pub type DeserializeError = de::Error;
pub fn serialize<T>(value: &T) -> ser::Result<String>
where T: Serialize
{
ser::to_string(value)
}
pub fn deserialize<T, I>(bytes: &I) -> Result<T, ::error::BreakError>
where T: DeserializeOwned,
I: AsRef<[u8]>
{
let string = try!(String::from_utf8(bytes.as_ref().to_vec()));
Ok(de::from_str(&string)?)
}