@@ -74,45 +74,37 @@ enum DataInner {
74
74
impl Data {
75
75
/// Mark the data as binary (no post-processing)
76
76
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 ( )
81
78
}
82
79
83
80
/// Mark the data as text (post-processing)
84
81
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 ( )
89
83
}
90
84
91
85
#[ cfg( feature = "json" ) ]
92
86
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 ( )
97
88
}
98
89
99
90
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 ( )
104
92
}
105
93
106
94
/// Empty test data
107
95
pub fn new ( ) -> Self {
108
96
Self :: text ( "" )
109
97
}
110
98
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 ( ) ) ;
113
101
self
114
102
}
115
103
104
+ fn with_path ( self , path : impl Into < std:: path:: PathBuf > ) -> Self {
105
+ self . with_source ( path. into ( ) )
106
+ }
107
+
116
108
/// Load test data from a file
117
109
pub fn read_from ( path : & std:: path:: Path , data_format : Option < DataFormat > ) -> Self {
118
110
match Self :: try_read_from ( path, data_format) {
@@ -223,10 +215,7 @@ impl Data {
223
215
pub fn coerce_to ( self , format : DataFormat ) -> Self {
224
216
let mut data = match ( self . inner , format) {
225
217
( DataInner :: Error ( inner) , _) => Self :: error ( inner) ,
226
- ( inner, DataFormat :: Error ) => Self {
227
- inner,
228
- source : None ,
229
- } ,
218
+ ( inner, DataFormat :: Error ) => inner. into ( ) ,
230
219
( DataInner :: Binary ( inner) , DataFormat :: Binary ) => Self :: binary ( inner) ,
231
220
( DataInner :: Text ( inner) , DataFormat :: Text ) => Self :: text ( inner) ,
232
221
#[ cfg( feature = "json" ) ]
@@ -260,21 +249,14 @@ impl Data {
260
249
Err ( _) => Self :: text ( inner) ,
261
250
}
262
251
}
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
+ }
271
256
// This variant is already covered unless structured data is enabled
272
257
#[ cfg( feature = "structured-data" ) ]
273
258
( inner, DataFormat :: Text ) => {
274
- let remake = Self {
275
- inner,
276
- source : None ,
277
- } ;
259
+ let remake: Self = inner. into ( ) ;
278
260
if let Some ( str) = remake. render ( ) {
279
261
Self :: text ( str)
280
262
} else {
@@ -298,6 +280,15 @@ impl Data {
298
280
}
299
281
}
300
282
283
+ impl From < DataInner > for Data {
284
+ fn from ( inner : DataInner ) -> Self {
285
+ Data {
286
+ inner,
287
+ source : None ,
288
+ }
289
+ }
290
+ }
291
+
301
292
impl std:: fmt:: Display for Data {
302
293
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
303
294
match & self . inner {
0 commit comments