Radish alpha
r
rad:zwTxygwuz5LDGBq255RA2CbNGrz8
Radicle CI broker
Radicle
Git
feat! in ErgoError, hide types from dependencies
Merged liw opened 6 months ago

The should be no need for callers to know the specific type of source errors, but exposing them can lead to trickier crate dependency management, so we now hide them.

1 file changed +97 -24 79955241 09d21f76
modified src/ergo.rs
@@ -28,7 +28,7 @@ impl Radicle {
    /// Create a new [`Radicle`]. This may fail.
    pub fn new() -> Result<Self, ErgoError> {
        Ok(Self {
-
            profile: Profile::load().map_err(ErgoError::LoadProfile)?,
+
            profile: Profile::load().map_err(ErgoError::load_profile)?,
        })
    }

@@ -42,7 +42,7 @@ impl Radicle {
        self.profile
            .storage
            .repositories()
-
            .map_err(ErgoError::ListRepositories)
+
            .map_err(ErgoError::list_repositories)
    }

    /// Load information about a specific repository.
@@ -50,7 +50,7 @@ impl Radicle {
        self.profile
            .storage
            .repository(*repo_id)
-
            .map_err(|err| ErgoError::LoadRepo(*repo_id, Box::new(err)))
+
            .map_err(|err| ErgoError::load_repository(repo_id, err))
    }

    /// Load a repository by name, if the name is unique.
@@ -67,13 +67,13 @@ impl Radicle {
        let matching = matching?;

        match matching[..] {
-
            [] => Err(ErgoError::NoRepositoryWithName(wanted.to_string())),
+
            [] => Err(ErgoError::no_repository_with_name(wanted)),
            [_] => {
                let repo_id = matching[0];
                let repo = self.repository(&repo_id)?;
                Ok(repo)
            }
-
            [_, _, ..] => Err(ErgoError::NameIsNotUnique(wanted.to_string())),
+
            [_, _, ..] => Err(ErgoError::name_is_not_unique(wanted)),
        }
    }

@@ -82,7 +82,7 @@ impl Radicle {
    pub fn project(&self, repo_id: &RepoId) -> Result<Project, ErgoError> {
        let repo = self.repository(repo_id)?;
        repo.project()
-
            .map_err(|err| ErgoError::LoadProject(*repo_id, Box::new(err)))
+
            .map_err(|err| ErgoError::load_project(repo_id, err))
    }

    /// Load all patches in a repository.
@@ -92,13 +92,13 @@ impl Radicle {
            .profile
            .home
            .patches(&repo)
-
            .map_err(|err| ErgoError::LoadPathces(*repo_id, Box::new(err)))?;
+
            .map_err(|err| ErgoError::load_patches(repo_id, err))?;
        let mut items = vec![];
        let list = patches
            .list()
-
            .map_err(|err| ErgoError::ListCache(*repo_id, err))?;
+
            .map_err(|err| ErgoError::list_cache(repo_id, err))?;
        for result in list {
-
            let (id, patch) = result.map_err(|err| ErgoError::CacheListItem(*repo_id, err))?;
+
            let (id, patch) = result.map_err(|err| ErgoError::cache_list_item(repo_id, err))?;
            items.push((id, patch));
        }
        Ok(items)
@@ -111,11 +111,11 @@ impl Radicle {
            .profile
            .home
            .patches(&repo)
-
            .map_err(|err| ErgoError::LoadPathces(*repo_id, Box::new(err)))?;
+
            .map_err(|err| ErgoError::LoadPatches(*repo_id, Box::new(err)))?;
        patches
            .get(patch_id)
-
            .map_err(|err| ErgoError::GetPatch(*repo_id, *patch_id, Box::new(err)))?
-
            .ok_or(ErgoError::NoSuchPatch(*repo_id, *patch_id))
+
            .map_err(|err| ErgoError::get_patch(repo_id, patch_id, err))?
+
            .ok_or(ErgoError::no_such_patch(repo_id, patch_id))
    }

    /// Resolve a shortened patch ID into a full patch ID.
@@ -124,7 +124,7 @@ impl Radicle {
        let object = repo
            .backend
            .revparse_single(id)
-
            .map_err(|err| ErgoError::ResolvePatchId(id.to_string(), err))?;
+
            .map_err(|err| ErgoError::resolve_patch_id(id, err))?;
        Ok(PatchId::from(object.id()))
    }

@@ -137,7 +137,7 @@ impl Radicle {
            let object = repo
                .backend
                .revparse_single(gitref)
-
                .map_err(|err| ErgoError::ResolveCommit(gitref.to_string(), *repo_id, err))?;
+
                .map_err(|err| ErgoError::resolve_commit(gitref, repo_id, err))?;
            Ok(Oid::from(object.id()))
        }
    }
@@ -147,34 +147,42 @@ impl Radicle {
#[derive(Debug, thiserror::Error)]
pub enum ErgoError {
    #[error("failed to load Radicle profile")]
-
    LoadProfile(#[source] radicle::profile::Error),
+
    LoadProfile(#[source] Box<dyn std::error::Error + Send + Sync>),

    #[error("failed to list repositories in Radicle node storage")]
-
    ListRepositories(#[source] radicle::storage::Error),
+
    ListRepositories(#[source] Box<dyn std::error::Error + Send + Sync>),

    #[error("failed to load info from Radicle node storage for repository {0}")]
-
    LoadRepo(RepoId, #[source] Box<radicle::storage::RepositoryError>),
+
    LoadRepo(RepoId, #[source] Box<dyn std::error::Error + Send + Sync>),

    #[error("failed to load project info from Radicle node storage for repository {0}")]
-
    LoadProject(RepoId, #[source] Box<radicle::storage::RepositoryError>),
+
    LoadProject(RepoId, #[source] Box<dyn std::error::Error + Send + Sync>),

    #[error("failed to load patch list from Radicle node storage for repository {0}")]
-
    LoadPathces(RepoId, #[source] Box<radicle::profile::Error>),
+
    LoadPatches(RepoId, #[source] Box<dyn std::error::Error + Send + Sync>),

    #[error("failed to list patches for repository {0}")]
-
    ListCache(RepoId, #[source] radicle::patch::cache::Error),
+
    ListCache(RepoId, #[source] Box<dyn std::error::Error + Send + Sync>),

    #[error("failed to list info for patch {0}")]
-
    CacheListItem(RepoId, #[source] radicle::patch::cache::Error),
+
    CacheListItem(RepoId, #[source] Box<dyn std::error::Error + Send + Sync>),

    #[error("failed to resolve patch id {0:?} in repository {1}")]
-
    ResolvePatchId(String, #[source] radicle::storage::git::raw::Error),
+
    ResolvePatchId(String, #[source] Box<dyn std::error::Error + Send + Sync>),

    #[error("failed to resolve commit id {0:?} in repository {1}")]
-
    ResolveCommit(String, RepoId, #[source] radicle::storage::git::raw::Error),
+
    ResolveCommit(
+
        String,
+
        RepoId,
+
        #[source] Box<dyn std::error::Error + Send + Sync>,
+
    ),

    #[error("failed to load patch {1} from Radicle node storage for repository {0}")]
-
    GetPatch(RepoId, PatchId, #[source] Box<radicle::patch::cache::Error>),
+
    GetPatch(
+
        RepoId,
+
        PatchId,
+
        #[source] Box<dyn std::error::Error + Send + Sync>,
+
    ),

    #[error("no patch {1} in repository {0}")]
    NoSuchPatch(RepoId, PatchId),
@@ -185,3 +193,68 @@ pub enum ErgoError {
    #[error("repository name is not unique: {0:?}")]
    NameIsNotUnique(String),
}
+

+
impl ErgoError {
+
    pub(crate) fn load_profile(err: radicle::profile::Error) -> Self {
+
        Self::LoadProfile(Box::new(err))
+
    }
+

+
    pub(crate) fn list_repositories(err: radicle::storage::Error) -> Self {
+
        Self::ListRepositories(Box::new(err))
+
    }
+

+
    pub(crate) fn load_repository(id: &RepoId, err: radicle::storage::RepositoryError) -> Self {
+
        Self::LoadRepo(*id, Box::new(err))
+
    }
+

+
    pub(crate) fn load_project(id: &RepoId, err: radicle::storage::RepositoryError) -> Self {
+
        Self::LoadRepo(*id, Box::new(err))
+
    }
+

+
    pub(crate) fn load_patches(id: &RepoId, err: radicle::profile::Error) -> Self {
+
        Self::LoadPatches(*id, Box::new(err))
+
    }
+

+
    pub(crate) fn list_cache(id: &RepoId, err: radicle::patch::cache::Error) -> Self {
+
        Self::ListCache(*id, Box::new(err))
+
    }
+

+
    pub(crate) fn cache_list_item(id: &RepoId, err: radicle::patch::cache::Error) -> Self {
+
        Self::CacheListItem(*id, Box::new(err))
+
    }
+

+
    pub(crate) fn resolve_patch_id<S: Into<String>>(
+
        id: S,
+
        err: radicle::storage::git::raw::Error,
+
    ) -> Self {
+
        Self::ResolvePatchId(id.into(), Box::new(err))
+
    }
+

+
    pub(crate) fn resolve_commit<S: Into<String>>(
+
        commit: S,
+
        repo: &RepoId,
+
        err: radicle::storage::git::raw::Error,
+
    ) -> Self {
+
        Self::ResolveCommit(commit.into(), *repo, Box::new(err))
+
    }
+

+
    pub(crate) fn get_patch(
+
        repo: &RepoId,
+
        patch: &PatchId,
+
        err: radicle::patch::cache::Error,
+
    ) -> Self {
+
        Self::GetPatch(*repo, *patch, Box::new(err))
+
    }
+

+
    pub(crate) fn no_such_patch(repo: &RepoId, patch: &PatchId) -> Self {
+
        Self::NoSuchPatch(*repo, *patch)
+
    }
+

+
    pub(crate) fn no_repository_with_name<S: Into<String>>(name: S) -> Self {
+
        Self::NoRepositoryWithName(name.into())
+
    }
+

+
    pub(crate) fn name_is_not_unique<S: Into<String>>(name: S) -> Self {
+
        Self::NameIsNotUnique(name.into())
+
    }
+
}