Skip to content

Commit 191a360

Browse files
authored
Merge pull request #293 from epage/json
feat(data): Add IntoJson
2 parents 62977b5 + f0aece9 commit 191a360

File tree

4 files changed

+62
-14
lines changed

4 files changed

+62
-14
lines changed

Cargo.lock

Lines changed: 25 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/snapbox/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ examples = ["dep:escargot"]
4646
regex = ["dep:regex"]
4747

4848
## Snapshotting of json
49-
json = ["structured-data", "dep:serde_json"]
49+
json = ["structured-data", "dep:serde_json", "dep:serde"]
5050
## Snapshotting of term styling
5151
term-svg = ["structured-data", "dep:anstyle-svg"]
5252
## Snapshotting of structured data
@@ -97,6 +97,7 @@ document-features = { version = "0.2.6", optional = true }
9797
serde_json = { version = "1.0.85", optional = true}
9898
anstyle-svg = { version = "0.1.3", optional = true }
9999
regex = { version = "1.10.4", optional = true, default-features = false, features = ["std"] }
100+
serde = { version = "1.0.198", optional = true }
100101

101102
[target.'cfg(windows)'.dependencies]
102103
windows-sys = { version = "0.52.0", features = ["Win32_Foundation"], optional = true }

crates/snapbox/src/data/mod.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,35 @@ impl<D: std::fmt::Debug> ToDebug for D {
4242
}
4343
}
4444

45+
/// Capture the serde representation of a value
46+
///
47+
/// ```rust,no_run
48+
/// use snapbox::IntoJson as _;
49+
///
50+
/// fn some_function() -> usize {
51+
/// // ...
52+
/// # 5
53+
/// }
54+
///
55+
/// let actual = some_function();
56+
/// let expected = snapbox::str![["5"]];
57+
/// snapbox::assert_eq(actual.into_json(), expected);
58+
/// ```
59+
#[cfg(feature = "json")]
60+
pub trait IntoJson {
61+
fn into_json(self) -> Data;
62+
}
63+
64+
#[cfg(feature = "json")]
65+
impl<S: serde::Serialize> IntoJson for S {
66+
fn into_json(self) -> Data {
67+
match serde_json::to_value(self) {
68+
Ok(value) => Data::json(value),
69+
Err(err) => Data::error(err.to_string(), DataFormat::Json),
70+
}
71+
}
72+
}
73+
4574
/// Convert to [`Data`] with modifiers for `expected` data
4675
pub trait IntoData: Sized {
4776
/// Remove default [`filters`][crate::filters] from this `expected` result
@@ -215,7 +244,9 @@ pub(crate) enum DataInner {
215244
/// See also
216245
/// - [`str!`] for inline snapshots
217246
/// - [`file!`] for external snapshots
247+
/// - [`ToString`] for verifying a `Display` representation
218248
/// - [`ToDebug`] for verifying a debug representation
249+
/// - [`IntoJson`] for verifying the serde representation
219250
/// - [`IntoData`] for modifying `expected`
220251
impl Data {
221252
/// Mark the data as binary (no post-processing)

crates/snapbox/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ pub use action::DEFAULT_ACTION_ENV;
113113
pub use assert::Assert;
114114
pub use data::Data;
115115
pub use data::IntoData;
116+
#[cfg(feature = "json")]
117+
pub use data::IntoJson;
116118
pub use data::ToDebug;
117119
pub use error::Error;
118120
pub use filters::Redactions;
@@ -123,6 +125,8 @@ pub type Result<T, E = Error> = std::result::Result<T, E>;
123125
/// Easier access to common traits
124126
pub mod prelude {
125127
pub use crate::IntoData;
128+
#[cfg(feature = "json")]
129+
pub use crate::IntoJson;
126130
pub use crate::ToDebug;
127131
}
128132

0 commit comments

Comments
 (0)