Skip to content

Commit 30fac51

Browse files
committed
fix(config): allow optional config fields with default values
- Support missing or empty fields in config.json such as format, includes, excludes, dateRange, and lang - Program will automatically fill reasonable defaults for these fields - Only authors and repos are required, others are now optional
1 parent e2f2ebd commit 30fac51

File tree

2 files changed

+51
-9
lines changed

2 files changed

+51
-9
lines changed

src/config/schema.rs

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::{collections::HashMap, fs};
66
#[derive(Debug, Deserialize)]
77
pub struct ConfigFile {
88
/// Language setting: 'en' or 'zh'
9+
#[serde(default)]
910
pub lang: String,
1011

1112
/// List of author names
@@ -14,7 +15,7 @@ pub struct ConfigFile {
1415

1516
/// Date range: [start_date, end_date]
1617
/// Vec<String> here represents a fixed-length array of 2 strings
17-
#[serde(rename = "dateRange")]
18+
#[serde(rename = "dateRange", default)]
1819
pub date_range: Vec<String>,
1920

2021
/// List of Git repository paths
@@ -23,12 +24,15 @@ pub struct ConfigFile {
2324
/// Repository name formatting map
2425
/// HashMap<K,V> is equivalent to Record<K,V> in TypeScript
2526
/// or { [key: string]: string }
27+
#[serde(default)]
2628
pub format: HashMap<String, String>,
2729

2830
/// List of commit types to include
31+
#[serde(default)]
2932
pub includes: Vec<String>,
3033

3134
/// List of keywords to exclude
35+
#[serde(default)]
3236
pub excludes: Vec<String>,
3337
}
3438

@@ -38,20 +42,56 @@ impl ConfigFile {
3842
let content = fs::read_to_string(path)?;
3943

4044
// Parsing JSON
41-
let config: ConfigFile = serde_json::from_str(&content)?;
45+
let mut config: ConfigFile = serde_json::from_str(&content)?;
46+
47+
// Allow missing fields
48+
config.fill_defaults();
4249

4350
// Validate the config
4451
config.validate()?;
4552

4653
Ok(config)
4754
}
4855

49-
fn validate(&self) -> Result<(), ConfigError> {
56+
fn fill_defaults(&mut self) {
57+
use chrono::Local;
58+
59+
// lang default en
60+
if self.lang.is_empty() {
61+
self.lang = "en".to_string();
62+
}
63+
64+
// dateRange default today
5065
if self.date_range.len() != 2 {
51-
return Err(ConfigError::InvalidConfig(
52-
"date_range must contain exactly 2 dates".to_string(),
53-
));
66+
let today = Local::now().format("%Y-%m-%d").to_string();
67+
self.date_range = vec![today.clone(), today];
68+
}
69+
70+
// includes default the day the program is running
71+
if self.includes.is_empty() {
72+
self.includes = vec![
73+
"feat".into(),
74+
"fix".into(),
75+
"docs".into(),
76+
"style".into(),
77+
"refactor".into(),
78+
"test".into(),
79+
"chore".into(),
80+
];
81+
}
82+
83+
// excludes default empty
84+
if self.excludes.is_empty() {
85+
self.excludes = vec![];
86+
}
87+
88+
// format default empty
89+
if self.format.is_empty() {
90+
self.format = std::collections::HashMap::new();
5491
}
92+
}
93+
94+
fn validate(&self) -> Result<(), ConfigError> {
5595
if self.authors.is_empty() {
5696
return Err(ConfigError::InvalidConfig(
5797
"authors list cannot be empty".to_string(),

src/config/types.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,11 @@ impl fmt::Display for Config {
9797
for inc in &self.includes {
9898
writeln!(f, " - {}", inc)?;
9999
}
100-
writeln!(f, "{}:", t("excludes"))?;
101-
for exc in &self.excludes {
102-
writeln!(f, " - {}", exc)?;
100+
if !self.excludes.is_empty() {
101+
writeln!(f, "{}:", t("excludes"))?;
102+
for exc in &self.excludes {
103+
writeln!(f, " - {}", exc)?;
104+
}
103105
}
104106
writeln!(f, "")?;
105107
writeln!(f, "--------------------------------")?;

0 commit comments

Comments
 (0)