1- use anyhow:: { Result , anyhow } ;
1+ use anyhow:: Result ;
22use serde:: { Deserialize , Serialize } ;
33use std:: fs;
44use std:: path:: Path ;
@@ -16,37 +16,39 @@ pub struct AIConfig {
1616
1717/// 应用配置
1818#[ derive( Debug , Clone , Serialize , Deserialize ) ]
19+ #[ serde( default ) ]
1920pub struct AppConfig {
2021 /// AI 配置
2122 pub ai : AIConfig ,
2223 /// 默认列表
2324 pub default_list : String ,
2425 /// 默认提醒时间(分钟)
2526 pub default_reminder_minutes : Vec < i32 > ,
26- /// 是否使用 AI 解析(默认 true)
27- #[ serde( default = "default_true" ) ]
27+ /// 是否使用 AI 解析
2828 pub use_ai : bool ,
2929}
3030
31- fn default_true ( ) -> bool {
32- true
33- }
34-
3531impl Default for AppConfig {
3632 fn default ( ) -> Self {
3733 Self {
38- ai : AIConfig {
39- api_url : "https://api.openai.com/v1/chat/completions" . to_string ( ) ,
40- api_key : "" . to_string ( ) ,
41- model : "gpt-4-turbo-preview" . to_string ( ) ,
42- } ,
34+ ai : AIConfig :: default ( ) ,
4335 default_list : "提醒事项" . to_string ( ) ,
44- default_reminder_minutes : vec ! [ 15 ] ,
36+ default_reminder_minutes : vec ! [ 0 ] ,
4537 use_ai : false ,
4638 }
4739 }
4840}
4941
42+ impl Default for AIConfig {
43+ fn default ( ) -> Self {
44+ Self {
45+ api_url : "" . to_string ( ) ,
46+ api_key : "" . to_string ( ) ,
47+ model : "" . to_string ( )
48+ }
49+ }
50+ }
51+
5052/// 配置管理器
5153#[ derive( Clone ) ]
5254pub struct ConfigManager {
@@ -56,9 +58,7 @@ pub struct ConfigManager {
5658impl ConfigManager {
5759 /// 创建新的配置管理器
5860 pub fn new ( ) -> Result < Self > {
59- let config_dir = dirs:: config_dir ( )
60- . ok_or_else ( || anyhow ! ( "无法获取配置目录" ) ) ?
61- . join ( "reminders" ) ;
61+ let config_dir = dirs:: home_dir ( ) . unwrap ( ) . join ( ".config/reminders" ) ;
6262
6363 if !config_dir. exists ( ) {
6464 fs:: create_dir_all ( & config_dir) ?;
@@ -119,7 +119,7 @@ mod tests {
119119 fn test_default_config ( ) {
120120 let config = AppConfig :: default ( ) ;
121121 assert_eq ! ( config. default_list, "提醒事项" ) ;
122- assert_eq ! ( config. default_reminder_minutes, vec![ 15 ] ) ;
122+ assert_eq ! ( config. default_reminder_minutes, vec![ 0 ] ) ;
123123 assert ! ( !config. use_ai) ;
124124 }
125125}
0 commit comments