commitlint_rs/
config.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
use serde::{Deserialize, Serialize};
use std::fmt;
use std::{fs, path::PathBuf};

use crate::rule::Rules;

/// Default Root config file path to search for.
const DEFAULT_CONFIG_ROOT: &str = ".";

/// Default commitlintrc configuration files
/// If the user didn't specify a configuration file with -c or --config argument,
/// we will try to find one of these files in the current directory.
const DEFAULT_CONFIG_FILE: [&str; 4] = [
    ".commitlintrc",
    ".commitlintrc.json",
    ".commitlintrc.yaml",
    ".commitlintrc.yml",
];

/// Config represents the configuration of commitlint.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct Config {
    /// Rules represents the rules of commitlint.
    pub rules: Rules,
}

impl fmt::Display for Config {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let s = serde_yaml::to_string(&self).unwrap();
        write!(f, "{}", s)
    }
}

/// Load configuration from the specified path.
pub async fn load(path: Option<PathBuf>) -> Result<Config, String> {
    let config_file = match &path {
        Some(p) => Some(p.clone()),
        None => find_config_file(PathBuf::from(DEFAULT_CONFIG_ROOT)),
    };

    match (config_file, path) {
        // If the file was specified and found, load it.
        (Some(p), _) => load_config_file(p).await,
        // If the file was not specified and not found, return default config.
        (None, None) => Ok(Config::default()),
        // If the was explicitly specified but not found, return an error.
        (None, Some(p)) => Err(format!("Configuration file not found in {}", p.display())),
    }
}

/// Find configuration file in the specified path.
/// Note that the first file found will be returned.
pub fn find_config_file(path: PathBuf) -> Option<PathBuf> {
    let mut path = path;
    for file in DEFAULT_CONFIG_FILE.iter() {
        path.push(file);
        if path.exists() {
            return Some(path);
        }
        path.pop();
    }

    None
}

/// Load config file from the specified path.
pub async fn load_config_file(path: PathBuf) -> Result<Config, String> {
    if !path.exists() {
        return Err(format!(
            "Configuration file not found in {}",
            path.display()
        ));
    }

    match path.extension() {
        Some(ext) => match ext.to_str() {
            Some("json") => load_json_config_file(path).await,
            Some("yaml") | Some("yml") => load_yaml_config_file(path).await,
            _ => load_unknown_config_file(path).await,
        },
        None => Err(format!(
            "Unsupported configuration file format: {}",
            path.display()
        )),
    }
}

/// Load JSON config file from the specified path.
async fn load_json_config_file(path: PathBuf) -> Result<Config, String> {
    let text = fs::read_to_string(path).unwrap();

    match serde_json::from_str::<Config>(&text) {
        Ok(config) => Ok(config),
        Err(err) => Err(format!("Failed to parse configuration file: {}", err)),
    }
}

/// Load YAML config file from the specified path.
async fn load_yaml_config_file(path: PathBuf) -> Result<Config, String> {
    let text = fs::read_to_string(path).unwrap();

    match serde_yaml::from_str::<Config>(&text) {
        Ok(config) => Ok(config),
        Err(err) => Err(format!("Failed to parse configuration file: {}", err)),
    }
}

/// Try to load configuration file from the specified path.
/// First try to load it as JSON, then as YAML.
/// If both fail, return an error.
async fn load_unknown_config_file(path: PathBuf) -> Result<Config, String> {
    let text = fs::read_to_string(path.clone()).unwrap();

    if let Ok(config) = serde_json::from_str::<Config>(&text) {
        return Ok(config);
    }

    if let Ok(config) = serde_yaml::from_str::<Config>(&text) {
        return Ok(config);
    }

    Err(format!(
        "Failed to parse configuration file: {}",
        path.display()
    ))
}