@@ -10,12 +10,14 @@ pub struct Config {
1010 pub default_file : Option < String > ,
1111 pub print_title : Option < bool > ,
1212 pub use_cache : Option < bool > ,
13+ #[ serde( default ) ]
14+ pub fortune_files : Vec < String > ,
1315}
1416
1517pub ( crate ) fn app_dir ( ) -> PathBuf {
1618 let mut base = data_dir ( ) . unwrap_or_else ( || {
1719 // fallback molto conservativo
18- let mut p = std :: env:: current_dir ( ) . unwrap_or_else ( |_| PathBuf :: from ( "." ) ) ;
20+ let mut p = env:: current_dir ( ) . unwrap_or_else ( |_| PathBuf :: from ( "." ) ) ;
1921 p. push ( ".rfortune" ) ;
2022 p
2123 } ) ;
@@ -60,6 +62,7 @@ pub fn init_config_file() -> std::io::Result<()> {
6062 default_file : Some ( get_default_path ( ) . to_string_lossy ( ) . to_string ( ) ) ,
6163 print_title : Some ( true ) ,
6264 use_cache : Some ( true ) ,
65+ fortune_files : vec ! [ ] ,
6366 } ;
6467 let yaml = serde_yaml:: to_string ( & cfg) . expect ( "Failed to serialize config" ) ;
6568 fs:: write ( path, yaml) ?;
@@ -92,8 +95,19 @@ In Rust we trust.
9295/// Carica la configurazione se presente
9396pub fn load_config ( ) -> Option < Config > {
9497 let path = get_config_path ( ) ;
95- let content = fs:: read_to_string ( path) . ok ( ) ?;
96- serde_yaml:: from_str ( & content) . ok ( )
98+ let content = fs:: read_to_string ( & path) . ok ( ) ?;
99+ let mut cfg: Config = serde_yaml:: from_str ( & content) . ok ( ) ?;
100+
101+ // ✅ MIGRATION AUTOMATICA
102+ if cfg. fortune_files . is_empty ( )
103+ && let Some ( df) = & cfg. default_file
104+ {
105+ cfg. fortune_files = vec ! [ df. clone( ) ] ;
106+ }
107+
108+ let _ = cfg. save ( ) ; // ignoriamo eventuali errori non critici
109+
110+ Some ( cfg)
97111}
98112
99113/// Tenta di migrare una vecchia configurazione `config.yaml` a `rfortune.conf`
@@ -190,3 +204,21 @@ pub fn run_config_edit(editor_arg: Option<String>) -> std::io::Result<()> {
190204
191205 Ok ( ( ) )
192206}
207+
208+ impl Config {
209+ /// Salva la configurazione corrente su disco (YAML).
210+ pub fn save ( & self ) -> Result < ( ) , String > {
211+ let path = get_config_path ( ) ;
212+ let parent = path. parent ( ) . unwrap ( ) ;
213+
214+ if !parent. exists ( ) {
215+ fs:: create_dir_all ( parent)
216+ . map_err ( |e| format ! ( "Could not create config directory: {e}" ) ) ?;
217+ }
218+
219+ let yaml =
220+ serde_yaml:: to_string ( & self ) . map_err ( |e| format ! ( "Could not serialize config: {e}" ) ) ?;
221+
222+ fs:: write ( & path, yaml) . map_err ( |e| format ! ( "Could not write config file: {e}" ) )
223+ }
224+ }
0 commit comments