cnidarium/storage/
temp.rs

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
use crate::Storage;
use std::ops::Deref;
use tempfile::TempDir;

/// A [`Storage`] instance backed by a [`tempfile::TempDir`] for testing.
///
/// The `TempDir` handle is bundled into the `TempStorage`, so the temporary
/// directory is cleaned up when the `TempStorage` instance is dropped.
pub struct TempStorage {
    inner: Storage,
    _dir: TempDir,
}

impl Deref for TempStorage {
    type Target = Storage;
    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl AsRef<Storage> for TempStorage {
    fn as_ref(&self) -> &Storage {
        &self.inner
    }
}

impl TempStorage {
    pub async fn new() -> anyhow::Result<Self> {
        let dir = tempfile::tempdir()?;
        let db_filepath = dir.path().join("storage.db");
        let inner = Storage::load(db_filepath.clone(), vec![]).await?;

        Ok(TempStorage { inner, _dir: dir })
    }

    pub async fn new_with_prefixes(prefixes: Vec<String>) -> anyhow::Result<Self> {
        let dir = tempfile::tempdir()?;
        let db_filepath = dir.path().join("storage.db");
        let inner = Storage::load(db_filepath.clone(), prefixes).await?;

        Ok(TempStorage { inner, _dir: dir })
    }
}