discord-proxy 0.1.0

Windows-first Discord process-local proxy launcher
Documentation
use anyhow::{Context, Result};
use serde::Deserialize;
use std::{
    fs,
    path::{Path, PathBuf},
};

const DEFAULT_CONFIG: &str = "discord-proxy.toml";

#[derive(Debug, Default, Deserialize)]
pub struct FileConfig {
    pub proxy: Option<String>,
    pub channel: Option<String>,
    pub discord_dir: Option<PathBuf>,
}

pub fn load(path: Option<&Path>) -> Result<FileConfig> {
    let path = match path {
        Some(path) => path.to_path_buf(),
        None => PathBuf::from(DEFAULT_CONFIG),
    };

    if !path.exists() {
        if path.file_name().and_then(|name| name.to_str()) == Some(DEFAULT_CONFIG) {
            return Ok(FileConfig::default());
        }
        anyhow::bail!("config file does not exist: {}", path.display());
    }

    let content = fs::read_to_string(&path)
        .with_context(|| format!("failed to read config file {}", path.display()))?;
    toml::from_str(&content)
        .with_context(|| format!("failed to parse config file {}", path.display()))
}