Skip to content

Commit fafd687

Browse files
committed
feat(snap): Simplify DataSource creation
1 parent febb9f6 commit fafd687

File tree

2 files changed

+37
-34
lines changed

2 files changed

+37
-34
lines changed

crates/snapbox/src/data/mod.rs

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -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) {
@@ -223,10 +215,7 @@ impl Data {
223215
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")]
@@ -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 {

0 commit comments

Comments
 (0)