Skip to content

Commit 9a7facf

Browse files
committed
Delete old file when serializing state
1 parent a7af065 commit 9a7facf

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

src/structures/state.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use serde_json::{from_reader, to_writer_pretty};
22
use serde::{Deserialize, Serialize};
3-
use std::{path::Path, fs::{File, OpenOptions}};
4-
use super::{Snippet};
3+
use std::fs::{self, File, OpenOptions };
4+
use std::path::Path;
5+
use super::Snippet;
56

67
#[derive(Deserialize, Serialize)]
78
pub struct State {
@@ -30,6 +31,11 @@ impl State {
3031

3132
pub fn write(&self, file_path: &str) {
3233
let path = Path::new(file_path);
34+
35+
if path.exists() {
36+
fs::remove_file(path).expect("Failed to delete old file.");
37+
}
38+
3339
let result = OpenOptions::new()
3440
.read(true)
3541
.write(true)

0 commit comments

Comments
 (0)