keep refactoring git store to make this iterator work nice
This commit is contained in:
parent
53e253b362
commit
4460aae75a
@ -1,2 +1,2 @@
|
|||||||
pub mod origin;
|
|
||||||
pub mod domain;
|
pub mod domain;
|
||||||
|
pub mod origin;
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
use super::Descr;
|
use super::Descr;
|
||||||
|
use mockall::automock;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
@ -54,9 +55,7 @@ impl<E: Error + 'static> From<E> for AllDescrsError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Used in the return from all_descrs from Store.
|
#[automock]
|
||||||
type AllDescrsResult<T> = Result<T, AllDescrsError>;
|
|
||||||
|
|
||||||
pub trait Origin {
|
pub trait Origin {
|
||||||
fn descr(&self) -> &Descr;
|
fn descr(&self) -> &Descr;
|
||||||
fn read_file_into(
|
fn read_file_into(
|
||||||
@ -66,14 +65,19 @@ pub trait Origin {
|
|||||||
) -> Result<(), ReadFileIntoError>;
|
) -> Result<(), ReadFileIntoError>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Used in the return from all_descrs from Store.
|
||||||
|
pub type AllDescrsResult<T> = Result<T, AllDescrsError>;
|
||||||
|
|
||||||
/// Describes a storage mechanism for Origins. Each Origin is uniquely identified by its Descr.
|
/// Describes a storage mechanism for Origins. Each Origin is uniquely identified by its Descr.
|
||||||
pub trait Store {
|
pub trait Store<'a> {
|
||||||
|
type AllDescrsIter: IntoIterator<Item = AllDescrsResult<Descr>>;
|
||||||
|
|
||||||
/// If the origin is of a kind which can be updated, sync will pull down the latest version of
|
/// If the origin is of a kind which can be updated, sync will pull down the latest version of
|
||||||
/// the origin into the storage.
|
/// the origin into the storage.
|
||||||
fn sync(&self, descr: &Descr, limits: Limits) -> Result<(), SyncError>;
|
fn sync(&'a self, descr: &Descr, limits: Limits) -> Result<(), SyncError>;
|
||||||
|
|
||||||
fn get(&self, descr: Descr) -> Result<Box<dyn Origin>, GetError>;
|
fn get(&'a self, descr: Descr) -> Result<Box<dyn Origin>, GetError>;
|
||||||
fn all_descrs(&self) -> AllDescrsResult<Box<dyn Iterator<Item = AllDescrsResult<Descr>> + '_>>;
|
fn all_descrs(&'a self) -> AllDescrsResult<Self::AllDescrsIter>;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod git {
|
pub mod git {
|
||||||
@ -117,13 +121,13 @@ pub mod git {
|
|||||||
|
|
||||||
/// git::Store implements the Store trait for any Descr::Git based Origins. If any non-git
|
/// git::Store implements the Store trait for any Descr::Git based Origins. If any non-git
|
||||||
/// Descrs are used then this implementation will panic.
|
/// Descrs are used then this implementation will panic.
|
||||||
pub struct Store<'a> {
|
pub struct Store {
|
||||||
dir_path: &'a Path,
|
dir_path: PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Store<'a> {
|
impl Store {
|
||||||
pub fn new(dir_path: &Path) -> io::Result<Store> {
|
pub fn new(dir_path: PathBuf) -> io::Result<Store> {
|
||||||
fs::create_dir_all(dir_path)?;
|
fs::create_dir_all(&dir_path)?;
|
||||||
Ok(Store { dir_path })
|
Ok(Store { dir_path })
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -135,13 +139,15 @@ pub mod git {
|
|||||||
self.dir_path.join(descr_id).join("descr.json")
|
self.dir_path.join(descr_id).join("descr.json")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn branch_ref(branch_name: &str) -> String {
|
fn branch_ref(&self, branch_name: &str) -> String {
|
||||||
format!("origin/{branch_name}")
|
format!("origin/{branch_name}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> super::Store for Store<'a> {
|
impl<'a> super::Store<'a> for Store {
|
||||||
fn sync(&self, descr: &Descr, _limits: Limits) -> Result<(), SyncError> {
|
type AllDescrsIter = Box<dyn Iterator<Item = AllDescrsResult<Descr>> + 'a>;
|
||||||
|
|
||||||
|
fn sync(&'a self, descr: &Descr, _limits: Limits) -> Result<(), SyncError> {
|
||||||
use gix::clone::Error as gixCloneErr;
|
use gix::clone::Error as gixCloneErr;
|
||||||
use gix::progress::Discard;
|
use gix::progress::Discard;
|
||||||
|
|
||||||
@ -170,7 +176,7 @@ pub mod git {
|
|||||||
|
|
||||||
// Check to make sure the branch name exists
|
// Check to make sure the branch name exists
|
||||||
// TODO if this fails we should delete repo_path
|
// TODO if this fails we should delete repo_path
|
||||||
repo.try_find_reference(&Self::branch_ref(branch_name))?
|
repo.try_find_reference(&self.branch_ref(branch_name))?
|
||||||
.ok_or(SyncError::InvalidBranchName)?;
|
.ok_or(SyncError::InvalidBranchName)?;
|
||||||
|
|
||||||
// Add the descr to the repo directory, so we can know the actual descr later
|
// Add the descr to the repo directory, so we can know the actual descr later
|
||||||
@ -198,7 +204,7 @@ pub mod git {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get(&self, descr: Descr) -> Result<Box<dyn super::Origin>, GetError> {
|
fn get(&'a self, descr: Descr) -> Result<Box<dyn super::Origin>, GetError> {
|
||||||
let repo_path = self.repo_path(&descr);
|
let repo_path = self.repo_path(&descr);
|
||||||
let Descr::Git {
|
let Descr::Git {
|
||||||
ref branch_name, ..
|
ref branch_name, ..
|
||||||
@ -212,7 +218,7 @@ pub mod git {
|
|||||||
let repo = gix::open(&repo_path)?;
|
let repo = gix::open(&repo_path)?;
|
||||||
|
|
||||||
let commit_object_id = repo
|
let commit_object_id = repo
|
||||||
.find_reference(&Self::branch_ref(branch_name))?
|
.find_reference(&self.branch_ref(branch_name))?
|
||||||
.peel_to_id_in_place()?
|
.peel_to_id_in_place()?
|
||||||
.detach();
|
.detach();
|
||||||
|
|
||||||
@ -228,11 +234,9 @@ pub mod git {
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn all_descrs(
|
fn all_descrs(&'a self) -> AllDescrsResult<Self::AllDescrsIter> {
|
||||||
&self,
|
Ok(Box::from(fs::read_dir(&self.dir_path)?.map(
|
||||||
) -> AllDescrsResult<Box<dyn Iterator<Item = AllDescrsResult<Descr>> + '_>> {
|
|dir_entry_res: io::Result<fs::DirEntry>| -> AllDescrsResult<Descr> {
|
||||||
let inner_iter = fs::read_dir(self.dir_path)?.map(
|
|
||||||
|dir_entry_res: io::Result<fs::DirEntry>| -> Result<Descr, AllDescrsError> {
|
|
||||||
let descr_id: String = dir_entry_res?
|
let descr_id: String = dir_entry_res?
|
||||||
.file_name()
|
.file_name()
|
||||||
.to_str()
|
.to_str()
|
||||||
@ -251,9 +255,7 @@ pub mod git {
|
|||||||
|
|
||||||
Ok(descr)
|
Ok(descr)
|
||||||
},
|
},
|
||||||
);
|
)))
|
||||||
|
|
||||||
Ok(Box::from(inner_iter))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -281,7 +283,7 @@ pub mod git {
|
|||||||
|
|
||||||
let limits = super::Limits {};
|
let limits = super::Limits {};
|
||||||
|
|
||||||
let store = super::Store::new(tmp_dir.path()).expect("store created");
|
let store = super::Store::new(tmp_dir.path().to_path_buf()).expect("store created");
|
||||||
|
|
||||||
store.sync(&descr, limits).expect("sync should succeed");
|
store.sync(&descr, limits).expect("sync should succeed");
|
||||||
store
|
store
|
||||||
|
Loading…
Reference in New Issue
Block a user