1
use async_trait::async_trait;
2

            
3
use super::{KeyOperation, KeyValue, Output};
4
use crate::Error;
5

            
6
/// A namespaced key-value store. All operations performed with this will be
7
/// separate from other namespaces.
8
pub struct Namespaced<'a, K> {
9
    namespace: String,
10
    kv: &'a K,
11
}
12

            
13
impl<'a, K> Namespaced<'a, K> {
14
21
    pub(crate) const fn new(namespace: String, kv: &'a K) -> Self {
15
21
        Self { namespace, kv }
16
21
    }
17
}
18

            
19
#[async_trait]
20
impl<'a, K> KeyValue for Namespaced<'a, K>
21
where
22
    K: KeyValue,
23
{
24
406
    async fn execute_key_operation(&self, op: KeyOperation) -> Result<Output, Error> {
25
410
        self.kv.execute_key_operation(op).await
26
812
    }
27

            
28
406
    fn key_namespace(&self) -> Option<&'_ str> {
29
406
        Some(&self.namespace)
30
406
    }
31

            
32
    fn with_key_namespace(&'_ self, namespace: &str) -> Namespaced<'_, Self>
33
    where
34
        Self: Sized,
35
    {
36
        Namespaced {
37
            namespace: format!("{}\u{0}{}", self.namespace, namespace),
38
            kv: self,
39
        }
40
    }
41
}