Add config reading

This commit is contained in:
Elias Schriefer 2021-06-15 14:45:38 +00:00
parent ac4ab4c132
commit e62f3ee2dc

View File

@ -1,5 +1,11 @@
use structopt::StructOpt;
use std::path::PathBuf;
use std::{
path::PathBuf,
fs::File,
io::prelude::*,
boxed::Box,
error::Error,
};
use serde::Deserialize;
const DEFAULT_DATABASE_PATH: &'static str = "/var/lib/noise-server/noise-server.sqlite";
@ -17,7 +23,6 @@ struct Options {
#[serde(default)]
struct Config {
database_path: PathBuf,
}
impl Default for Config {
@ -28,8 +33,31 @@ impl Default for Config {
}
}
fn main() {
let options = Options::from_args();
fn main() -> Result<(), Box<dyn Error>> {
let clap_matches = Options::clap().get_matches();
let options = Options::from_clap(&clap_matches);
let mut config = get_config(&options)?;
clap_matches.value_of("database_path")
.map(|x| config.database_path = x.into());
#[cfg(debug_assertions)]
println!("{:?}", options);
Ok(())
}
///
fn get_config(options: &Options) -> std::io::Result<Config> {
let mut config_file = File::open(&options.config)?;
let mut config_string = String::new();
config_file.read_to_string(&mut config_string)?;
toml::from_str(&config_string)
.map_err(|err| std::io::Error::new(
std::io::ErrorKind::InvalidData,
err
)
)
}