Skip to content

Commit 0865e04

Browse files
authored
Merge pull request #250 from epage/cleanup
fix(snap)!: Clean up API
2 parents ab6b1be + fafd687 commit 0865e04

File tree

8 files changed

+74
-72
lines changed

8 files changed

+74
-72
lines changed

crates/snapbox/src/assert.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ impl Assert {
129129
// On `expected` being an error, make a best guess
130130
let format = expected.format();
131131

132-
actual = actual.try_coerce(format).normalize(NormalizeNewlines);
132+
actual = actual.coerce_to(format).normalize(NormalizeNewlines);
133133

134134
(expected, actual)
135135
}
@@ -142,7 +142,7 @@ impl Assert {
142142
let expected = expected.normalize(NormalizeNewlines);
143143
// On `expected` being an error, make a best guess
144144
let format = expected.format();
145-
actual = actual.try_coerce(format);
145+
actual = actual.coerce_to(format);
146146

147147
if self.normalize_paths {
148148
actual = actual.normalize(NormalizePaths);

crates/snapbox/src/data/mod.rs

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ macro_rules! file {
3535
}};
3636
[_ : $type:ident] => {{
3737
let stem = ::std::path::Path::new(::std::file!()).file_stem().unwrap();
38-
let ext = $crate::DataFormat:: $type.ext();
38+
let ext = $crate::data::DataFormat:: $type.ext();
3939
let rel_path = ::std::format!("snapshots/{}-{}.{ext}", stem.to_str().unwrap(), line!());
4040
let mut path = $crate::current_dir!();
4141
path.push(rel_path);
42-
$crate::Data::read_from(&path, Some($crate::DataFormat:: $type))
42+
$crate::Data::read_from(&path, Some($crate::data::DataFormat:: $type))
4343
}};
4444
[$path:literal] => {{
4545
let mut path = $crate::current_dir!();
@@ -49,7 +49,7 @@ macro_rules! file {
4949
[$path:literal : $type:ident] => {{
5050
let mut path = $crate::current_dir!();
5151
path.push($path);
52-
$crate::Data::read_from(&path, Some($crate::DataFormat:: $type))
52+
$crate::Data::read_from(&path, Some($crate::data::DataFormat:: $type))
5353
}};
5454
}
5555

@@ -74,45 +74,37 @@ enum DataInner {
7474
impl Data {
7575
/// Mark the data as binary (no post-processing)
7676
pub fn binary(raw: impl Into<Vec<u8>>) -> Self {
77-
Self {
78-
inner: DataInner::Binary(raw.into()),
79-
source: None,
80-
}
77+
DataInner::Binary(raw.into()).into()
8178
}
8279

8380
/// Mark the data as text (post-processing)
8481
pub fn text(raw: impl Into<String>) -> Self {
85-
Self {
86-
inner: DataInner::Text(raw.into()),
87-
source: None,
88-
}
82+
DataInner::Text(raw.into()).into()
8983
}
9084

9185
#[cfg(feature = "json")]
9286
pub fn json(raw: impl Into<serde_json::Value>) -> Self {
93-
Self {
94-
inner: DataInner::Json(raw.into()),
95-
source: None,
96-
}
87+
DataInner::Json(raw.into()).into()
9788
}
9889

9990
fn error(raw: impl Into<crate::Error>) -> Self {
100-
Self {
101-
inner: DataInner::Error(raw.into()),
102-
source: None,
103-
}
91+
DataInner::Error(raw.into()).into()
10492
}
10593

10694
/// Empty test data
10795
pub fn new() -> Self {
10896
Self::text("")
10997
}
11098

111-
fn with_path(mut self, path: impl Into<std::path::PathBuf>) -> Self {
112-
self.source = Some(DataSource::path(path));
99+
fn with_source(mut self, source: impl Into<DataSource>) -> Self {
100+
self.source = Some(source.into());
113101
self
114102
}
115103

104+
fn with_path(self, path: impl Into<std::path::PathBuf>) -> Self {
105+
self.with_source(path.into())
106+
}
107+
116108
/// Load test data from a file
117109
pub fn read_from(path: &std::path::Path, data_format: Option<DataFormat>) -> Self {
118110
match Self::try_read_from(path, data_format) {
@@ -156,8 +148,8 @@ impl Data {
156148
.unwrap_or_default()
157149
{
158150
#[cfg(feature = "json")]
159-
"json" => data.try_coerce(DataFormat::Json),
160-
_ => data.try_coerce(DataFormat::Text),
151+
"json" => data.coerce_to(DataFormat::Json),
152+
_ => data.coerce_to(DataFormat::Text),
161153
}
162154
}
163155
};
@@ -220,13 +212,10 @@ impl Data {
220212
}
221213
}
222214

223-
pub fn try_coerce(self, format: DataFormat) -> Self {
215+
pub fn coerce_to(self, format: DataFormat) -> Self {
224216
let mut data = match (self.inner, format) {
225217
(DataInner::Error(inner), _) => Self::error(inner),
226-
(inner, DataFormat::Error) => Self {
227-
inner,
228-
source: None,
229-
},
218+
(inner, DataFormat::Error) => inner.into(),
230219
(DataInner::Binary(inner), DataFormat::Binary) => Self::binary(inner),
231220
(DataInner::Text(inner), DataFormat::Text) => Self::text(inner),
232221
#[cfg(feature = "json")]
@@ -237,11 +226,11 @@ impl Data {
237226
} else {
238227
match String::from_utf8(inner) {
239228
Ok(str) => {
240-
let coerced = Self::text(str).try_coerce(format);
229+
let coerced = Self::text(str).coerce_to(format);
241230
// if the Text cannot be coerced into the correct format
242231
// reset it back to Binary
243232
if coerced.format() != format {
244-
coerced.try_coerce(DataFormat::Binary)
233+
coerced.coerce_to(DataFormat::Binary)
245234
} else {
246235
coerced
247236
}
@@ -260,21 +249,14 @@ impl Data {
260249
Err(_) => Self::text(inner),
261250
}
262251
}
263-
(inner, DataFormat::Binary) => Self::binary(
264-
Self {
265-
inner,
266-
source: None,
267-
}
268-
.to_bytes()
269-
.expect("error case handled"),
270-
),
252+
(inner, DataFormat::Binary) => {
253+
let remake: Self = inner.into();
254+
Self::binary(remake.to_bytes().expect("error case handled"))
255+
}
271256
// This variant is already covered unless structured data is enabled
272257
#[cfg(feature = "structured-data")]
273258
(inner, DataFormat::Text) => {
274-
let remake = Self {
275-
inner,
276-
source: None,
277-
};
259+
let remake: Self = inner.into();
278260
if let Some(str) = remake.render() {
279261
Self::text(str)
280262
} else {
@@ -298,6 +280,15 @@ impl Data {
298280
}
299281
}
300282

283+
impl From<DataInner> for Data {
284+
fn from(inner: DataInner) -> Self {
285+
Data {
286+
inner,
287+
source: None,
288+
}
289+
}
290+
}
291+
301292
impl std::fmt::Display for Data {
302293
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
303294
match &self.inner {

crates/snapbox/src/data/source.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ impl DataSource {
2626
}
2727
}
2828

29+
impl From<&'_ std::path::Path> for DataSource {
30+
fn from(value: &'_ std::path::Path) -> Self {
31+
Self::path(value)
32+
}
33+
}
34+
35+
impl From<std::path::PathBuf> for DataSource {
36+
fn from(value: std::path::PathBuf) -> Self {
37+
Self::path(value)
38+
}
39+
}
40+
2941
impl std::fmt::Display for DataSource {
3042
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
3143
match &self.inner {

crates/snapbox/src/data/tests.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ fn json_to_bytes_render() {
2828
fn binary_to_text() {
2929
let binary = String::from("test").into_bytes();
3030
let d = Data::binary(binary);
31-
let text = d.try_coerce(DataFormat::Text);
31+
let text = d.coerce_to(DataFormat::Text);
3232
assert_eq!(DataFormat::Text, text.format())
3333
}
3434

3535
#[test]
3636
fn binary_to_text_not_utf8() {
3737
let binary = b"\xFF\xE0\x00\x10\x4A\x46\x49\x46\x00".to_vec();
3838
let d = Data::binary(binary);
39-
let d = d.try_coerce(DataFormat::Text);
39+
let d = d.coerce_to(DataFormat::Text);
4040
assert_ne!(DataFormat::Text, d.format());
4141
assert_eq!(DataFormat::Binary, d.format());
4242
}
@@ -47,7 +47,7 @@ fn binary_to_json() {
4747
let value = json!({"name": "John\\Doe\r\n"});
4848
let binary = serde_json::to_vec_pretty(&value).unwrap();
4949
let d = Data::binary(binary);
50-
let json = d.try_coerce(DataFormat::Json);
50+
let json = d.coerce_to(DataFormat::Json);
5151
assert_eq!(DataFormat::Json, json.format());
5252
}
5353

@@ -56,7 +56,7 @@ fn binary_to_json() {
5656
fn binary_to_json_not_utf8() {
5757
let binary = b"\xFF\xE0\x00\x10\x4A\x46\x49\x46\x00".to_vec();
5858
let d = Data::binary(binary);
59-
let d = d.try_coerce(DataFormat::Json);
59+
let d = d.coerce_to(DataFormat::Json);
6060
assert_ne!(DataFormat::Json, d.format());
6161
assert_eq!(DataFormat::Binary, d.format());
6262
}
@@ -66,7 +66,7 @@ fn binary_to_json_not_utf8() {
6666
fn binary_to_json_not_json() {
6767
let binary = String::from("test").into_bytes();
6868
let d = Data::binary(binary);
69-
let d = d.try_coerce(DataFormat::Json);
69+
let d = d.coerce_to(DataFormat::Json);
7070
assert_ne!(DataFormat::Json, d.format());
7171
assert_eq!(DataFormat::Binary, d.format());
7272
}
@@ -75,7 +75,7 @@ fn binary_to_json_not_json() {
7575
fn text_to_binary() {
7676
let text = String::from("test");
7777
let d = Data::text(text);
78-
let binary = d.try_coerce(DataFormat::Binary);
78+
let binary = d.coerce_to(DataFormat::Binary);
7979
assert_eq!(DataFormat::Binary, binary.format());
8080
}
8181

@@ -85,7 +85,7 @@ fn text_to_json() {
8585
let value = json!({"name": "John\\Doe\r\n"});
8686
let text = serde_json::to_string_pretty(&value).unwrap();
8787
let d = Data::text(text);
88-
let json = d.try_coerce(DataFormat::Json);
88+
let json = d.coerce_to(DataFormat::Json);
8989
assert_eq!(DataFormat::Json, json.format());
9090
}
9191

@@ -94,7 +94,7 @@ fn text_to_json() {
9494
fn text_to_json_not_json() {
9595
let text = String::from("test");
9696
let d = Data::text(text);
97-
let json = d.try_coerce(DataFormat::Json);
97+
let json = d.coerce_to(DataFormat::Json);
9898
assert_eq!(DataFormat::Text, json.format());
9999
}
100100

@@ -103,7 +103,7 @@ fn text_to_json_not_json() {
103103
fn json_to_binary() {
104104
let value = json!({"name": "John\\Doe\r\n"});
105105
let d = Data::json(value);
106-
let binary = d.try_coerce(DataFormat::Binary);
106+
let binary = d.coerce_to(DataFormat::Binary);
107107
assert_eq!(DataFormat::Binary, binary.format());
108108
}
109109

@@ -112,7 +112,7 @@ fn json_to_binary() {
112112
fn json_to_text() {
113113
let value = json!({"name": "John\\Doe\r\n"});
114114
let d = Data::json(value);
115-
let text = d.try_coerce(DataFormat::Text);
115+
let text = d.coerce_to(DataFormat::Text);
116116
assert_eq!(DataFormat::Text, text.format());
117117
}
118118

@@ -124,7 +124,7 @@ fn json_to_text() {
124124
fn text_to_bin_coerce_equals_to_bytes() {
125125
let text = String::from("test");
126126
let d = Data::text(text);
127-
let binary = d.clone().try_coerce(DataFormat::Binary);
127+
let binary = d.clone().coerce_to(DataFormat::Binary);
128128
assert_eq!(Data::binary(d.to_bytes().unwrap()), binary);
129129
}
130130

@@ -133,7 +133,7 @@ fn text_to_bin_coerce_equals_to_bytes() {
133133
fn json_to_bin_coerce_equals_to_bytes() {
134134
let json = json!({"name": "John\\Doe\r\n"});
135135
let d = Data::json(json);
136-
let binary = d.clone().try_coerce(DataFormat::Binary);
136+
let binary = d.clone().coerce_to(DataFormat::Binary);
137137
assert_eq!(Data::binary(d.to_bytes().unwrap()), binary);
138138
}
139139

@@ -142,7 +142,7 @@ fn json_to_bin_coerce_equals_to_bytes() {
142142
fn json_to_text_coerce_equals_render() {
143143
let json = json!({"name": "John\\Doe\r\n"});
144144
let d = Data::json(json);
145-
let text = d.clone().try_coerce(DataFormat::Text);
145+
let text = d.clone().coerce_to(DataFormat::Text);
146146
assert_eq!(Data::text(d.render().unwrap()), text);
147147
}
148148

crates/snapbox/src/lib.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,12 @@
9595

9696
mod action;
9797
mod assert;
98-
mod data;
9998
mod error;
10099
mod macros;
101100
mod substitutions;
102101

103102
pub mod cmd;
103+
pub mod data;
104104
pub mod path;
105105
pub mod report;
106106
pub mod utils;
@@ -112,9 +112,6 @@ pub use action::Action;
112112
pub use action::DEFAULT_ACTION_ENV;
113113
pub use assert::Assert;
114114
pub use data::Data;
115-
pub use data::DataFormat;
116-
pub use data::DataSource;
117-
pub use data::{Normalize, NormalizeMatches, NormalizeNewlines, NormalizePaths};
118115
pub use error::Error;
119116
pub use snapbox_macros::debug;
120117
pub use substitutions::Substitutions;

crates/snapbox/src/path.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ impl PathDiff {
189189
crate::Data::read_from(&expected_path, None).normalize(NormalizeNewlines);
190190

191191
actual = actual
192-
.try_coerce(expected.format())
192+
.coerce_to(expected.format())
193193
.normalize(NormalizeNewlines);
194194

195195
if expected != actual {
@@ -266,7 +266,7 @@ impl PathDiff {
266266
crate::Data::read_from(&expected_path, None).normalize(NormalizeNewlines);
267267

268268
actual = actual
269-
.try_coerce(expected.format())
269+
.coerce_to(expected.format())
270270
.normalize(NormalizePaths)
271271
.normalize(NormalizeNewlines)
272272
.normalize(NormalizeMatches::new(substitutions, &expected));

src/runner.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use std::eprintln;
1212
use std::io::stderr;
1313

1414
use rayon::prelude::*;
15+
use snapbox::data::{DataFormat, NormalizeNewlines, NormalizePaths};
1516
use snapbox::path::FileType;
16-
use snapbox::{DataFormat, NormalizeNewlines, NormalizePaths};
1717

1818
#[derive(Debug)]
1919
pub(crate) struct Runner {
@@ -441,10 +441,12 @@ impl Case {
441441
}
442442

443443
if let Some(expected_content) = expected_content {
444-
stream.content = stream.content.normalize(snapbox::NormalizeMatches::new(
445-
substitutions,
446-
expected_content,
447-
));
444+
stream.content = stream
445+
.content
446+
.normalize(snapbox::data::NormalizeMatches::new(
447+
substitutions,
448+
expected_content,
449+
));
448450

449451
if stream.content != *expected_content {
450452
stream.status = StreamStatus::Expected(expected_content.clone());
@@ -734,7 +736,7 @@ struct Stream {
734736

735737
impl Stream {
736738
fn make_text(mut self) -> Self {
737-
let content = self.content.try_coerce(DataFormat::Text);
739+
let content = self.content.coerce_to(DataFormat::Text);
738740
if content.format() != DataFormat::Text {
739741
self.status = StreamStatus::Failure("Unable to convert underlying Data to Text".into());
740742
}

0 commit comments

Comments
 (0)