Skip to main content

bitwarden_state/
repository.rs

1use std::any::TypeId;
2
3use serde::{Serialize, de::DeserializeOwned};
4
5use crate::registry::StateRegistryError;
6
7/// An error resulting from operations on a repository.
8#[derive(thiserror::Error, Debug)]
9pub enum RepositoryError {
10    /// An internal unspecified error.
11    #[error("Internal error: {0}")]
12    Internal(String),
13
14    /// A serialization or deserialization error.
15    #[error(transparent)]
16    Serde(#[from] serde_json::Error),
17
18    /// An internal database error.
19    #[error(transparent)]
20    Database(#[from] crate::sdk_managed::DatabaseError),
21
22    /// State registry error.
23    #[error(transparent)]
24    StateRegistry(#[from] StateRegistryError),
25}
26
27/// This trait represents a generic repository interface, capable of storing and retrieving
28/// items using a key-value API.
29#[async_trait::async_trait]
30pub trait Repository<V: RepositoryItem>: Send + Sync {
31    /// Retrieves an item from the repository by its key.
32    async fn get(&self, key: V::Key) -> Result<Option<V>, RepositoryError>;
33    /// Lists all items in the repository.
34    async fn list(&self) -> Result<Vec<V>, RepositoryError>;
35    /// Sets an item in the repository with the specified key.
36    async fn set(&self, key: V::Key, value: V) -> Result<(), RepositoryError>;
37    /// Sets multiple items in the repository.
38    async fn set_bulk(&self, values: Vec<(V::Key, V)>) -> Result<(), RepositoryError>;
39    /// Removes an item from the repository by its key.
40    async fn remove(&self, key: V::Key) -> Result<(), RepositoryError>;
41    /// Removes multiple items from the repository by their keys.
42    async fn remove_bulk(&self, keys: Vec<V::Key>) -> Result<(), RepositoryError>;
43    /// Removes all items from the repository.
44    async fn remove_all(&self) -> Result<(), RepositoryError>;
45}
46
47/// This trait is used to mark types that can be stored in a repository.
48/// It should not be implemented manually; instead, users should
49/// use the [crate::register_repository_item] macro to register their item types.
50///
51/// All repository items must implement `Serialize` and `DeserializeOwned` to support
52/// SDK-managed repositories that persist items to storage.
53pub trait RepositoryItem: Internal + Serialize + DeserializeOwned + Send + Sync + 'static {
54    /// The name of the type implementing this trait.
55    const NAME: &'static str;
56
57    /// The type used as a key in the Repository
58    type Key: ToString + Send + Sync + 'static;
59
60    /// Returns the `TypeId` of the type implementing this trait.
61    fn type_id() -> TypeId {
62        TypeId::of::<Self>()
63    }
64
65    /// Returns metadata about the repository item type.
66    fn data() -> RepositoryItemData {
67        RepositoryItemData::new::<Self>()
68    }
69}
70
71/// This struct holds metadata about a registered repository item type.
72#[allow(dead_code)]
73#[derive(Debug, Clone, Copy)]
74pub struct RepositoryItemData {
75    type_id: TypeId,
76    name: &'static str,
77}
78
79impl RepositoryItemData {
80    /// Create a new `RepositoryItemData` from a type that implements `RepositoryItem`.
81    pub fn new<T: RepositoryItem>() -> Self {
82        Self {
83            type_id: TypeId::of::<T>(),
84            name: T::NAME,
85        }
86    }
87
88    /// Get the `TypeId` of the registered type.
89    pub fn type_id(&self) -> TypeId {
90        self.type_id
91    }
92    /// Get the name of the registered type.
93    /// This name is guaranteed to be a valid identifier.
94    pub fn name(&self) -> &'static str {
95        self.name
96    }
97}
98
99/// Validate that the provided name will be a valid identifier at compile time.
100/// This is intentionally limited to ensure compatibility with current and future storage backends.
101/// For example, SQLite tables must not begin with a number or contain special characters.
102/// Valid characters are a-z, A-Z, and underscore (_).
103pub const fn validate_registry_name(name: &str) -> bool {
104    let bytes = name.as_bytes();
105    let mut i = 0;
106    while i < bytes.len() {
107        let byte = bytes[i];
108        // Check if character is alphabetic (a-z, A-Z) or underscore
109        if !((byte >= b'a' && byte <= b'z') || (byte >= b'A' && byte <= b'Z') || byte == b'_') {
110            return false;
111        }
112        i += 1;
113    }
114    true
115}
116
117/// Represents a set of migrations for multiple repositories in a database migration process.
118#[derive(Debug, Clone)]
119pub struct RepositoryMigrations {
120    pub(crate) steps: Vec<RepositoryMigrationStep>,
121    // This is used only by indexedDB
122    #[allow(dead_code)]
123    pub(crate) version: u32,
124}
125
126/// Represents a single step for a repository in a database migration process.
127#[derive(Debug, Clone, Copy)]
128pub enum RepositoryMigrationStep {
129    /// Add a new repository.
130    Add(RepositoryItemData),
131    /// Remove an existing repository.
132    Remove(RepositoryItemData),
133}
134
135impl RepositoryMigrations {
136    /// Create a new `RepositoryMigrations` with the given steps. The version is derived from the
137    /// number of steps.
138    pub fn new(steps: Vec<RepositoryMigrationStep>) -> Self {
139        Self {
140            version: steps.len() as u32,
141            steps,
142        }
143    }
144
145    /// Converts the migration steps into a list of unique repository item data.
146    pub fn into_repository_items(self) -> Vec<RepositoryItemData> {
147        let mut map = std::collections::HashMap::new();
148        for step in self.steps {
149            match step {
150                RepositoryMigrationStep::Add(data) => {
151                    map.insert(data.type_id, data);
152                }
153                RepositoryMigrationStep::Remove(data) => {
154                    map.remove(&data.type_id);
155                }
156            }
157        }
158        map.into_values().collect()
159    }
160}
161
162/// Register a type for use in a repository. The type must only be registered once in the crate
163/// where it's defined. The provided name must be unique and not be changed.
164#[macro_export]
165macro_rules! register_repository_item {
166    ($keyty:ty => $ty:ty, $name:literal) => {
167        const _: () = {
168            impl $crate::repository::___internal::Internal for $ty {}
169            impl $crate::repository::RepositoryItem for $ty {
170                const NAME: &'static str = $name;
171                type Key = $keyty;
172            }
173            assert!(
174                $crate::repository::validate_registry_name($name),
175                concat!(
176                    "Repository name '",
177                    $name,
178                    "' must contain only alphabetic characters and underscores"
179                )
180            )
181        };
182    };
183}
184
185/// This code is not meant to be used directly, users of this crate should use the
186/// [crate::register_repository_item] macro to register their types.
187#[doc(hidden)]
188pub mod ___internal {
189
190    // This trait is in an internal module to try to forbid users from implementing `RepositoryItem`
191    // directly.
192    pub trait Internal {}
193}
194pub(crate) use ___internal::Internal;
195
196#[cfg(test)]
197mod tests {
198    use super::*;
199
200    #[test]
201    fn test_validate_name() {
202        assert!(validate_registry_name("valid"));
203        assert!(validate_registry_name("Valid_Name"));
204        assert!(!validate_registry_name("Invalid-Name"));
205        assert!(!validate_registry_name("Invalid Name"));
206        assert!(!validate_registry_name("Invalid.Name"));
207        assert!(!validate_registry_name("Invalid123"));
208    }
209}