Skip to content

Commit cfcda48

Browse files
committed
create a backup of the exercise file
1 parent 7170279 commit cfcda48

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

exercises_downloader/src/main.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::fs;
88
use std::fs::File;
99
use std::io::Write;
1010
use std::path::Path;
11+
use std::time::SystemTime;
1112

1213
#[derive(Deserialize, Debug)]
1314
struct Bite {
@@ -57,6 +58,17 @@ fn write_exercise(path: &Path, template: String) -> std::io::Result<()> {
5758
let src_dir = path.join("src");
5859
fs::create_dir_all(&src_dir)?;
5960
let filename = src_dir.join("lib.rs");
61+
62+
if fs::exists(&filename)? {
63+
// backup the original lib.rs (exercise file) by adding a UNIX_EPOCH timestamp after .rs
64+
let now = SystemTime::now()
65+
.duration_since(SystemTime::UNIX_EPOCH)
66+
.unwrap()
67+
.as_secs();
68+
let new_filename = &filename.with_extension(format!("rs.{}", now));
69+
fs::rename(&filename, new_filename)?;
70+
}
71+
6072
let mut file = File::create(filename)?;
6173
file.write_all(template.as_bytes())?;
6274
Ok(())

0 commit comments

Comments
 (0)