nrc_mls_storage/
lib.rs

1//! Nostr MLS storage - A set of storage provider traits and types for implementing MLS storage
2//! It is designed to be used in conjunction with the `openmls` crate.
3
4#![forbid(unsafe_code)]
5#![warn(missing_docs)]
6#![warn(rustdoc::bare_urls)]
7
8use openmls_traits::storage::StorageProvider;
9
10pub mod groups;
11pub mod messages;
12pub mod welcomes;
13
14pub mod test_utils;
15
16use self::groups::GroupStorage;
17use self::messages::MessageStorage;
18use self::welcomes::WelcomeStorage;
19
20const CURRENT_VERSION: u16 = 1;
21
22/// Backend
23#[derive(Debug, Clone, PartialEq, Eq)]
24pub enum Backend {
25    /// Memory
26    Memory,
27    /// SQLite
28    SQLite,
29}
30
31impl Backend {
32    /// Check if it's a persistent backend
33    ///
34    /// All values different from [`Backend::Memory`] are considered persistent
35    pub fn is_persistent(&self) -> bool {
36        !matches!(self, Self::Memory)
37    }
38}
39
40/// Storage provider for the Nostr MLS storage
41pub trait NostrMlsStorageProvider: GroupStorage + MessageStorage + WelcomeStorage {
42    /// The OpenMLS storage provider
43    type OpenMlsStorageProvider: StorageProvider<CURRENT_VERSION>;
44
45    /// Returns the backend type.
46    ///
47    /// # Returns
48    ///
49    /// [`Backend::Memory`] indicating this is a memory-based storage implementation.
50    fn backend(&self) -> Backend;
51
52    /// Get a reference to the openmls storage provider.
53    ///
54    /// This method provides access to the underlying OpenMLS storage provider.
55    /// This is primarily useful for internal operations and testing.
56    ///
57    /// # Returns
58    ///
59    /// A reference to the openmls storage implementation.
60    fn openmls_storage(&self) -> &Self::OpenMlsStorageProvider;
61
62    /// Get a mutable reference to the openmls storage provider.
63    ///
64    /// This method provides mutable access to the underlying OpenMLS storage provider.
65    /// This is primarily useful for internal operations and testing.
66    ///
67    /// # Returns
68    ///
69    /// A mutable reference to the openmls storage implementation.
70    fn openmls_storage_mut(&mut self) -> &mut Self::OpenMlsStorageProvider;
71}
72
73#[cfg(test)]
74mod tests {
75    use super::*;
76
77    #[test]
78    fn test_backend_is_persistent() {
79        assert!(!Backend::Memory.is_persistent());
80        assert!(Backend::SQLite.is_persistent());
81    }
82}