bitwarden_uniffi/platform/
repository.rs

1use std::sync::Arc;
2
3pub struct UniffiRepositoryBridge<T>(pub T);
4
5impl<T: ?Sized> UniffiRepositoryBridge<Arc<T>> {
6    pub fn new(store: Arc<T>) -> Arc<Self> {
7        Arc::new(UniffiRepositoryBridge(store))
8    }
9}
10
11impl<T: std::fmt::Debug> std::fmt::Debug for UniffiRepositoryBridge<T> {
12    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
13        self.0.fmt(f)
14    }
15}
16
17#[derive(uniffi::Error, thiserror::Error, Debug)]
18pub enum RepositoryError {
19    #[error("Internal error: {0}")]
20    Internal(String),
21}
22
23// Need to implement this From<> impl in order to handle unexpected callback errors.  See the
24// following page in the Uniffi user guide:
25// <https://mozilla.github.io/uniffi-rs/foreign_traits.html#error-handling>
26impl From<uniffi::UnexpectedUniFFICallbackError> for RepositoryError {
27    fn from(e: uniffi::UnexpectedUniFFICallbackError) -> Self {
28        Self::Internal(e.reason)
29    }
30}
31
32impl From<RepositoryError> for bitwarden_state::repository::RepositoryError {
33    fn from(e: RepositoryError) -> Self {
34        match e {
35            RepositoryError::Internal(msg) => Self::Internal(msg),
36        }
37    }
38}
39
40/// This macro creates a Uniffi repository trait and its implementation for the
41/// [bitwarden_state::repository::Repository] trait
42macro_rules! create_uniffi_repositories {
43    ( $container_name:ident ; $( $qualified_type_name:ty, $type_name:ident, $field_name:ident, $repo_name:ident );+ $(;)? ) => {
44
45        #[derive(::uniffi::Record)]
46        pub struct $container_name {
47            $(
48                pub $field_name: Option<::std::sync::Arc<dyn $repo_name>>,
49            )+
50        }
51
52        impl $container_name {
53            pub fn register_all(self, client: &bitwarden_core::platform::StateClient) {
54                $(
55                    if let Some(repo) = self.$field_name {
56                        let bridge = $crate::platform::repository::UniffiRepositoryBridge::new(repo);
57                        client.register_client_managed(bridge);
58                    }
59                )+
60            }
61        }
62
63        $(
64            #[::uniffi::export(with_foreign)]
65            #[::async_trait::async_trait]
66            pub trait $repo_name: Send + Sync {
67                async fn get(
68                    &self,
69                    id: String,
70                ) -> Result<Option<$qualified_type_name>, $crate::platform::repository::RepositoryError>;
71                async fn list(&self)
72                    -> Result<Vec<$qualified_type_name>, $crate::platform::repository::RepositoryError>;
73                async fn set(
74                    &self,
75                    id: String,
76                    value: $qualified_type_name,
77                ) -> Result<(), $crate::platform::repository::RepositoryError>;
78                async fn remove(
79                    &self,
80                    id: String,
81                ) -> Result<(), $crate::platform::repository::RepositoryError>;
82
83                async fn has(
84                    &self,
85                    id: String,
86                ) -> Result<bool, $crate::platform::repository::RepositoryError>;
87            }
88
89            #[async_trait::async_trait]
90            impl bitwarden_state::repository::Repository<$qualified_type_name>
91                for $crate::platform::repository::UniffiRepositoryBridge<Arc<dyn $repo_name>>
92            {
93                async fn get(
94                    &self,
95                    key: String,
96                ) -> Result<Option<$qualified_type_name>, bitwarden_state::repository::RepositoryError> {
97                    self.0.get(key).await.map_err(Into::into)
98                }
99                async fn list(&self) -> Result<Vec<$qualified_type_name>, bitwarden_state::repository::RepositoryError> {
100                    self.0.list().await.map_err(Into::into)
101                }
102                async fn set(
103                    &self,
104                    key: String,
105                    value: $qualified_type_name,
106                ) -> Result<(), bitwarden_state::repository::RepositoryError> {
107                    self.0.set(key, value).await.map_err(Into::into)
108                }
109                async fn remove(
110                    &self,
111                    key: String,
112                ) -> Result<(), bitwarden_state::repository::RepositoryError> {
113                    self.0.remove(key).await.map_err(Into::into)
114                }
115            }
116        )+
117    };
118}
119
120pub(super) use create_uniffi_repositories;