1use serde::Deserialize;
2use std::fs::File;
3use std::io::prelude::*;
4use std::path::{Path, PathBuf};
5use toml;
6
7use color_eyre::{eyre::eyre, Result};
8
9use platform_dirs::AppDirs;
10
11#[derive(Deserialize, Debug, Clone)]
12pub struct Config {
13 pub library: Vec<PathBuf>,
14 pub server_address: String,
15 pub server_port: usize,
16
17 pub database_dir: PathBuf,
18 pub database_port: usize,
19}
20
21impl Config {
22 pub fn new_from_file(path: &Path) -> Result<Config> {
23 let mut file = File::open(path)?;
24 let mut contents = String::new();
25 file.read_to_string(&mut contents)?;
26
27 let config: Config = toml::from_str(&contents)?;
28 Ok(config)
29 }
30}
31
32impl Default for Config {
33 fn default() -> Config {
34 Config {
35 library: vec![],
36 server_address: "127.0.0.1".to_string(),
37 server_port: 6603,
38
39 database_dir: AppDirs::new(Some("ouverture/postgres"), true)
40 .unwrap()
41 .data_dir,
42 database_port: 6604,
43 }
44 }
45}