@@ -55,7 +55,7 @@ impl CacheConfig {
5555 Self :: new ( None , None , None )
5656 }
5757
58- pub ( crate ) fn load ( cache_path : Option < PathBuf > ) -> Self {
58+ pub ( crate ) fn load ( cache_path : Option < PathBuf > , arg_history_file : & Option < String > ) -> Self {
5959 let mut cache = Self :: empty ( ) ;
6060
6161 if let Some ( cache_path) = cache_path {
@@ -68,6 +68,16 @@ impl CacheConfig {
6868 }
6969 } ;
7070
71+ if let Some ( hf) = arg_history_file {
72+ cache. last_history_file = Some ( hf. to_string ( ) ) ;
73+ }
74+
75+ if cache. last_history_file . is_none ( ) {
76+ println ! ( "You must specify a history file `cforge <history_file>` for the first time." ) ;
77+ println ! ( "See `cforge --help` for more information." ) ;
78+ panic ! ( "No history file specified and no previous history file found from cache." ) ;
79+ }
80+
7181 cache
7282 }
7383
@@ -83,6 +93,15 @@ impl CacheConfig {
8393 }
8494 }
8595 }
96+
97+ pub fn get_history_file_path ( & self ) -> String {
98+ match & self . last_history_file {
99+ Some ( hf) => hf. to_string ( ) ,
100+ None => panic ! (
101+ "No history file given as an argument or found from cache. This shouldn't happen"
102+ ) ,
103+ }
104+ }
86105}
87106
88107#[ cfg( test) ]
@@ -91,60 +110,57 @@ mod tests {
91110
92111 use tempfile:: TempDir ;
93112
94- use crate :: config:: cache_config:: { CacheConfig , CACHE_FILE } ;
113+ use crate :: config:: cache_config:: { CACHE_FILE , CacheConfig } ;
95114
96115 #[ test]
116+ #[ should_panic]
97117 fn load_invalid_cache_config ( ) {
98118 let temp_dir = create_cache_config (
99119 "
100120 thisisa malformed \" string !\" #¤
101121 " ,
102122 ) ;
103123 let path_opt = Some ( temp_dir. path ( ) . to_path_buf ( ) ) ;
104- let config = CacheConfig :: load ( path_opt) ;
105-
106- assert_eq ! (
107- config. last_history_file,
108- CacheConfig :: empty( ) . last_history_file
109- ) ;
124+ let _ = CacheConfig :: load ( path_opt, & None ) ;
110125 }
111126
112127 #[ test]
128+ #[ should_panic]
113129 fn load_non_existent_cache_config ( ) {
114130 let temp_dir = create_cache_config ( "" ) ;
115131 let path_opt = Some ( temp_dir. path ( ) . join ( "doesnt_exist.toml" ) . to_path_buf ( ) ) ;
116- let config = CacheConfig :: load ( path_opt) ;
117-
118- assert_eq ! (
119- config. last_history_file,
120- CacheConfig :: empty( ) . last_history_file
121- ) ;
132+ let _ = CacheConfig :: load ( path_opt, & None ) ;
122133 }
123134
124135 #[ test]
136+ #[ should_panic]
125137 fn load_empty_cache_config ( ) {
126138 let temp_dir = create_cache_config ( "" ) ;
127139 let path_opt = Some ( temp_dir. path ( ) . to_path_buf ( ) ) ;
128- let config = CacheConfig :: load ( path_opt) ;
129-
130- assert_eq ! (
131- config. last_history_file,
132- CacheConfig :: empty( ) . last_history_file
133- ) ;
140+ let _ = CacheConfig :: load ( path_opt, & None ) ;
134141 }
135142
136143 #[ test]
137144 fn load_valid_cache_config ( ) {
138145 let temp_dir = create_cache_config ( "last_history_file = \" some_history_file\" " ) ;
139146 let path_opt = Some ( temp_dir. path ( ) . to_path_buf ( ) ) ;
140- let config = CacheConfig :: load ( path_opt) ;
147+ let config = CacheConfig :: load ( path_opt, & None ) ;
141148
142149 assert_eq ! (
143150 config. last_history_file,
144151 Some ( "some_history_file" . to_string( ) )
145152 ) ;
146153 }
147154
155+ #[ test]
156+ fn override_using_arg_history ( ) {
157+ let temp_dir = create_cache_config ( "last_history_file = \" some_history_file\" " ) ;
158+ let path_opt = Some ( temp_dir. path ( ) . to_path_buf ( ) ) ;
159+ let config = CacheConfig :: load ( path_opt, & Some ( "override_path" . to_string ( ) ) ) ;
160+
161+ assert_eq ! ( config. last_history_file, Some ( "override_path" . to_string( ) ) ) ;
162+ }
163+
148164 fn create_cache_config ( content : & str ) -> TempDir {
149165 let temp_dir: TempDir = TempDir :: new ( ) . unwrap ( ) ;
150166 let config_path: PathBuf = temp_dir. path ( ) . join ( CACHE_FILE ) ;
0 commit comments