Skip to content

Commit 3169901

Browse files
author
Vlad Volodkin
committed
Read config once
Signed-off-by: Vlad Volodkin <[email protected]>
1 parent 13495fa commit 3169901

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

mountpoint-s3-fs/examples/mount_from_config.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{fs::File, io::BufReader, path::Path, time::Instant};
1+
use std::{fs::File, io::Read, path::Path, time::Instant};
22

33
use anyhow::{Context, Result, anyhow};
44
use clap::Parser;
@@ -173,15 +173,13 @@ impl ConfigOptions {
173173
}
174174

175175
/// 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<()> {
177177
#[derive(Deserialize)]
178178
struct VersionOnly {
179179
config_version: String,
180180
}
181181

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)?;
185183

186184
if version_info.config_version != CONFIG_VERSION {
187185
return Err(anyhow!(
@@ -194,10 +192,11 @@ fn validate_config_version<P: AsRef<Path>>(path: P) -> Result<()> {
194192
}
195193

196194
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)?;
201200
Ok(config)
202201
}
203202

0 commit comments

Comments
 (0)