//! Loads configuration information from a TOML file in the current working directory.
//! Currently doesn't do much but this may be expanded.

use std::path::Path;

use serde::Deserialize;

#[derive(Deserialize, Default)]
pub struct Config {
    pub misc: MiscTable,
}

/// For config options where you aren't sure what table it should go under.
#[derive(Deserialize, Default)]
pub struct MiscTable {
    /// Whether or not we can enable Discord RPC for fun.
    #[cfg(feature = "discord-rpc")]
    pub use_discord_rpc: bool,
}

impl Config {
    pub fn load(path: &Path) -> Result<Self, toml::de::Error> {
        let file_contents = match std::fs::read_to_string(path) {
            Ok(file_contents) => file_contents,
            Err(why) => {
                eprintln!(
                    "WARN: Expected to read config file from '{}' with error '{}'. Using default settings.",
                    path.display(),
                    why
                );

                return Ok(Self::default());
            }
        };

        Self::deserialize(toml::Deserializer::new(&file_contents))
    }
}
