44 lines
923 B
Rust
44 lines
923 B
Rust
|
extern crate toml;
|
||
|
|
||
|
use std::fs::File;
|
||
|
use std::io::prelude::*;
|
||
|
|
||
|
#[derive(Debug, Deserialize, Clone)]
|
||
|
pub struct Config {
|
||
|
pub audio: AudioConfig,
|
||
|
pub output: OutputConfig,
|
||
|
}
|
||
|
|
||
|
#[derive(Debug, Deserialize, Clone)]
|
||
|
pub struct AudioConfig {
|
||
|
pub alsa_device: String,
|
||
|
pub rate: u32,
|
||
|
}
|
||
|
|
||
|
#[derive(Debug, Deserialize, Clone)]
|
||
|
pub struct OutputConfig {
|
||
|
pub directory: String,
|
||
|
pub prefix: String,
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
pub fn read_config(path: &str) -> Config {
|
||
|
|
||
|
let mut config_toml = String::new();
|
||
|
|
||
|
let mut file = match File::open(path) {
|
||
|
Ok(file) => file,
|
||
|
Err(_) => panic!("Could not find config file!"),
|
||
|
};
|
||
|
|
||
|
file.read_to_string(&mut config_toml)
|
||
|
.unwrap_or_else(|err| panic!("Error while reading config: [{}]", err));
|
||
|
|
||
|
let config: Config = toml::from_str(&config_toml).unwrap_or_else(|err| {
|
||
|
panic!("Error while parsing config: [{}]", err)
|
||
|
});
|
||
|
|
||
|
return config;
|
||
|
}
|