Add `ReadStorage::contains` method

This commit is contained in:
Alexis Sellier 2023-02-12 21:04:55 +01:00
parent c673f5db68
commit b461950d59
No known key found for this signature in database
3 changed files with 18 additions and 0 deletions

View File

@ -254,6 +254,8 @@ pub trait ReadStorage {
remote: &RemoteId,
rid: Id,
) -> Result<Option<identity::Doc<Verified>>, ProjectError>;
/// Check whether storage contains a repository.
fn contains(&self, rid: &Id) -> Result<bool, ProjectError>;
/// Get the inventory of repositories hosted under this storage.
fn inventory(&self) -> Result<Inventory, Error>;
}
@ -357,6 +359,10 @@ where
self.deref().path()
}
fn contains(&self, rid: &Id) -> Result<bool, ProjectError> {
self.deref().contains(rid)
}
fn inventory(&self) -> Result<Inventory, Error> {
self.deref().inventory()
}

View File

@ -99,6 +99,14 @@ impl ReadStorage for Storage {
self.path.as_path()
}
fn contains(&self, rid: &Id) -> Result<bool, ProjectError> {
if paths::repository(&self, rid).exists() {
let _ = self.repository(*rid)?.head()?;
return Ok(true);
}
Ok(false)
}
fn get(&self, remote: &RemoteId, proj: Id) -> Result<Option<Doc<Verified>>, ProjectError> {
// TODO: Don't create a repo here if it doesn't exist?
// Perhaps for checking we could have a `contains` method?

View File

@ -36,6 +36,10 @@ impl ReadStorage for MockStorage {
self.path.as_path()
}
fn contains(&self, rid: &Id) -> Result<bool, ProjectError> {
Ok(self.inventory.contains_key(rid))
}
fn get(
&self,
_remote: &RemoteId,