-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathschema.rs
More file actions
107 lines (88 loc) · 2.84 KB
/
schema.rs
File metadata and controls
107 lines (88 loc) · 2.84 KB
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
use super::error::ConfigError;
use serde::Deserialize;
use std::{collections::HashMap, fs};
// The schema of the config file
#[derive(Debug, Deserialize)]
pub struct ConfigFile {
/// Language setting: 'en' or 'zh'
#[serde(default)]
pub lang: String,
/// List of author names
/// Vec<String> is equivalent to string[] in TypeScript
pub authors: Vec<String>,
/// Date range: [start_date, end_date]
/// Vec<String> here represents a fixed-length array of 2 strings
#[serde(rename = "dateRange", default)]
pub date_range: Vec<String>,
/// List of Git repository paths
pub repos: Vec<String>,
/// Repository name formatting map
/// HashMap<K,V> is equivalent to Record<K,V> in TypeScript
/// or { [key: string]: string }
#[serde(default)]
pub format: HashMap<String, String>,
/// List of commit types to include
#[serde(default)]
pub includes: Vec<String>,
/// List of keywords to exclude
#[serde(default)]
pub excludes: Vec<String>,
}
impl ConfigFile {
pub fn from_file(path: &str) -> Result<Self, Box<dyn std::error::Error>> {
// Reading file contents
let content = fs::read_to_string(path)?;
// Parsing JSON
let mut config: ConfigFile = serde_json::from_str(&content)?;
// Allow missing fields
config.fill_defaults();
// Validate the config
config.validate()?;
Ok(config)
}
fn fill_defaults(&mut self) {
use chrono::Local;
// lang default en
if self.lang.is_empty() {
self.lang = "en".to_string();
}
// dateRange default today
if self.date_range.len() != 2 {
let today = Local::now().format("%Y-%m-%d").to_string();
self.date_range = vec![today.clone(), today];
}
// includes default the day the program is running
if self.includes.is_empty() {
self.includes = vec![
"feat".into(),
"fix".into(),
"docs".into(),
"style".into(),
"refactor".into(),
"test".into(),
"chore".into(),
];
}
// excludes default empty
if self.excludes.is_empty() {
self.excludes = vec![];
}
// format default empty
if self.format.is_empty() {
self.format = std::collections::HashMap::new();
}
}
fn validate(&self) -> Result<(), ConfigError> {
if self.authors.is_empty() {
return Err(ConfigError::InvalidConfig(
"authors list cannot be empty".to_string(),
));
}
if self.repos.is_empty() {
return Err(ConfigError::InvalidConfig(
"repos list cannot be empty".to_string(),
));
}
Ok(())
}
}