@@ -6,6 +6,7 @@ use std::{collections::HashMap, fs};
6
6
#[ derive( Debug , Deserialize ) ]
7
7
pub struct ConfigFile {
8
8
/// Language setting: 'en' or 'zh'
9
+ #[ serde( default ) ]
9
10
pub lang : String ,
10
11
11
12
/// List of author names
@@ -14,7 +15,7 @@ pub struct ConfigFile {
14
15
15
16
/// Date range: [start_date, end_date]
16
17
/// Vec<String> here represents a fixed-length array of 2 strings
17
- #[ serde( rename = "dateRange" ) ]
18
+ #[ serde( rename = "dateRange" , default ) ]
18
19
pub date_range : Vec < String > ,
19
20
20
21
/// List of Git repository paths
@@ -23,12 +24,15 @@ pub struct ConfigFile {
23
24
/// Repository name formatting map
24
25
/// HashMap<K,V> is equivalent to Record<K,V> in TypeScript
25
26
/// or { [key: string]: string }
27
+ #[ serde( default ) ]
26
28
pub format : HashMap < String , String > ,
27
29
28
30
/// List of commit types to include
31
+ #[ serde( default ) ]
29
32
pub includes : Vec < String > ,
30
33
31
34
/// List of keywords to exclude
35
+ #[ serde( default ) ]
32
36
pub excludes : Vec < String > ,
33
37
}
34
38
@@ -38,20 +42,56 @@ impl ConfigFile {
38
42
let content = fs:: read_to_string ( path) ?;
39
43
40
44
// 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 ( ) ;
42
49
43
50
// Validate the config
44
51
config. validate ( ) ?;
45
52
46
53
Ok ( config)
47
54
}
48
55
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
50
65
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 ( ) ;
54
91
}
92
+ }
93
+
94
+ fn validate ( & self ) -> Result < ( ) , ConfigError > {
55
95
if self . authors . is_empty ( ) {
56
96
return Err ( ConfigError :: InvalidConfig (
57
97
"authors list cannot be empty" . to_string ( ) ,
0 commit comments