I wrote a bot that performs some network queries once every so often and dumps the latest state into a file stored on disk. This is my code:
let log_file = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open(&log_file_name)
.unwrap();
serde_json::to_writer(
log_file,
&State {
foo: self.foo,
bar: self.bar
},
)
.unwrap();
The issue is that, occasionally, the size of the newest state json will be smaller than the size of the previous state JSON. This leaves that portion of the previous state JSON that is longer than the new state JSON to remain in the log file:
{"foo": 1,"bar": 2}"baz": 3}
In the example above, "baz": 3}
is the leftover data from the previous state JSON.
The question is: how can I rewrite the code so that I first flush all the contents of the file and then write the new state json?
I know deleting the file is a solution but I would prefer not to do that.