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
use std::sync::Arc;

use crate::{io::FileManager, vault::AnyVault, ChunkCache, Vault};

/// A shared environment for database operations.
#[derive(Default, Debug, Clone)]
#[must_use]
pub struct Context<M: FileManager> {
    /// The file manager for the [`ManagedFile`](crate::io::ManagedFile) implementor.
    pub file_manager: M,
    /// The optional vault in use.
    pub(crate) vault: Option<Arc<dyn AnyVault>>,
    /// The optional chunk cache to use.
    pub(crate) cache: Option<ChunkCache>,
}

impl<M: FileManager> Context<M> {
    /// Returns the vault as a dynamic reference.
    pub fn vault(&self) -> Option<&dyn AnyVault> {
        self.vault.as_deref()
    }

    /// Returns the context's chunk cache.
    pub fn cache(&self) -> Option<&ChunkCache> {
        self.cache.as_ref()
    }

    /// Replaces the cache currently set with `cache`.
    pub fn with_cache(mut self, cache: ChunkCache) -> Self {
        self.cache = Some(cache);
        self
    }

    /// Replaces the vault currently set with `vault`.
    pub fn with_vault<V: Vault>(self, vault: V) -> Self {
        self.with_any_vault(Arc::new(vault))
    }

    /// Replaces the vault currently set with `vault`.
    pub fn with_any_vault(mut self, vault: Arc<dyn AnyVault>) -> Self {
        self.vault = Some(vault);
        self
    }
}