mwkeep 0.0.1

A tool for house keeping MediaWiki sites.
//! Application configuration type

use std::{
    fs::File,
    io::{self, BufRead, BufReader},
    path::Path,
};

use serde::{Deserialize, Serialize};

use crate::deser;

/// Application configuration type
#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
pub struct Config {
    username: String,
    password: String,
    domains: Vec<String>,
    keep_alive_title: String,
    keep_alive_text: String,
    keep_alive_summary: String,
    keep_alive_days: u32,
    delete_title: String,
    keep_days: u32,
}

impl Config {
    pub fn username(&self) -> &str {
        &self.username
    }

    pub fn password(&self) -> &str {
        &self.password
    }

    pub fn domains(&self) -> &Vec<String> {
        &self.domains
    }

    pub fn keep_alive_title(&self) -> &str {
        &self.keep_alive_title
    }

    pub fn keep_alive_text(&self) -> &str {
        &self.keep_alive_text
    }

    pub fn keep_alive_summary(&self) -> &str {
        &self.keep_alive_summary
    }

    pub fn keep_alive_days(&self) -> u32 {
        self.keep_alive_days
    }

    pub fn delete_title(&self) -> &str {
        &self.delete_title
    }

    pub fn keep_days(&self) -> u32 {
        self.keep_days
    }

    pub fn load(path: &Path) -> Result<Config, deser::Error> {
        let f = File::open(path)?;
        let reader = BufReader::new(f);
        let lines = reader.lines().collect::<io::Result<Vec<String>>>()?;
        let text = lines.join("\n");
        Ok(toml::from_str(&text)?)
    }
}