File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -202,17 +202,15 @@ pub(crate) fn trimmed_option(value: Option<&str>) -> Option<&str> {
202202}
203203
204204pub fn save_file ( path : & Path , config : & Config ) -> Result < ( ) > {
205- if let Some ( parent) = path. parent ( ) {
206- fs:: create_dir_all ( parent) ?;
207- }
205+ let parent = path. parent ( ) . unwrap_or_else ( || Path :: new ( "." ) ) ;
206+ fs:: create_dir_all ( parent) ?;
208207
209208 let json = serde_json:: to_string_pretty ( config) ?;
210- let temp_path = path. with_extension ( "tmp" ) ;
211- let mut file = fs:: File :: create ( & temp_path) ?;
209+ let mut file = tempfile:: NamedTempFile :: new_in ( parent) ?;
212210 file. write_all ( json. as_bytes ( ) ) ?;
213211 file. write_all ( b"\n " ) ?;
214- file. sync_all ( ) ?;
215- fs :: rename ( & temp_path , path) ?;
212+ file. as_file ( ) . sync_all ( ) ?;
213+ file . persist ( path) ?;
216214
217215 Ok ( ( ) )
218216}
You can’t perform that action at this time.
0 commit comments