mirror of
https://seed.radicle.dev/rad:z6cFWeWpnZNHh9rUW8phgA3b5yGt.git
synced 2026-11-21 18:40:40 +08:00
radicle-surf: reorganise errors
The current `error::Error` type aggregated a lot of different error scenarios that were not for the actual repo actions, instead they would be for the domains that the repo actions would return. This is a reorganisation of those by localising error variants in their modules: * namespace::Error * commit::Error * glob::Error * adding `type Error` to ToCommit * adding `type Error` to Revision The main Error type now consists of these other Error types and lives in the `repo` module. It is re-exported at the top-level since RepositoryRef is the main interaction with the library. During this refactor, some reorganisation of functions was done as well: * Move `get_file` from `Commit` to `RepositoryRef` as `get_commit_file`. * Have a blanket implementation of `ToCommit` for any `Revision`, instead of repeating the implementations. Signed-off-by: Fintan Halpenny <fintan.halpenny@gmail.com> X-Clacks-Overhead: GNU Terry Pratchett
This commit is contained in:
+62
-22
@@ -63,7 +63,7 @@
|
||||
//! # }
|
||||
//! ```
|
||||
|
||||
use std::str::FromStr;
|
||||
use std::{convert::Infallible, str::FromStr};
|
||||
|
||||
// Re-export git2 as sub-module
|
||||
pub use git2::{self, Error as Git2Error, Time};
|
||||
@@ -71,17 +71,14 @@ use git_ref_format::{name::Components, Component, Qualified, RefString};
|
||||
pub use radicle_git_ext::Oid;
|
||||
|
||||
mod repo;
|
||||
pub use repo::{Repository, RepositoryRef};
|
||||
pub use repo::{Error, Repository, RepositoryRef};
|
||||
|
||||
mod glob;
|
||||
pub mod glob;
|
||||
pub use glob::Glob;
|
||||
|
||||
mod history;
|
||||
pub use history::History;
|
||||
|
||||
pub mod error;
|
||||
pub use error::Error;
|
||||
|
||||
/// Provides the data for talking about branches.
|
||||
pub mod branch;
|
||||
pub use branch::{Branch, Local, Remote};
|
||||
@@ -116,57 +113,100 @@ impl From<git2::Buf> for Signature {
|
||||
|
||||
/// Supports various ways to specify a revision used in Git.
|
||||
pub trait Revision {
|
||||
type Error: std::error::Error + Send + Sync + 'static;
|
||||
|
||||
/// Returns the object id of this revision in `repo`.
|
||||
fn object_id(&self, repo: &RepositoryRef) -> Result<Oid, Error>;
|
||||
fn object_id(&self, repo: &RepositoryRef) -> Result<Oid, Self::Error>;
|
||||
}
|
||||
|
||||
impl Revision for RefString {
|
||||
fn object_id(&self, repo: &RepositoryRef) -> Result<Oid, Error> {
|
||||
repo.refname_to_oid(self.as_str())
|
||||
type Error = git2::Error;
|
||||
|
||||
fn object_id(&self, repo: &RepositoryRef) -> Result<Oid, Self::Error> {
|
||||
repo.repo_ref.refname_to_id(self.as_str()).map(Oid::from)
|
||||
}
|
||||
}
|
||||
|
||||
impl Revision for &RefString {
|
||||
fn object_id(&self, repo: &RepositoryRef) -> Result<Oid, Error> {
|
||||
repo.refname_to_oid(self.as_str())
|
||||
type Error = git2::Error;
|
||||
|
||||
fn object_id(&self, repo: &RepositoryRef) -> Result<Oid, Self::Error> {
|
||||
repo.repo_ref.refname_to_id(self.as_str()).map(Oid::from)
|
||||
}
|
||||
}
|
||||
|
||||
impl Revision for Qualified<'_> {
|
||||
fn object_id(&self, repo: &RepositoryRef) -> Result<Oid, Error> {
|
||||
repo.refname_to_oid(self.as_str())
|
||||
type Error = git2::Error;
|
||||
|
||||
fn object_id(&self, repo: &RepositoryRef) -> Result<Oid, Self::Error> {
|
||||
repo.repo_ref.refname_to_id(self.as_str()).map(Oid::from)
|
||||
}
|
||||
}
|
||||
|
||||
impl Revision for &Qualified<'_> {
|
||||
fn object_id(&self, repo: &RepositoryRef) -> Result<Oid, Error> {
|
||||
repo.refname_to_oid(self.as_str())
|
||||
type Error = git2::Error;
|
||||
|
||||
fn object_id(&self, repo: &RepositoryRef) -> Result<Oid, Self::Error> {
|
||||
repo.repo_ref.refname_to_id(self.as_str()).map(Oid::from)
|
||||
}
|
||||
}
|
||||
|
||||
impl Revision for Oid {
|
||||
fn object_id(&self, _repo: &RepositoryRef) -> Result<Oid, Error> {
|
||||
type Error = Infallible;
|
||||
|
||||
fn object_id(&self, _repo: &RepositoryRef) -> Result<Oid, Self::Error> {
|
||||
Ok(*self)
|
||||
}
|
||||
}
|
||||
|
||||
impl Revision for &str {
|
||||
fn object_id(&self, _repo: &RepositoryRef) -> Result<Oid, Error> {
|
||||
Oid::from_str(self).map_err(Error::Git)
|
||||
type Error = git2::Error;
|
||||
|
||||
fn object_id(&self, _repo: &RepositoryRef) -> Result<Oid, Self::Error> {
|
||||
Oid::from_str(self).map(Oid::from)
|
||||
}
|
||||
}
|
||||
|
||||
impl Revision for &Branch {
|
||||
fn object_id(&self, repo: &RepositoryRef) -> Result<Oid, Error> {
|
||||
type Error = Error;
|
||||
|
||||
fn object_id(&self, repo: &RepositoryRef) -> Result<Oid, Self::Error> {
|
||||
let refname = repo.namespaced_refname(&self.refname())?;
|
||||
Ok(repo.repo_ref.refname_to_id(&refname).map(Oid::from)?)
|
||||
}
|
||||
}
|
||||
|
||||
impl Revision for &Tag {
|
||||
fn object_id(&self, repo: &RepositoryRef) -> Result<Oid, Error> {
|
||||
let refname = repo.namespaced_refname(&self.refname())?;
|
||||
Ok(repo.repo_ref.refname_to_id(&refname).map(Oid::from)?)
|
||||
type Error = Infallible;
|
||||
|
||||
fn object_id(&self, _repo: &RepositoryRef) -> Result<Oid, Self::Error> {
|
||||
Ok(self.id())
|
||||
}
|
||||
}
|
||||
|
||||
/// A common trait for anything that can convert to a `Commit`.
|
||||
pub trait ToCommit {
|
||||
type Error: std::error::Error + Send + Sync + 'static;
|
||||
|
||||
/// Converts to a commit in `repo`.
|
||||
fn to_commit(self, repo: &RepositoryRef) -> Result<Commit, Self::Error>;
|
||||
}
|
||||
|
||||
impl ToCommit for Commit {
|
||||
type Error = Infallible;
|
||||
|
||||
fn to_commit(self, _repo: &RepositoryRef) -> Result<Commit, Self::Error> {
|
||||
Ok(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: Revision> ToCommit for R {
|
||||
type Error = Error;
|
||||
|
||||
fn to_commit(self, repo: &RepositoryRef) -> Result<Commit, Self::Error> {
|
||||
let oid = repo.object_id(&self)?;
|
||||
let commit = repo.repo_ref.find_commit(oid.into())?;
|
||||
Ok(Commit::try_from(commit)?)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user