Skip to content
Snippets Groups Projects
config.rs 827 B
Newer Older
Stephen D's avatar
Stephen D committed
use std::fs;

use anyhow::anyhow;
use serde::Deserialize;

Stephen D's avatar
Stephen D committed
#[derive(Deserialize)]
pub struct Logo {
    pub path: String,
    pub width: String,
    pub height: String,
    pub alt: String,
}

Stephen D's avatar
Stephen D committed
#[derive(Deserialize)]
pub struct Config {
    pub name: String,
    pub description: String,
    pub path: String,
Stephen D's avatar
Stephen D committed
    pub logo: Option<Logo>,
Stephen D's avatar
Stephen D committed
}

impl Config {
    pub fn load() -> anyhow::Result<Self> {
        let file_contents = read_conf_file()?;
        let config = toml::from_str(&file_contents)?;

        Ok(config)
    }
}

fn read_conf_file() -> anyhow::Result<String> {
    let filenames = ["config.toml", "/etc/org_flux/config.toml"];
    for filename in filenames.iter() {
        if let Ok(x) = fs::read_to_string(filename) {
            return Ok(x);
        }
    }

    Err(anyhow!("Could not open config file"))
}