1
use std::sync::Arc;
2

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

            
5
/// A shared environment for database operations.
6
104794
#[derive(Default, Debug, Clone)]
7
#[must_use]
8
pub struct Context<M: FileManager> {
9
    /// The file manager for the [`ManagedFile`](crate::io::ManagedFile) implementor.
10
    pub file_manager: M,
11
    /// The optional vault in use.
12
    pub(crate) vault: Option<Arc<dyn AnyVault>>,
13
    /// The optional chunk cache to use.
14
    pub(crate) cache: Option<ChunkCache>,
15
}
16

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

            
23
    /// Returns the context's chunk cache.
24
10095
    pub fn cache(&self) -> Option<&ChunkCache> {
25
10095
        self.cache.as_ref()
26
10095
    }
27

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

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

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