1
+ use anyhow:: Context ;
1
2
use serde:: { Deserialize , Serialize } ;
2
3
use std:: fs:: File ;
3
4
use std:: io:: Write ;
@@ -11,8 +12,18 @@ pub struct Config {
11
12
12
13
impl Config {
13
14
pub fn write_result < A : bytemuck:: NoUninit > ( & self , output : & [ A ] ) -> anyhow:: Result < ( ) > {
14
- let mut f = File :: create ( & self . output_path ) ?;
15
- f. write_all ( bytemuck:: cast_slice ( output) ) ?;
15
+ let mut f = File :: create ( & self . output_path ) . with_context ( || {
16
+ format ! (
17
+ "failed to create output file '{}'" ,
18
+ self . output_path. display( )
19
+ )
20
+ } ) ?;
21
+ f. write_all ( bytemuck:: cast_slice ( output) ) . with_context ( || {
22
+ format ! (
23
+ "failed to write to output file '{}'" ,
24
+ self . output_path. display( )
25
+ )
26
+ } ) ?;
16
27
Ok ( ( ) )
17
28
}
18
29
}
@@ -106,15 +117,28 @@ impl TestMetadata {
106
117
107
118
impl Config {
108
119
pub fn from_path < P : AsRef < Path > > ( path : P ) -> anyhow:: Result < Self > {
109
- let content = fs:: read_to_string ( path) ?;
110
- let config = serde_json:: from_str ( & content) ?;
120
+ let path = path. as_ref ( ) ;
121
+ let content = fs:: read_to_string ( path)
122
+ . with_context ( || format ! ( "Could not read file '{}'" , path. display( ) ) ) ?;
123
+ let config = serde_json:: from_str ( & content) . with_context ( || {
124
+ format ! (
125
+ "Could not parse json in file '{}':\n {content}" ,
126
+ path. display( )
127
+ )
128
+ } ) ?;
111
129
Ok ( config)
112
130
}
113
131
114
132
/// Write test metadata to the configured metadata path
115
133
pub fn write_metadata ( & self , metadata : & TestMetadata ) -> anyhow:: Result < ( ) > {
116
- let metadata_json = serde_json:: to_string ( metadata) ?;
117
- fs:: write ( & self . metadata_path , metadata_json) ?;
134
+ let metadata_json =
135
+ serde_json:: to_string ( metadata) . context ( "Could not serialize TestMetadata" ) ?;
136
+ fs:: write ( & self . metadata_path , metadata_json) . with_context ( || {
137
+ format ! (
138
+ "Could not write TestMetadata to file at '{}'" ,
139
+ self . metadata_path. display( )
140
+ )
141
+ } ) ?;
118
142
Ok ( ( ) )
119
143
}
120
144
}
0 commit comments