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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
use std::fs::File;
use super::Result;
use std::io::prelude::*;
use serde_yaml::from_str;
use std::env::current_dir;
pub const CONFIG_FILE: &str = ".changelog.yml";
const CONFIG_DEFAULT: &str = include_str!("assets/changelog.yml");
pub const TEMPLATE_FILE: &str = ".changelog.hbs";
const TEMPLATE_DEFAULT: &str = include_str!("assets/changelog.hbs");
#[serde(default)]
#[derive(Debug, Default, Deserialize)]
pub struct Configuration {
pub conventions: Conventions,
pub output: OutputPreferences,
}
#[serde(default)]
#[derive(Debug, Default, Deserialize, Eq, PartialEq)]
pub struct Conventions {
pub scopes: Vec<Keyword>,
pub categories: Vec<Keyword>,
}
#[serde(default)]
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq)]
pub struct Keyword {
pub tag: String,
pub title: String,
}
#[serde(default)]
#[derive(Debug, Default, Deserialize, Eq, PartialEq)]
pub struct OutputPreferences {
pub json: bool,
pub template: Option<String>,
pub remote: Option<String>,
pub post_processors: Vec<PostProcessor>,
}
#[serde(default)]
#[derive(Debug, Default, Deserialize, Eq, PartialEq)]
pub struct PostProcessor {
pub lookup: String,
pub replace: String,
}
impl Configuration {
pub fn new() -> Self {
Self::from_file(None).unwrap_or_else(|_| Self::default())
}
pub fn from_file(file: Option<&str>) -> Result<Self> {
file.map(str::to_owned)
.or_else(|| find_file(CONFIG_FILE))
.map_or_else(|| Ok(String::from(CONFIG_DEFAULT)), |f| read_file(&f))
.and_then(|yml| Self::from_yaml(&yml))
}
pub fn from_yaml(yml: &str) -> Result<Self> {
from_str(yml).map_err(|e| format_err!("Configuration contains invalid YAML: {}", e))
}
}
impl Conventions {
pub fn scope_title(&self, scope: Option<String>) -> Option<&str> {
self.title(&self.scopes, scope)
}
pub fn category_title(&self, category: Option<String>) -> Option<&str> {
self.title(&self.categories, category)
}
pub fn category_titles(&self) -> Vec<&str> {
Self::titles(&self.categories)
}
pub fn scope_titles(&self) -> Vec<&str> {
Self::titles(&self.scopes)
}
fn title<'a>(&'a self, keywords: &'a [Keyword], tag: Option<String>) -> Option<&'a str> {
if keywords.is_empty() && tag.is_none() {
return Some("");
}
let given = tag.unwrap_or_default();
for kw in keywords {
if kw.tag == given {
return Some(&kw.title);
}
}
None
}
fn titles(keywords: &[Keyword]) -> Vec<&str> {
if keywords.is_empty() {
vec![""]
} else {
keywords.iter().map(|k| k.title.as_ref()).collect()
}
}
}
impl Keyword {
pub fn new<T: AsRef<str>>(tag: T, title: T) -> Self {
Keyword {
tag: tag.as_ref().to_owned(),
title: title.as_ref().to_owned(),
}
}
}
impl OutputPreferences {
pub fn new() -> Self {
Self::default()
}
pub fn get_template(&self) -> Result<String> {
self.template
.clone()
.or_else(|| find_file(TEMPLATE_FILE))
.map_or_else(|| Ok(String::from(TEMPLATE_DEFAULT)), |f| read_file(&f))
}
}
fn read_file(name: &str) -> Result<String> {
info!("Reading file '{}'", name);
let mut contents = String::new();
File::open(name)
.map_err(|e| format_err!("Cannot open file '{}' (Reason: {})", name, e))?
.read_to_string(&mut contents)
.map_err(|e| format_err!("Cannot read file '{}' (Reason: {})", name, e))?;
Ok(contents)
}
fn find_file(file: &str) -> Option<String> {
let mut cwd = current_dir().expect("Current directory is invalid");
while cwd.exists() {
cwd.push(file);
if cwd.is_file() {
return Some(cwd.to_string_lossy().to_string());
}
cwd.pop();
if cwd.parent().is_some() {
cwd.pop();
} else {
break;
}
}
None
}
#[cfg(test)]
mod tests {
use super::Configuration;
#[test]
fn configuration_from_yaml() {
let project = include_str!("../.changelog.yml");
let no_category = r#"
conventions:
scopes: [{keyword:"a", title: "A"}]
"#;
let no_scope = r#"
conventions:
categories: [{keyword:"a", title: "A"}]
"#;
assert!(Configuration::from_yaml("").is_err());
assert!(Configuration::from_yaml(project).is_ok());
assert!(Configuration::from_yaml(no_scope).is_ok());
assert!(Configuration::from_yaml(no_category).is_ok());
}
#[test]
fn find_file() {
use super::find_file;
assert!(find_file("unknown").is_none());
assert!(find_file("Cargo.toml").is_some());
}
}