refactor errors in store.rs

This commit is contained in:
Brian Picciano 2023-05-07 14:33:22 +02:00
parent a7d5819a78
commit 71dcc94e29

View File

@ -8,10 +8,17 @@ use super::Descr;
pub struct Limits {}
#[derive(Debug)]
pub enum AllDescrsError {
pub enum WriteFileError {
FileNotFound,
Unexpected(Box<dyn Error>),
}
impl<E: Error + 'static> From<E> for WriteFileError {
fn from(e: E) -> WriteFileError {
WriteFileError::Unexpected(Box::from(e))
}
}
#[derive(Debug)]
pub enum SyncError {
InvalidURL,
@ -19,12 +26,25 @@ pub enum SyncError {
Unexpected(Box<dyn Error>),
}
impl<E: Error + 'static> From<E> for SyncError {
fn from(e: E) -> SyncError {
SyncError::Unexpected(Box::from(e))
}
}
#[derive(Debug)]
pub enum WriteFileError {
FileNotFound,
pub enum AllDescrsError {
Unexpected(Box<dyn Error>),
}
impl<E: Error + 'static> From<E> for AllDescrsError {
fn from(e: E) -> AllDescrsError {
AllDescrsError::Unexpected(Box::from(e))
}
}
type AllDescrsResult<T> = Result<T, AllDescrsError>;
pub trait Store {
fn write_file<W>(&self, descr: &Descr, path: &str, into: &mut W) -> Result<(), WriteFileError>
where
@ -32,9 +52,7 @@ pub trait Store {
fn sync(&self, descr: &Descr, limits: Limits) -> Result<(), SyncError>;
fn all_descrs(
&self,
) -> Result<Box<dyn Iterator<Item = Result<Descr, AllDescrsError>> + '_>, AllDescrsError>;
fn all_descrs(&self) -> AllDescrsResult<Box<dyn Iterator<Item = AllDescrsResult<Descr>> + '_>>;
}
pub mod git {
@ -43,24 +61,8 @@ pub mod git {
use std::path::{Path, PathBuf};
use std::{fs, io};
use gix::clone::Error as gixCloneErr;
use gix::progress::Discard;
// Catching error from gix::prepare_clone_bare
impl From<gixCloneErr> for SyncError {
fn from(e: gixCloneErr) -> SyncError {
match e {
gixCloneErr::Init(gix::init::Error::InvalidBranchName { .. }) => {
SyncError::InvalidBranchName
}
gixCloneErr::UrlParse(_) | gixCloneErr::CanonicalizeUrl { .. } => {
SyncError::InvalidURL
}
_ => SyncError::Unexpected(Box::from(e)),
}
}
}
pub struct Store<'a> {
dir_path: &'a Path,
}
@ -93,47 +95,51 @@ pub mod git {
// if the path doesn't exist then use the gix clone feature to clone it into the
// directory.
if fs::read_dir(repo_path).is_err() {
fs::create_dir_all(repo_path).map_err(|e| SyncError::Unexpected(Box::from(e)))?;
fs::create_dir_all(repo_path)?;
let Descr::Git { url, branch_name } = descr;
let (repo, _) = gix::prepare_clone_bare(url.clone(), repo_path)?
use gix::clone::Error as gixCloneErr;
let (repo, _) = gix::prepare_clone_bare(url.clone(), repo_path)
.map_err(|e| match e {
gixCloneErr::Init(gix::init::Error::InvalidBranchName { .. }) => {
SyncError::InvalidBranchName
}
gixCloneErr::UrlParse(_) | gixCloneErr::CanonicalizeUrl { .. } => {
SyncError::InvalidURL
}
_ => SyncError::Unexpected(Box::from(e)),
})?
.fetch_only(Discard, should_interrupt)
.map_err(|_| SyncError::InvalidURL)?;
// Check to make sure the branch name exists
// TODO if this fails we should delete the whole repo
repo.try_find_reference(&Self::branch_ref(branch_name))
.map_err(|e| SyncError::Unexpected(Box::from(e)))?
repo.try_find_reference(&Self::branch_ref(branch_name))?
.ok_or(SyncError::InvalidBranchName)?;
// Add the descr to the repo directory, so we can know the actual descr later
// TODO if this fails we should delete the whole repo
let descr_file = fs::File::create(self.descr_file_path(descr.id().as_ref()))
.map_err(|e| SyncError::Unexpected(Box::from(e)))?;
let descr_file = fs::File::create(self.descr_file_path(descr.id().as_ref()))?;
serde_json::to_writer(descr_file, &descr)
.map_err(|e| SyncError::Unexpected(Box::from(e)))?;
serde_json::to_writer(descr_file, &descr)?;
return Ok(());
}
let direction = gix::remote::Direction::Fetch;
let repo = gix::open(repo_path).map_err(|e| SyncError::Unexpected(Box::from(e)))?;
let repo = gix::open(repo_path)?;
let remote = repo
.find_default_remote(direction)
.ok_or_else(|| SyncError::Unexpected(Box::from("no default configured")))?
.map_err(|e| SyncError::Unexpected(Box::from(e)))?;
.ok_or_else(|| SyncError::Unexpected(Box::from("no default configured")))??;
remote
.connect(direction)
.map_err(|e| SyncError::Unexpected(Box::from(e)))?
.prepare_fetch(Discard, Default::default())
.map_err(|e| SyncError::Unexpected(Box::from(e)))?
.receive(Discard, should_interrupt)
.map_err(|e| SyncError::Unexpected(Box::from(e)))?;
.connect(direction)?
.prepare_fetch(Discard, Default::default())?
.receive(Discard, should_interrupt)?;
Ok(())
}
@ -154,69 +160,51 @@ pub mod git {
let mut clean_path = Path::new(path);
clean_path = clean_path.strip_prefix("/").unwrap_or(clean_path);
let repo =
gix::open(repo_path).map_err(|e| WriteFileError::Unexpected(Box::from(e)))?;
let repo = gix::open(repo_path)?;
let commit_object_id = repo
.find_reference(branch_ref)
.map_err(|e| WriteFileError::Unexpected(Box::from(e)))?
.peel_to_id_in_place()
.map_err(|e| WriteFileError::Unexpected(Box::from(e)))?;
let commit_object_id = repo.find_reference(branch_ref)?.peel_to_id_in_place()?;
let tree_object_id = repo
.find_object(commit_object_id)
.map_err(|e| WriteFileError::Unexpected(Box::from(e)))?
.try_to_commit_ref()
.map_err(|e| WriteFileError::Unexpected(Box::from(e)))?
.find_object(commit_object_id)?
.try_to_commit_ref()?
.tree();
let file_object = repo
.find_object(tree_object_id)
.map_err(|e| WriteFileError::Unexpected(Box::from(e)))?
.peel_to_tree()
.map_err(|e| WriteFileError::Unexpected(Box::from(e)))?
.lookup_entry_by_path(clean_path)
.map_err(|e| WriteFileError::Unexpected(Box::from(e)))?
.find_object(tree_object_id)?
.peel_to_tree()?
.lookup_entry_by_path(clean_path)?
.ok_or(WriteFileError::FileNotFound)?
.object()
.map_err(|e| WriteFileError::Unexpected(Box::from(e)))?;
.object()?;
into.write_all(file_object.data.as_ref())
.map_err(|e| WriteFileError::Unexpected(Box::from(e)))?;
into.write_all(file_object.data.as_ref())?;
Ok(())
}
fn all_descrs(
&self,
) -> Result<Box<dyn Iterator<Item = Result<Descr, AllDescrsError>> + '_>, AllDescrsError>
{
let inner_iter = fs::read_dir(self.dir_path)
.map_err(|e| AllDescrsError::Unexpected(Box::from(e)))?
.map(
|dir_entry_res: io::Result<fs::DirEntry>| -> Result<Descr, AllDescrsError> {
let descr_id: String = dir_entry_res
.map_err(|e| AllDescrsError::Unexpected(Box::from(e)))?
.file_name()
.to_str()
.ok_or_else(|| {
AllDescrsError::Unexpected(Box::from(
"couldn't convert os string to &str",
))
})?
.into();
) -> AllDescrsResult<Box<dyn Iterator<Item = 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?
.file_name()
.to_str()
.ok_or_else(|| {
AllDescrsError::Unexpected(Box::from(
"couldn't convert os string to &str",
))
})?
.into();
let descr_file_path = self.descr_file_path(descr_id.as_ref());
let descr_file_path = self.descr_file_path(descr_id.as_ref());
let descr_file = fs::File::open(descr_file_path)
.map_err(|e| AllDescrsError::Unexpected(Box::from(e)))?;
let descr_file = fs::File::open(descr_file_path)?;
let descr = serde_json::from_reader(descr_file)
.map_err(|e| AllDescrsError::Unexpected(Box::from(e)))?;
let descr = serde_json::from_reader(descr_file)?;
Ok(descr)
},
);
Ok(descr)
},
);
Ok(Box::from(inner_iter))
}