Skip to main content

dbx_tools_databricks_auth/storage/
memory.rs

1use std::{collections::HashMap, path::PathBuf, time::Duration};
2
3use async_trait::async_trait;
4use tokio::sync::RwLock;
5
6use super::{CredentialStore, FileStore, StorageLock};
7use crate::{Result, Token};
8
9pub struct MemoryStore {
10    tokens: RwLock<HashMap<String, Token>>,
11    lock_store: FileStore,
12}
13
14impl MemoryStore {
15    pub fn new(lock_directory: PathBuf) -> Result<Self> {
16        Ok(Self {
17            tokens: RwLock::new(HashMap::new()),
18            lock_store: FileStore::new(lock_directory)?,
19        })
20    }
21}
22
23#[async_trait]
24impl CredentialStore for MemoryStore {
25    async fn load(&self, profile: &str) -> Result<Option<Token>> {
26        Ok(self.tokens.read().await.get(profile).cloned())
27    }
28
29    async fn save(&self, profile: &str, token: &Token) -> Result<()> {
30        self.tokens
31            .write()
32            .await
33            .insert(profile.to_owned(), token.clone());
34        Ok(())
35    }
36
37    async fn delete(&self, profile: &str) -> Result<()> {
38        self.tokens.write().await.remove(profile);
39        Ok(())
40    }
41
42    async fn lock(&self, profile: &str, timeout: Duration) -> Result<Box<dyn StorageLock>> {
43        Ok(Box::new(
44            self.lock_store.acquire_file_lock(profile, timeout).await?,
45        ))
46    }
47
48    fn name(&self) -> &'static str {
49        "memory"
50    }
51}