commitfmt 0.0.1

A git commit message formatter
Documentation
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::Path;

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct Config {
    pub subject_max_length: usize,
    pub body_max_line_length: usize,
}

impl Default for Config {
    fn default() -> Self {
        Config {
            subject_max_length: 50,
            body_max_line_length: 72,
        }
    }
}

impl Config {
    pub fn load_from_file<P: AsRef<Path>>(path: P) -> Result<Self, Box<dyn std::error::Error>> {
        let contents = fs::read_to_string(path)?;
        let config: Config = toml::from_str(&contents)?;
        Ok(config)
    }

    pub fn discover() -> Self {
        let current_dir = std::env::current_dir().ok();

        if let Some(dir) = current_dir {
            let config_path = dir.join(".commitfmt.toml");
            if config_path.exists() {
                if let Ok(config) = Config::load_from_file(config_path) {
                    return config;
                }
            }
        }

        Config::default()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_default_config() {
        let config = Config::default();
        assert_eq!(config.subject_max_length, 50);
        assert_eq!(config.body_max_line_length, 72);
    }
}