From b9ea2d1514915c141ec221891d6ffe3f289cafa4 Mon Sep 17 00:00:00 2001 From: Ryan Date: Tue, 10 Oct 2017 18:36:33 -0700 Subject: [PATCH] add support for ron serialization --- Cargo.toml | 2 ++ src/lib.rs | 3 +++ src/ron_enc.rs | 21 +++++++++++++++++++++ 3 files changed, 26 insertions(+) create mode 100644 src/ron_enc.rs diff --git a/Cargo.toml b/Cargo.toml index af4ebd7..b759d9a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] diff --git a/src/lib.rs b/src/lib.rs index e26c38e..fc03d5b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/ron_enc.rs b/src/ron_enc.rs new file mode 100644 index 0000000..6b8ebf5 --- /dev/null +++ b/src/ron_enc.rs @@ -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(value: &T) -> ser::Result + where T: Serialize +{ + ser::to_string(value) +} + +pub fn deserialize(bytes: &I) -> Result + where T: DeserializeOwned, + I: AsRef<[u8]> +{ + let string = try!(String::from_utf8(bytes.as_ref().to_vec())); + Ok(de::from_str(&string)?) +}