Skip to content

Commit

Permalink
conf: Move Toml to its own module
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentdephily committed Jan 24, 2024
1 parent 3824cf1 commit 15d2de4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 46 deletions.
52 changes: 6 additions & 46 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,52 +1,11 @@
use crate::*;
use anyhow::{Context, Error};
mod toml;

Check warning on line 1 in src/config.rs

View workflow job for this annotation

GitHub Actions / Rustfmt

Diff in /home/runner/work/emlop/emlop/src/config.rs

use crate::{*, config::toml::Toml};
use anyhow::{Error};
use clap::{error::{ContextKind, ContextValue, Error as ClapError, ErrorKind},
ArgMatches};
use serde::Deserialize;
use std::{env::var, fs::File, io::Read};
use std::{env::var};

#[derive(Deserialize, Debug)]
struct TomlLog {
starttime: Option<bool>,
}
#[derive(Deserialize, Debug)]
struct TomlPred {
average: Option<String>,
}
#[derive(Deserialize, Debug)]
struct TomlStats {
average: Option<String>,
}
#[derive(Deserialize, Debug)]
struct TomlAccuracy {
average: Option<String>,
}
#[derive(Deserialize, Debug, Default)]
pub struct Toml {
date: Option<String>,
log: Option<TomlLog>,
predict: Option<TomlPred>,
stats: Option<TomlStats>,
accuracy: Option<TomlAccuracy>,
}
impl Toml {
fn load(arg: Option<&String>, env: Option<String>) -> Result<Self, Error> {
match arg.or(env.as_ref()) {
Some(s) if s.is_empty() => Ok(Self::default()),
Some(s) => Self::doload(s.as_str()),
_ => Self::doload(&format!("{}/.config/emlop.toml",
var("HOME").unwrap_or("".to_string()))),
}
}
fn doload(name: &str) -> Result<Self, Error> {
log::trace!("Loading config {name:?}");
let mut f = File::open(name).with_context(|| format!("Cannot open {name:?}"))?;
let mut buf = String::new();
// TODO Streaming read
f.read_to_string(&mut buf).with_context(|| format!("Cannot read {name:?}"))?;
toml::from_str(&buf).with_context(|| format!("Cannot parse {name:?}"))
}
}

pub fn err(val: String, src: &'static str, possible: &'static str) -> ClapError {
let mut err = clap::Error::new(ErrorKind::InvalidValue);
Expand Down Expand Up @@ -104,6 +63,7 @@ pub enum Configs {
Accuracy(ArgMatches, Conf, ConfAccuracy),
Complete(ArgMatches),
}

/// Global config
///
/// Colors use `prefix/suffix()` instead of `paint()` because `paint()` doesn't handle `'{:>9}'`
Expand Down
46 changes: 46 additions & 0 deletions src/config/toml.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use serde::Deserialize;

Check warning on line 1 in src/config/toml.rs

View workflow job for this annotation

GitHub Actions / Rustfmt

Diff in /home/runner/work/emlop/emlop/src/config/toml.rs
use anyhow::{Context, Error};
use std::{env::var, fs::File, io::Read};

#[derive(Deserialize, Debug)]
pub struct TomlLog {
pub starttime: Option<bool>,
}
#[derive(Deserialize, Debug)]
pub struct TomlPred {
pub average: Option<String>,
}
#[derive(Deserialize, Debug)]
pub struct TomlStats {
pub average: Option<String>,
}
#[derive(Deserialize, Debug)]
pub struct TomlAccuracy {
pub average: Option<String>,
}
#[derive(Deserialize, Debug, Default)]
pub struct Toml {
pub date: Option<String>,
pub log: Option<TomlLog>,
pub predict: Option<TomlPred>,
pub stats: Option<TomlStats>,
pub accuracy: Option<TomlAccuracy>,
}
impl Toml {
pub fn load(arg: Option<&String>, env: Option<String>) -> Result<Self, Error> {
match arg.or(env.as_ref()) {
Some(s) if s.is_empty() => Ok(Self::default()),
Some(s) => Self::doload(s.as_str()),
_ => Self::doload(&format!("{}/.config/emlop.toml",
var("HOME").unwrap_or("".to_string()))),
}
}
fn doload(name: &str) -> Result<Self, Error> {
log::trace!("Loading config {name:?}");
let mut f = File::open(name).with_context(|| format!("Cannot open {name:?}"))?;
let mut buf = String::new();
// TODO Streaming read
f.read_to_string(&mut buf).with_context(|| format!("Cannot read {name:?}"))?;
toml::from_str(&buf).with_context(|| format!("Cannot parse {name:?}"))
}
}

0 comments on commit 15d2de4

Please sign in to comment.