Load config from file
This commit is contained in:
parent
6f95d58e11
commit
4f7d4f3204
5 changed files with 144 additions and 7 deletions
23
src/config.rs
Normal file
23
src/config.rs
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
//! Configuration from a file.
|
||||
|
||||
use std::{fs, io::ErrorKind, path::Path};
|
||||
|
||||
use serde::Deserialize;
|
||||
use tracing::info;
|
||||
|
||||
#[derive(Debug, Default, Deserialize)]
|
||||
pub struct Config {}
|
||||
|
||||
impl Config {
|
||||
pub fn load(path: &Path) -> anyhow::Result<Self> {
|
||||
info!("Loading config from {}", path.display());
|
||||
Ok(match fs::read_to_string(path) {
|
||||
Ok(str) => toml::from_str(&str)?,
|
||||
Err(e) if e.kind() == ErrorKind::NotFound => {
|
||||
info!("No config file found, using default config");
|
||||
Self::default()
|
||||
}
|
||||
Err(e) => Err(e)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue