mirror of
https://github.com/Drop-OSS/dropbreak.git
synced 2026-07-18 09:57:53 -04:00
add support for ron serialization
This commit is contained in:
@@ -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"]
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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)?)
|
||||
}
|
||||
Reference in New Issue
Block a user