Skip to content

Commit

Permalink
Support converting Value into Debug (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus authored Apr 30, 2019
1 parent 69b701f commit 73cd623
Show file tree
Hide file tree
Showing 18 changed files with 745 additions and 38 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ matrix:
- rust: beta
script:
- ./ci/travis.sh
- cargo test -p sval_fmt_tests
- rust: stable
script:
- ./ci/travis.sh
- ./ci/travis.sh
8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[workspace]
members = [
"derive",
"tests",
"tests/serde",
"tests/fmt",

"json",
"json/benches",
Expand All @@ -25,7 +26,7 @@ keywords = ["serialization", "no_std"]
categories = ["encoding", "no-std"]

[package.metadata.docs.rs]
features = ["derive", "serde", "test"]
features = ["derive", "serde", "fmt", "test"]

[badges]
travis-ci = { repository = "sval-rs/sval" }
Expand All @@ -44,6 +45,9 @@ derive = ["sval_derive"]
# that can be inspected in tests
test = ["std"]

# Support integration with `std::fmt`
fmt = []

# Support integration with `serde`
serde = ["std", "serde_lib/std"]

Expand Down
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,22 @@ struct MyData {
props: serde_json::Map<String, serde_json::Value>,
}
```

## To integrate with `std::fmt`

`sval` can provide a compatible `Debug` implementation for any `sval::Value`. Add the `fmt` feature to `sval` to enable it:

```toml
[dependencies.sval]
features = ["fmt"]
```

Use the `to_debug` function to turn any `sval::Value` into a `std::fmt::Debug`:

```rust
fn with_value(value: impl Value) {
dbg!(sval::fmt::to_debug(&value));

..
}
```
9 changes: 8 additions & 1 deletion ci/travis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ cargo test
printf "\n\n---- sval with std ----\n\n"
cargo test --features std

printf "\n\n---- sval with fmt ----\n\n"
cargo test --features fmt

printf "\n\n---- sval with serde ----\n\n"
cargo test --features serde

Expand All @@ -25,5 +28,9 @@ cargo test --features std
popd

# other builds
# Benches are checked in the `nightly` build
# Format consistency is checked in the `beta` build
printf "\n\n---- integration tests ----\n\n"
cargo test --all --exclude sval_json_benches
cargo test --all \
--exclude sval_json_benches \
--exclude sval_fmt_tests
7 changes: 5 additions & 2 deletions json/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ extern crate std;
extern crate core as std;

mod fmt;
pub use self::fmt::{Formatter, to_fmt};
pub use self::fmt::{
to_fmt,
Formatter,
};

#[cfg(feature = "std")]
mod std_support;

#[cfg(feature = "std")]
pub use self::std_support::{
Writer,
to_string,
to_writer,
Writer,
};
5 changes: 4 additions & 1 deletion json/src/std_support.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use sval::stream::{self, Stream};
use sval::stream::{
self,
Stream,
};

use crate::std::string::String;
use crate::{
Expand Down
5 changes: 1 addition & 4 deletions src/collect/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ pub(crate) struct Value<'a> {
impl<'a> Value<'a> {
#[inline]
pub(crate) fn new(value: &'a impl value::Value, stack: &'a mut DebugStack) -> Self {
Value {
stack,
value,
}
Value { stack, value }
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ impl fmt::Display for ErrorInner {
}
}

impl From<Error> for fmt::Error {
fn from(_: Error) -> fmt::Error {
fmt::Error
}
}

#[cfg(not(feature = "std"))]
mod no_std_support {
use super::*;
Expand Down
49 changes: 49 additions & 0 deletions src/fmt/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*!
Integration between `sval` and `std::fmt`.
# From `sval` to `std::fmt`
A type that implements [`sval::Value`](../value/trait.Value.html) can be converted into
a type that implements [`std::fmt::Debug`]:
```
# struct MyValue;
# impl sval::value::Value for MyValue {
# fn stream(&self, stream: &mut sval::value::Stream) -> Result<(), sval::value::Error> {
# unimplemented!()
# }
# }
# let my_value = MyValue;
let my_debug = sval::fmt::to_debug(my_value);
```
*/

use crate::{
std::fmt::{
self,
Debug,
Formatter,
},
Value,
};

mod to_debug;

/**
Convert a [`Value`] into a [`Debug`].
The formatted value is not guaranteed to be exactly the same as
a `Debug` implementation that might exist on the type.
This method doesn't need to allocate or perform any buffering.
*/
pub fn to_debug(value: impl Value) -> impl Debug {
self::to_debug::ToDebug(value)
}

/**
Format a [`Value`] using the given [`Formatter`].
*/
pub fn debug(value: impl Value, f: &mut Formatter) -> fmt::Result {
to_debug(value).fmt(f)
}
Loading

0 comments on commit 73cd623

Please sign in to comment.