Skip to content

Commit

Permalink
test sval feature in CI
Browse files Browse the repository at this point in the history
split consumer builds of MSRV from dev builds
  • Loading branch information
KodrAus committed Jul 16, 2019
1 parent 06beb05 commit 368a777
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 148 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.31.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
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ release_max_level_trace = []

std = []

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

Expand All @@ -54,3 +55,4 @@ 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
62 changes: 44 additions & 18 deletions src/kv/value/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ use std::fmt;
use super::{Fill, Slot, Error};
use kv;

// `Visit` and `Visitor` is an internal API for visiting the structure of a value.
// `Visitor` is an internal API for visiting the structure of a value.
// It's not intended to be public (at this stage).
//
// Right now we only have an implementation for `std::fmt`, but
// this trait makes it possible to add more structured backends like
// `serde` that can retain that original structure.

/// A container for a structured value for a specific kind of visitor.
#[derive(Clone, Copy)]
Expand Down Expand Up @@ -165,7 +161,7 @@ mod fmt_support {
}

#[cfg(feature = "kv_unstable_sval")]
mod sval_support {
pub(super) mod sval_support {
use super::*;

extern crate sval;
Expand All @@ -184,55 +180,85 @@ mod sval_support {

impl<'v> sval::Value for kv::Value<'v> {
fn stream(&self, s: &mut sval::value::Stream) -> sval::value::Result {
self.visit(&mut SvalVisitor(s)).map_err(|_| sval::value::Error::msg("sval formatting failed"))?;
self.visit(&mut SvalVisitor(s)).map_err(Error::into_sval)?;

Ok(())
}
}

pub(super) use self::sval::Value;
pub(in kv::value) use self::sval::Value;

pub(super) fn fmt(f: &mut fmt::Formatter, v: &sval::Value) -> Result<(), Error> {
sval::fmt::debug(f, v).map_err(|_| Error::msg("sval formatting failed"))
sval::fmt::debug(f, v)?;
Ok(())
}

impl Error {
fn from_sval(_: sval::value::Error) -> Self {
Error::msg("`sval` serialization failed")
}

fn into_sval(self) -> sval::value::Error {
sval::value::Error::msg("`sval` serialization failed")
}
}

struct SvalVisitor<'a, 'b: 'a>(&'a mut sval::value::Stream<'b>);

impl<'a, 'b: 'a> Visitor for SvalVisitor<'a, 'b> {
fn debug(&mut self, v: &fmt::Debug) -> Result<(), Error> {
self.0.fmt(format_args!("{:?}", v)).map_err(|_| Error::msg("sval formatting failed"))
self.0.fmt(format_args!("{:?}", v)).map_err(Error::from_sval)
}

fn u64(&mut self, v: u64) -> Result<(), Error> {
self.0.u64(v).map_err(|_| Error::msg("sval formatting failed"))
self.0.u64(v).map_err(Error::from_sval)
}

fn i64(&mut self, v: i64) -> Result<(), Error> {
self.0.i64(v).map_err(|_| Error::msg("sval formatting failed"))
self.0.i64(v).map_err(Error::from_sval)
}

fn f64(&mut self, v: f64) -> Result<(), Error> {
self.0.f64(v).map_err(|_| Error::msg("sval formatting failed"))
self.0.f64(v).map_err(Error::from_sval)
}

fn bool(&mut self, v: bool) -> Result<(), Error> {
self.0.bool(v).map_err(|_| Error::msg("sval formatting failed"))
self.0.bool(v).map_err(Error::from_sval)
}

fn char(&mut self, v: char) -> Result<(), Error> {
self.0.char(v).map_err(|_| Error::msg("sval formatting failed"))
self.0.char(v).map_err(Error::from_sval)
}

fn str(&mut self, v: &str) -> Result<(), Error> {
self.0.str(v).map_err(|_| Error::msg("sval formatting failed"))
self.0.str(v).map_err(Error::from_sval)
}

fn none(&mut self) -> Result<(), Error> {
self.0.none().map_err(|_| Error::msg("sval formatting failed"))
self.0.none().map_err(Error::from_sval)
}

fn sval(&mut self, v: &sval::Value) -> Result<(), Error> {
self.0.any(v).map_err(|_| Error::msg("sval formatting failed"))
self.0.any(v).map_err(Error::from_sval)
}
}

#[cfg(test)]
mod tests {
use super::*;
use kv::value::test::Token;

#[test]
fn test_from_sval() {
assert_eq!(kv::Value::from_sval(&42u64).to_token(), Token::Sval);
}

#[test]
fn test_sval_structured() {
let value = kv::Value::from(42u64);
let expected = vec![sval::test::Token::Unsigned(42)];

assert_eq!(sval::test::tokens(value), expected);
}
}
}
4 changes: 2 additions & 2 deletions src/kv/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ mod tests {
}
}

assert_eq!("1", Value::from_fill(&TestFill).to_str_buf());
assert_eq!("1", Value::from_fill(&TestFill).to_string());
}

#[test]
Expand All @@ -150,6 +150,6 @@ mod tests {
}
}

let _ = Value::from_fill(&BadFill).to_str_buf();
let _ = Value::from_fill(&BadFill).to_string();
}
}
Loading

0 comments on commit 368a777

Please sign in to comment.