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
use crate::error::Result;
use regex::Regex;
#[derive(Debug, Clone, serde_derive::Serialize, serde_derive::Deserialize)]
pub struct Config {
pub changelog: ChangelogConfig,
pub git: GitConfig,
}
#[derive(Debug, Clone, serde_derive::Serialize, serde_derive::Deserialize)]
pub struct ChangelogConfig {
pub header: Option<String>,
pub body: String,
pub footer: Option<String>,
pub trim: Option<bool>,
}
#[derive(Debug, Clone, serde_derive::Serialize, serde_derive::Deserialize)]
pub struct GitConfig {
pub conventional_commits: bool,
pub commit_parsers: Option<Vec<CommitParser>>,
pub filter_commits: Option<bool>,
pub tag_pattern: Option<String>,
#[serde(with = "serde_regex", default)]
pub skip_tags: Option<Regex>,
}
#[derive(Debug, Clone, serde_derive::Serialize, serde_derive::Deserialize)]
pub struct CommitParser {
#[serde(with = "serde_regex", default)]
pub message: Option<Regex>,
#[serde(with = "serde_regex", default)]
pub body: Option<Regex>,
pub group: Option<String>,
pub default_scope: Option<String>,
pub skip: Option<bool>,
}
impl Config {
pub fn parse(file_name: String) -> Result<Config> {
let mut config = config::Config::default();
config
.merge(config::File::with_name(&file_name))?
.merge(config::Environment::with_prefix("CLIFF").separator("_"))?;
Ok(config.try_into()?)
}
}
#[cfg(test)]
mod test {
use super::*;
use pretty_assertions::assert_eq;
use std::env;
use std::path::PathBuf;
#[test]
fn parse_config() -> Result<()> {
let file_name = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.parent()
.unwrap()
.to_path_buf()
.join("config")
.join(crate::DEFAULT_CONFIG)
.to_str()
.unwrap()
.to_string();
env::set_var("CLIFF_CHANGELOG_FOOTER", "test");
let config = Config::parse(file_name)?;
assert_eq!("test", config.changelog.footer.unwrap());
Ok(())
}
}