Skip to content

Commit

Permalink
add serde support to Value
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus committed Nov 29, 2020
1 parent c6be245 commit 71d6ed3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ exclude = ["rfcs/**/*", "/.travis.yml", "/appveyor.yml"]
build = "build.rs"

[package.metadata.docs.rs]
features = ["std", "serde", "kv_unstable_std", "kv_unstable_sval"]
features = ["std", "serde", "kv_unstable_std", "kv_unstable_sval", "kv_unstable_serde"]

[[test]]
name = "filters"
Expand Down Expand Up @@ -48,6 +48,7 @@ std = []
kv_unstable = ["value-bag"]
kv_unstable_sval = ["kv_unstable", "value-bag/sval", "sval"]
kv_unstable_std = ["std", "kv_unstable", "value-bag/error"]
kv_unstable_serde = ["kv_unstable_std", "value-bag/serde", "serde"]

[dependencies]
cfg-if = "1.0"
Expand Down
43 changes: 43 additions & 0 deletions src/kv/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ extern crate value_bag;
#[cfg(feature = "kv_unstable_sval")]
extern crate sval;

#[cfg(feature = "kv_unstable_serde")]
extern crate serde;

use self::value_bag::ValueBag;

pub use kv::Error;
Expand Down Expand Up @@ -147,6 +150,17 @@ impl<'v> Value<'v> {
}
}

#[cfg(feature = "kv_unstable_serde")]
/// Get a value from a type implementing `serde::Serialize`.
pub fn capture_serde<T>(value: &'v T) -> Self
where
T: self::serde::Serialize + 'static,
{
Value {
inner: ValueBag::capture_serde1(value),
}
}

/// Get a value from a type implementing `sval::value::Value`.
#[cfg(feature = "kv_unstable_sval")]
pub fn capture_sval<T>(value: &'v T) -> Self
Expand Down Expand Up @@ -178,6 +192,17 @@ impl<'v> Value<'v> {
}
}

/// Get a value from a type implementing `serde::Serialize`.
#[cfg(feature = "kv_unstable_serde")]
pub fn from_serde<T>(value: &'v T) -> Self
where
T: self::serde::Serialize,
{
Value {
inner: ValueBag::from_serde1(value),
}
}

/// Get a value from a type implementing `sval::value::Value`.
#[cfg(feature = "kv_unstable_sval")]
pub fn from_sval<T>(value: &'v T) -> Self
Expand Down Expand Up @@ -261,6 +286,16 @@ impl ToValue for dyn std::error::Error + 'static {
}
}

#[cfg(feature = "kv_unstable_serde")]
impl<'v> self::serde::Serialize for Value<'v> {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where
S: self::serde::Serializer,
{
self.inner.serialize(s)
}
}

#[cfg(feature = "kv_unstable_sval")]
impl<'v> self::sval::value::Value for Value<'v> {
fn stream(&self, stream: &mut self::sval::value::Stream) -> self::sval::value::Result {
Expand Down Expand Up @@ -485,6 +520,14 @@ pub(crate) mod tests {
assert!(Value::from_dyn_error(&err).to_error().is_some());
}

#[cfg(feature = "kv_unstable_serde")]
#[test]
fn test_capture_serde() {
assert_eq!(Some(42u64), Value::capture_serde(&42).to_u64());

assert_eq!(Some(42u64), Value::from_serde(&42).to_u64());
}

#[cfg(feature = "kv_unstable_sval")]
#[test]
fn test_capture_sval() {
Expand Down

0 comments on commit 71d6ed3

Please sign in to comment.