coven_foundation/files/
deletion.rs1use std::path::Path;
12
13use serde::{Deserialize, Serialize};
14
15use super::{atomic_file, FileError};
16
17const RECORD: &str = "deleting.json";
18
19#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
21pub enum IfUnsent {
22 Refuse,
24 Discard,
26}
27
28#[derive(Serialize, Deserialize)]
29#[serde(deny_unknown_fields)]
30struct DeletionRecord {
31 unsent: IfUnsent,
32}
33
34pub(super) fn read(directory: &Path) -> Result<Option<IfUnsent>, FileError> {
36 Ok(atomic_file::read_json::<DeletionRecord>(&directory.join(RECORD), "read deletion record")?
37 .map(|record| record.unsent))
38}
39
40pub(super) fn record(directory: &Path, unsent: IfUnsent) -> Result<(), FileError> {
42 let bytes = serde_json::to_vec(&DeletionRecord { unsent }).expect("deletion record encodes");
43 atomic_file::replace(&directory.join(RECORD), &bytes)
44}
45
46pub(super) fn forget(directory: &Path) -> Result<(), FileError> {
49 atomic_file::remove(&directory.join(RECORD))
50}
51
52#[cfg(test)]
53#[path = "deletion_tests.rs"]
54mod tests;