Skip to content

Commit

Permalink
feat(data): Add IntoData for easier initialization
Browse files Browse the repository at this point in the history
Cherry pick e9dbf68 (assert-rs#292)
Cherry pick 00e746b (assert-rs#292)
Cherry pick fb14410 (assert-rs#292)
  • Loading branch information
epage committed May 17, 2024
1 parent 589d9b8 commit 00c381f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
47 changes: 45 additions & 2 deletions crates/snapbox/src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,52 @@ impl<D: std::fmt::Debug> ToDebug for D {
}
}

/// Convert to [`Data`] with modifiers for `expected` data
pub trait IntoData: Sized {
/// Remove default [`filters`][crate::filter] from this `expected` result
fn raw(self) -> Data {
self.into_data().raw()
}

/// Initialize as [`format`][DataFormat] or [`Error`][DataFormat::Error]
///
/// This is generally used for `expected` data
///
/// # Examples
///
/// ```rust
/// # #[cfg(feature = "json")] {
/// use snapbox::prelude::*;
/// use snapbox::str;
///
/// let expected = str![[r#"{"hello": "world"}"#]]
/// .is(snapbox::data::DataFormat::Json);
/// assert_eq!(expected.format(), snapbox::data::DataFormat::Json);
/// # }
/// ```
fn is(self, format: DataFormat) -> Data {
self.into_data().is(format)
}

/// Convert to [`Data`], applying defaults
fn into_data(self) -> Data;
}

impl<D> IntoData for D
where
D: Into<Data>,
{
fn into_data(self) -> Data {
self.into()
}
}

/// Declare an expected value for an assert from a file
///
/// This is relative to the source file the macro is run from
///
/// Output type: [`Data`]
///
/// ```
/// # #[cfg(feature = "json")] {
/// # use snapbox::file;
Expand Down Expand Up @@ -88,6 +130,8 @@ macro_rules! file {

/// Declare an expected value from within Rust source
///
/// Output type: [`Inline`], see [`IntoData`] for operations
///
/// ```
/// # use snapbox::str;
/// str![["
Expand All @@ -97,8 +141,6 @@ macro_rules! file {
/// ```
///
/// Leading indentation is stripped.
///
/// See [`Inline::is`] for declaring the data to be of a certain format.
#[macro_export]
macro_rules! str {
[$data:literal] => { $crate::str![[$data]] };
Expand Down Expand Up @@ -147,6 +189,7 @@ pub(crate) enum DataInner {
/// - [`file!`] for external snapshots
/// - [`ToString`] for verifying a `Display` representation
/// - [`ToDebug`] for verifying a debug representation
/// - [`IntoData`] for modifying `expected`
impl Data {
/// Mark the data as binary (no post-processing)
pub fn binary(raw: impl Into<Vec<u8>>) -> Self {
Expand Down
2 changes: 2 additions & 0 deletions crates/snapbox/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ pub mod harness;
pub use assert::Action;
pub use assert::Assert;
pub use data::Data;
pub use data::IntoData;
pub use data::ToDebug;
pub use filter::RedactedValue;
pub use filter::Redactions;
Expand All @@ -132,6 +133,7 @@ pub type Error = assert::Error;

/// Easier access to common traits
pub mod prelude {
pub use crate::IntoData;
pub use crate::ToDebug;
}

Expand Down

0 comments on commit 00c381f

Please sign in to comment.