all the tidepool config file to be JSON

This commit is contained in:
tali 2023-04-16 20:24:44 -04:00
parent c8c2573e30
commit 75f2eafc4c
1 changed files with 24 additions and 4 deletions

View File

@ -61,7 +61,7 @@ fn create_mailbox() -> (mpsc::SyncSender<Msg>, mpsc::Receiver<Msg>) {
} }
fn single(args: SingleRun) -> Result<()> { fn single(args: SingleRun) -> Result<()> {
let config = parse_config_file(&args.config_file)?; let config = read_config_file(&args.config_file)?;
let (tx, rx) = create_mailbox(); let (tx, rx) = create_mailbox();
let exit_early = Arc::new(atomic::AtomicBool::new(false)); let exit_early = Arc::new(atomic::AtomicBool::new(false));
@ -114,7 +114,7 @@ fn single(args: SingleRun) -> Result<()> {
} }
fn multi(args: MultiRun) -> Result<()> { fn multi(args: MultiRun) -> Result<()> {
let config = parse_config_file(&args.config_file)?; let config = read_config_file(&args.config_file)?;
let (tx, rx) = create_mailbox(); let (tx, rx) = create_mailbox();
let tasks = args.count.map_or(i64::MAX, |n| n as i64); let tasks = args.count.map_or(i64::MAX, |n| n as i64);
@ -182,13 +182,33 @@ fn multi(args: MultiRun) -> Result<()> {
Ok(()) Ok(())
} }
fn parse_config_file(path: &Path) -> Result<Config> { fn read_config_file(path: &Path) -> Result<Config> {
enum Format {
Json,
Toml,
}
let extension = path.extension().and_then(|e| e.to_str());
let format = match extension {
Some(e) if e.eq_ignore_ascii_case("json") => Format::Json,
Some(e) if e.eq_ignore_ascii_case("toml") => Format::Toml,
_ => {
return Err(anyhow::anyhow!(
"invalid config file extension, supports .toml or .json"
));
}
};
let mut contents = String::new(); let mut contents = String::new();
std::fs::File::open(path) std::fs::File::open(path)
.with_context(|| format!("error opening config file '{}'", path.display()))? .with_context(|| format!("error opening config file '{}'", path.display()))?
.read_to_string(&mut contents) .read_to_string(&mut contents)
.context("error reading config file")?; .context("error reading config file")?;
toml::from_str(&contents).context("error parsing config file")
match format {
Format::Json => serde_json::from_str(&contents).context("error parsing config file"),
Format::Toml => toml::from_str(&contents).context("error parsing config file"),
}
} }
fn write_output(io_args: &IoArgs, path: Option<&Path>, output: &Output) -> Result<()> { fn write_output(io_args: &IoArgs, path: Option<&Path>, output: &Output) -> Result<()> {