Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion crates/snapbox/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ examples = ["dep:escargot"]
regex = ["dep:regex"]

## Snapshotting of json
json = ["structured-data", "dep:serde_json"]
json = ["structured-data", "dep:serde_json", "dep:serde"]
## Snapshotting of term styling
term-svg = ["structured-data", "dep:anstyle-svg"]
## Snapshotting of structured data
Expand Down Expand Up @@ -97,6 +97,7 @@ document-features = { version = "0.2.6", optional = true }
serde_json = { version = "1.0.85", optional = true}
anstyle-svg = { version = "0.1.3", optional = true }
regex = { version = "1.10.4", optional = true, default-features = false, features = ["std"] }
serde = { version = "1.0.198", optional = true }

[target.'cfg(windows)'.dependencies]
windows-sys = { version = "0.52.0", features = ["Win32_Foundation"], optional = true }
Expand Down
31 changes: 31 additions & 0 deletions crates/snapbox/src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,35 @@ impl<D: std::fmt::Debug> ToDebug for D {
}
}

/// Capture the serde representation of a value
///
/// ```rust,no_run
/// use snapbox::IntoJson as _;
///
/// fn some_function() -> usize {
/// // ...
/// # 5
/// }
///
/// let actual = some_function();
/// let expected = snapbox::str![["5"]];
/// snapbox::assert_eq(actual.into_json(), expected);
/// ```
#[cfg(feature = "json")]
pub trait IntoJson {
fn into_json(self) -> Data;
}

#[cfg(feature = "json")]
impl<S: serde::Serialize> IntoJson for S {
fn into_json(self) -> Data {
match serde_json::to_value(self) {
Ok(value) => Data::json(value),
Err(err) => Data::error(err.to_string(), DataFormat::Json),
}
}
}

/// Convert to [`Data`] with modifiers for `expected` data
pub trait IntoData: Sized {
/// Remove default [`filters`][crate::filters] from this `expected` result
Expand Down Expand Up @@ -215,7 +244,9 @@ pub(crate) enum DataInner {
/// See also
/// - [`str!`] for inline snapshots
/// - [`file!`] for external snapshots
/// - [`ToString`] for verifying a `Display` representation
/// - [`ToDebug`] for verifying a debug representation
/// - [`IntoJson`] for verifying the serde representation
/// - [`IntoData`] for modifying `expected`
impl Data {
/// Mark the data as binary (no post-processing)
Expand Down
4 changes: 4 additions & 0 deletions crates/snapbox/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ pub use action::DEFAULT_ACTION_ENV;
pub use assert::Assert;
pub use data::Data;
pub use data::IntoData;
#[cfg(feature = "json")]
pub use data::IntoJson;
pub use data::ToDebug;
pub use error::Error;
pub use filters::Redactions;
Expand All @@ -123,6 +125,8 @@ pub type Result<T, E = Error> = std::result::Result<T, E>;
/// Easier access to common traits
pub mod prelude {
pub use crate::IntoData;
#[cfg(feature = "json")]
pub use crate::IntoJson;
pub use crate::ToDebug;
}

Expand Down