1
- use std:: { fs:: File , io:: BufReader , path:: Path , time:: Instant } ;
1
+ use std:: { fs:: File , io:: Read , path:: Path , time:: Instant } ;
2
2
3
3
use anyhow:: { Context , Result , anyhow} ;
4
4
use clap:: Parser ;
@@ -173,15 +173,13 @@ impl ConfigOptions {
173
173
}
174
174
175
175
/// Reads the config_version field from a JSON config file and validates it against CONFIG_VERSION
176
- fn validate_config_version < P : AsRef < Path > > ( path : P ) -> Result < ( ) > {
176
+ fn validate_config_version ( json_str : & str ) -> Result < ( ) > {
177
177
#[ derive( Deserialize ) ]
178
178
struct VersionOnly {
179
179
config_version : String ,
180
180
}
181
181
182
- let file = File :: open ( & path) ?;
183
- let reader = BufReader :: new ( file) ;
184
- let version_info: VersionOnly = serde_json:: from_reader ( reader) ?;
182
+ let version_info: VersionOnly = serde_json:: from_str ( json_str) ?;
185
183
186
184
if version_info. config_version != CONFIG_VERSION {
187
185
return Err ( anyhow ! (
@@ -194,10 +192,11 @@ fn validate_config_version<P: AsRef<Path>>(path: P) -> Result<()> {
194
192
}
195
193
196
194
fn load_config < P : AsRef < Path > > ( path : P ) -> Result < ConfigOptions > {
197
- validate_config_version ( path. as_ref ( ) ) ?;
198
- let file = File :: open ( path) ?;
199
- let reader = BufReader :: new ( file) ;
200
- let config: ConfigOptions = serde_json:: from_reader ( reader) ?;
195
+ let mut file = File :: open ( path) ?;
196
+ let mut json_str = String :: new ( ) ;
197
+ file. read_to_string ( & mut json_str) ?;
198
+ validate_config_version ( & json_str) ?;
199
+ let config: ConfigOptions = serde_json:: from_str ( & json_str) ?;
201
200
Ok ( config)
202
201
}
203
202
0 commit comments