1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
use serde::{de::DeserializeOwned, Serialize};
use std::{
    io::Write,
    sync::{RwLock, RwLockReadGuard, RwLockWriteGuard},
};
use std::{
    ops::{Deref, DerefMut},
    path::{Path, PathBuf},
    sync::Arc,
};
use thiserror::Error;

#[derive(Error, Debug)]
pub enum AcidJsonError {
    #[error("I/O error: {0}")]
    IoError(#[from] std::io::Error),
    #[error("JSON parsing error: {0}")]
    JsonError(#[from] serde_json::Error),
}

/// A "smart pointer" to a JSON file on disk. Can be used in a RwLock-like fashion for thread-safe, ACID-guaranteed updates to the underlying file. Is "Arc-like" can can be cheaply cloned to create more references to the same file.
#[derive(Clone, Debug)]
pub struct AcidJson<T: Serialize + DeserializeOwned + Sync> {
    cached: Arc<RwLock<T>>,
    fname: PathBuf,
}

impl<T: Serialize + DeserializeOwned + Sync> AcidJson<T> {
    /// Opens an AcidJson.
    pub fn open(fname: &Path) -> Result<Self, AcidJsonError> {
        let file_contents = std::fs::read(fname)?;
        let parsed: T = serde_json::from_slice(&file_contents)?;
        Ok(Self {
            cached: RwLock::new(parsed).into(),
            fname: fname.to_owned(),
        })
    }

    /// Opens an AcidJson, with a default value if the file does not yet exist.
    pub fn open_or_else(fname: &Path, gen_def: impl FnOnce() -> T) -> Result<Self, AcidJsonError> {
        if !fname.exists() {
            std::fs::write(fname, serde_json::to_vec(&gen_def())?)?;
        }
        let file_contents = std::fs::read(fname)?;
        let parsed: T = serde_json::from_slice(&file_contents)?;
        Ok(Self {
            cached: RwLock::new(parsed).into(),
            fname: fname.to_owned(),
        })
    }

    /// Read-locks the AcidJson.
    pub fn read(&self) -> AcidJsonReadGuard<T> {
        let inner = self.cached.read().unwrap();
        AcidJsonReadGuard { inner }
    }

    /// Write-locks the AcidJson.
    pub fn write(&self) -> AcidJsonWriteGuard<T> {
        let inner = self.cached.write().unwrap();
        let init_serialized = serde_json::to_vec(inner.deref()).expect("cannot serialize");
        AcidJsonWriteGuard {
            inner,
            fname: self.fname.clone(),
            init_serialized,
        }
    }
}

/// A read guard for an acidjson.
pub struct AcidJsonReadGuard<'a, T: Serialize + DeserializeOwned + Sync> {
    inner: RwLockReadGuard<'a, T>,
}

impl<'a, T: Serialize + DeserializeOwned + Sync> Deref for AcidJsonReadGuard<'a, T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

/// A write guard for an acidjson.
pub struct AcidJsonWriteGuard<'a, T: Serialize + DeserializeOwned + Sync> {
    inner: RwLockWriteGuard<'a, T>,
    fname: PathBuf,
    init_serialized: Vec<u8>,
}

impl<'a, T: Serialize + DeserializeOwned + Sync> Deref for AcidJsonWriteGuard<'a, T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        self.inner.deref()
    }
}

impl<'a, T: Serialize + DeserializeOwned + Sync> DerefMut for AcidJsonWriteGuard<'a, T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.inner.deref_mut()
    }
}

impl<'a, T: Serialize + DeserializeOwned + Sync> Drop for AcidJsonWriteGuard<'a, T> {
    fn drop(&mut self) {
        let serialized = serde_json::to_vec(self.inner.deref()).expect("cannot serialize");
        if serialized != self.init_serialized {
            atomicwrites::AtomicFile::new(
                &self.fname,
                atomicwrites::OverwriteBehavior::AllowOverwrite,
            )
            .write(|f| f.write_all(&serialized))
            .expect("could not write acidjson");
            log::debug!(
                "wrote {} bytes to {}",
                serialized.len(),
                self.fname.as_os_str().to_string_lossy()
            );
        } else {
            log::debug!("not writing because nothing changed")
        }
    }
}