From 00c381f296c7f62efbd26855703b86a7dc5d1dd6 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 22 Apr 2024 16:40:32 -0500 Subject: [PATCH] feat(data): Add IntoData for easier initialization Cherry pick e9dbf6859dc5229acf96d8b5dc0ab57420c5d488 (#292) Cherry pick 00e746b545629048708e0046c2bab102ddd08052 (#292) Cherry pick fb144107063f8f32d195baf4dc378002faec440f (#292) --- crates/snapbox/src/data/mod.rs | 47 ++++++++++++++++++++++++++++++++-- crates/snapbox/src/lib.rs | 2 ++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/crates/snapbox/src/data/mod.rs b/crates/snapbox/src/data/mod.rs index 5056c777..450d4bae 100644 --- a/crates/snapbox/src/data/mod.rs +++ b/crates/snapbox/src/data/mod.rs @@ -50,10 +50,52 @@ impl 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 IntoData for D +where + D: Into, +{ + 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; @@ -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![[" @@ -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]] }; @@ -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>) -> Self { diff --git a/crates/snapbox/src/lib.rs b/crates/snapbox/src/lib.rs index fb5dbe6c..5f62ed24 100644 --- a/crates/snapbox/src/lib.rs +++ b/crates/snapbox/src/lib.rs @@ -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; @@ -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; }