Skip to content

Commit

Permalink
Merge pull request #342 from KodrAus/feat/sval-values
Browse files Browse the repository at this point in the history
Add structured support to kv::Value using sval
  • Loading branch information
KodrAus authored Jul 24, 2019
2 parents e5bfc54 + 368a777 commit c098145
Show file tree
Hide file tree
Showing 8 changed files with 245 additions and 192 deletions.
20 changes: 12 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
language: rust
sudo: false
rust:
- 1.16.0
- stable
- beta
- nightly
matrix:
include:
- rust: 1.21.0
script:
- cargo test --verbose --features kv_unstable
- cargo test --verbose --features "kv_unstable std"
- rust: stable
env:
- LABEL="Embedded"
script:
- rustup target add thumbv6m-none-eabi
- cargo build --verbose --target=thumbv6m-none-eabi
- rust: 1.16.0
env:
- LABEL="MSRV"
script:
- cargo build --verbose
- cargo build --verbose --features serde
- cargo build --verbose --features std
script:
- cargo build --verbose
- cargo build --verbose --features serde
- cargo build --verbose --features std
- cargo test --verbose
- cargo test --verbose --features serde
- cargo test --verbose --features std
- cargo test --verbose --features kv_unstable
- cargo test --verbose --features "kv_unstable std"
- cargo test --verbose --features "kv_unstable_sval"
- cargo run --verbose --manifest-path test_max_level_features/Cargo.toml
- cargo run --verbose --manifest-path test_max_level_features/Cargo.toml --release

Expand Down
8 changes: 6 additions & 2 deletions 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"]
features = ["std", "serde", "kv_unstable_sval"]

[[test]]
name = "filters"
Expand All @@ -39,8 +39,10 @@ release_max_level_trace = []

std = []

# requires Rust `>= 1.21.0`
# requires the latest stable
# this will have a tighter MSRV before stabilization
kv_unstable = []
kv_unstable_sval = ["kv_unstable", "sval/fmt"]

[badges]
travis-ci = { repository = "rust-lang-nursery/log" }
Expand All @@ -49,6 +51,8 @@ appveyor = { repository = "alexcrichton/log" }
[dependencies]
cfg-if = "0.1.2"
serde = { version = "1.0", optional = true, default-features = false }
sval = { version = "0.4.2", optional = true, default-features = false }

[dev-dependencies]
serde_test = "1.0"
sval = { version = "0.4.2", features = ["test"] }
32 changes: 19 additions & 13 deletions src/kv/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use std::fmt;
#[cfg(feature = "std")]
use std::io;

/// An error encountered while working with structured data.
#[derive(Debug)]
Expand All @@ -11,13 +9,13 @@ pub struct Error {
#[derive(Debug)]
enum Inner {
#[cfg(feature = "std")]
Io(io::Error),
Boxed(std_support::BoxedError),
Msg(&'static str),
Fmt,
}

impl Error {
/// Create an error from the given message.
/// Create an error from a message.
pub fn msg(msg: &'static str) -> Self {
Error {
inner: Inner::Msg(msg),
Expand All @@ -30,7 +28,7 @@ impl fmt::Display for Error {
use self::Inner::*;
match &self.inner {
#[cfg(feature = "std")]
&Io(ref err) => err.fmt(f),
&Boxed(ref err) => err.fmt(f),
&Msg(ref msg) => msg.fmt(f),
&Fmt => fmt::Error.fmt(f),
}
Expand All @@ -56,6 +54,20 @@ mod std_support {
use super::*;
use std::{error, io};

pub(super) type BoxedError = Box<error::Error + Send + Sync>;

impl Error {
/// Create an error from a standard error type.
pub fn boxed<E>(err: E) -> Self
where
E: Into<BoxedError>,
{
Error {
inner: Inner::Boxed(err.into())
}
}
}

impl error::Error for Error {
fn description(&self) -> &str {
"key values error"
Expand All @@ -64,19 +76,13 @@ mod std_support {

impl From<io::Error> for Error {
fn from(err: io::Error) -> Self {
Error {
inner: Inner::Io(err)
}
Error::boxed(err)
}
}

impl From<Error> for io::Error {
fn from(err: Error) -> Self {
if let Inner::Io(err) = err.inner {
err
} else {
io::Error::new(io::ErrorKind::Other, err)
}
io::Error::new(io::ErrorKind::Other, err)
}
}
}
20 changes: 10 additions & 10 deletions src/kv/value/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,16 +241,16 @@ mod tests {

#[test]
fn test_to_value_display() {
assert_eq!(42u64.to_value().to_str_buf(), "42");
assert_eq!(42i64.to_value().to_str_buf(), "42");
assert_eq!(42.01f64.to_value().to_str_buf(), "42.01");
assert_eq!(true.to_value().to_str_buf(), "true");
assert_eq!('a'.to_value().to_str_buf(), "'a'");
assert_eq!(format_args!("a {}", "value").to_value().to_str_buf(), "a value");
assert_eq!("a loong string".to_value().to_str_buf(), "\"a loong string\"");
assert_eq!(Some(true).to_value().to_str_buf(), "true");
assert_eq!(().to_value().to_str_buf(), "None");
assert_eq!(Option::None::<bool>.to_value().to_str_buf(), "None");
assert_eq!(42u64.to_value().to_string(), "42");
assert_eq!(42i64.to_value().to_string(), "42");
assert_eq!(42.01f64.to_value().to_string(), "42.01");
assert_eq!(true.to_value().to_string(), "true");
assert_eq!('a'.to_value().to_string(), "'a'");
assert_eq!(format_args!("a {}", "value").to_value().to_string(), "a value");
assert_eq!("a loong string".to_value().to_string(), "\"a loong string\"");
assert_eq!(Some(true).to_value().to_string(), "true");
assert_eq!(().to_value().to_string(), "None");
assert_eq!(Option::None::<bool>.to_value().to_string(), "None");
}

#[test]
Expand Down
Loading

0 comments on commit c098145

Please sign in to comment.