Skip to content

Change get_client_managed to return result #338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions crates/bitwarden-core/src/platform/state_client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::sync::Arc;

use bitwarden_state::repository::{Repository, RepositoryItem};
use bitwarden_state::{
registry::RepositoryNotFoundError,
repository::{Repository, RepositoryItem},
};

use crate::Client;

Expand All @@ -22,7 +25,9 @@
}

/// Get a client managed state repository for a specific type, if it exists.
pub fn get_client_managed<T: RepositoryItem>(&self) -> Option<Arc<dyn Repository<T>>> {
pub fn get_client_managed<T: RepositoryItem>(
&self,
) -> Result<Arc<dyn Repository<T>>, RepositoryNotFoundError> {

Check warning on line 30 in crates/bitwarden-core/src/platform/state_client.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-core/src/platform/state_client.rs#L28-L30

Added lines #L28 - L30 were not covered by tests
self.client.internal.repository_map.get_client_managed()
}
}
24 changes: 17 additions & 7 deletions crates/bitwarden-state/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use std::{
sync::{Arc, RwLock},
};

use thiserror::Error;

use crate::repository::{Repository, RepositoryItem};

/// A registry that contains repositories for different types of items.
Expand All @@ -18,6 +20,11 @@ impl std::fmt::Debug for StateRegistry {
}
}

/// Repository not found.
#[derive(Debug, Error)]
#[error("Repository not found for the requested type")]
pub struct RepositoryNotFoundError;

impl StateRegistry {
/// Creates a new empty `StateRegistry`.
#[allow(clippy::new_without_default)]
Expand All @@ -36,13 +43,16 @@ impl StateRegistry {
}

/// Retrieves a client-managed repository from the map given its type.
pub fn get_client_managed<T: RepositoryItem>(&self) -> Option<Arc<dyn Repository<T>>> {
pub fn get_client_managed<T: RepositoryItem>(
&self,
) -> Result<Arc<dyn Repository<T>>, RepositoryNotFoundError> {
self.client_managed
.read()
.expect("RwLock should not be poisoned")
.get(&TypeId::of::<T>())
.and_then(|boxed| boxed.downcast_ref::<Arc<dyn Repository<T>>>())
.map(Arc::clone)
.ok_or(RepositoryNotFoundError)
}
}

Expand Down Expand Up @@ -107,19 +117,19 @@ mod tests {
.unwrap()
}

assert!(map.get_client_managed::<TestItem<usize>>().is_none());
assert!(map.get_client_managed::<TestItem<String>>().is_none());
assert!(map.get_client_managed::<TestItem<Vec<u8>>>().is_none());
assert!(map.get_client_managed::<TestItem<usize>>().is_err());
assert!(map.get_client_managed::<TestItem<String>>().is_err());
assert!(map.get_client_managed::<TestItem<Vec<u8>>>().is_err());

map.register_client_managed(a.clone());
assert_eq!(get(&map).await, Some(TestItem(a.0)));
assert!(map.get_client_managed::<TestItem<String>>().is_none());
assert!(map.get_client_managed::<TestItem<Vec<u8>>>().is_none());
assert!(map.get_client_managed::<TestItem<String>>().is_err());
assert!(map.get_client_managed::<TestItem<Vec<u8>>>().is_err());

map.register_client_managed(b.clone());
assert_eq!(get(&map).await, Some(TestItem(a.0)));
assert_eq!(get(&map).await, Some(TestItem(b.0.clone())));
assert!(map.get_client_managed::<TestItem<Vec<u8>>>().is_none());
assert!(map.get_client_managed::<TestItem<Vec<u8>>>().is_err());

map.register_client_managed(c.clone());
assert_eq!(get(&map).await, Some(TestItem(a.0)));
Expand Down
6 changes: 6 additions & 0 deletions crates/bitwarden-state/src/repository.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
use std::any::TypeId;

use crate::registry::RepositoryNotFoundError;

/// An error resulting from operations on a repository.
#[derive(thiserror::Error, Debug)]
pub enum RepositoryError {
/// An internal unspecified error.
#[error("Internal error: {0}")]
Internal(String),

/// Repository not found.
#[error(transparent)]
RepositoryNotFound(#[from] RepositoryNotFoundError),
}

/// This trait represents a generic repository interface, capable of storing and retrieving
Expand Down