Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
radicle: move canonical errors into module
Fintan Halpenny committed 10 months ago
commit d7d1ac66c561e5b5ada6fdce74e0e73ccf518969
parent f05ffcad7b07fb25831317cc1afa51f645a32d63
7 files changed +138 -135
modified crates/radicle-node/src/worker/fetch.rs
@@ -122,9 +122,9 @@ impl Handle {
                            log::trace!(target: "worker", "Set HEAD to {}", head.new);
                        }
                    }
-
                    Err(RepositoryError::Quorum(radicle::git::canonical::QuorumError::Git(e))) => {
-
                        return Err(e.into())
-
                    }
+
                    Err(RepositoryError::Quorum(
+
                        radicle::git::canonical::error::QuorumError::Git(e),
+
                    )) => return Err(e.into()),
                    Err(RepositoryError::Quorum(e)) => {
                        log::warn!(target: "worker", "Fetch could not set HEAD: {e}")
                    }
modified crates/radicle-remote-helper/src/push.rs
@@ -113,7 +113,7 @@ pub enum Error {
    Repository(#[from] radicle::storage::RepositoryError),
    /// Quorum error.
    #[error(transparent)]
-
    Quorum(#[from] radicle::git::canonical::QuorumError),
+
    Quorum(#[from] radicle::git::canonical::error::QuorumError),
    #[error(transparent)]
    CanonicalRefs(#[from] radicle::identity::doc::CanonicalRefsError),
    #[error(transparent)]
modified crates/radicle-remote-helper/src/push/canonical.rs
@@ -85,7 +85,7 @@ impl<'a, 'b> Canonical<'a, 'b> {

pub(crate) mod io {
    use radicle::git;
-
    use radicle::git::canonical;
+
    use radicle::git::canonical::error::QuorumError;

    use crate::push::error;
    use crate::{hint, warn};
@@ -115,29 +115,27 @@ pub(crate) mod io {
                Err(e.into())
            }
            error::Canonical::Quorum(e) => match e {
-
                e @ canonical::QuorumError::DivergingCommits { .. } => {
+
                e @ QuorumError::DivergingCommits { .. } => {
                    warn(e.to_string());
                    warn("it is recommended to find a commit to agree upon");
                    Ok(())
                }
-
                e @ canonical::QuorumError::DivergingTags { .. } => {
+
                e @ QuorumError::DivergingTags { .. } => {
                    warn(e.to_string());
                    warn("it is recommended to find a tag to agree upon");
                    Ok(())
                }
-
                e @ canonical::QuorumError::DifferentTypes { .. } => {
+
                e @ QuorumError::DifferentTypes { .. } => {
                    warn(e.to_string());
                    warn("it is recommended to find an object type (either commit or tag) to agree upon");
                    Ok(())
                }
-
                e @ canonical::QuorumError::NoCandidates { .. } => {
+
                e @ QuorumError::NoCandidates { .. } => {
                    warn(e.to_string());
                    warn("it is recommended to find a commit to agree upon");
                    Ok(())
                }
-
                canonical::QuorumError::Git(err) => {
-
                    Err(error::CanonicalUnrecoverable::Git { source: err })
-
                }
+
                QuorumError::Git(err) => Err(error::CanonicalUnrecoverable::Git { source: err }),
            },
        }
    }
modified crates/radicle-remote-helper/src/push/error.rs
@@ -7,7 +7,7 @@ pub enum CanonicalUnrecoverable {
    #[error(transparent)]
    GraphDescendant(#[from] GraphDescendant),
    #[error(transparent)]
-
    Converges(#[from] canonical::ConvergesError),
+
    Converges(#[from] canonical::error::ConvergesError),
    #[error(transparent)]
    HeadsDiverge(#[from] HeadsDiverge),
    #[error("failure while computing canonical reference: {source}")]
@@ -19,11 +19,11 @@ pub enum Canonical {
    #[error(transparent)]
    GraphDescendant(GraphDescendant),
    #[error(transparent)]
-
    Converges(#[from] canonical::ConvergesError),
+
    Converges(#[from] canonical::error::ConvergesError),
    #[error(transparent)]
    HeadsDiverge(HeadsDiverge),
    #[error(transparent)]
-
    Quorum(#[from] canonical::QuorumError),
+
    Quorum(#[from] canonical::error::QuorumError),
}

impl Canonical {
modified crates/radicle/src/git/canonical.rs
@@ -1,13 +1,13 @@
+
pub mod error;
+
use error::*;
pub mod rules;
pub use rules::{MatchedRule, RawRule, Rules, ValidRule};

use std::cell::Cell;
use std::collections::BTreeMap;
-
use std::path::PathBuf;

use raw::ObjectType;
use raw::Repository;
-
use thiserror::Error;

use crate::prelude::Did;

@@ -32,123 +32,6 @@ pub struct Canonical<'a, 'b> {
    tips: BTreeMap<Did, (Oid, git2::ObjectType)>,
}

-
/// Error that can occur when calculation the [`Canonical::quorum`].
-
#[derive(Debug, Error)]
-
pub enum QuorumError {
-
    /// Could not determine a quorum [`Oid`], due to diverging tips.
-
    #[error("could not determine target commit for canonical reference '{refname}', found diverging commits {longest} and {head}, with base commit {base} and threshold {threshold}")]
-
    DivergingCommits {
-
        refname: String,
-
        threshold: usize,
-
        base: Oid,
-
        longest: Oid,
-
        head: Oid,
-
    },
-
    #[error("could not determine target tag for canonical reference '{refname}', found multiple candidates with threshold {threshold}")]
-
    DivergingTags {
-
        refname: String,
-
        threshold: usize,
-
        candidates: Vec<Oid>,
-
    },
-
    #[error("could not determine target for canonical reference '{refname}', found objects of different types")]
-
    DifferentTypes { refname: String },
-
    /// Could not determine a base candidate from the given set of delegates.
-
    #[error("could not determine target for canonical reference '{refname}', no object with at least {threshold} vote(s) found (threshold not met)")]
-
    NoCandidates { refname: String, threshold: usize },
-
    /// An error occurred from [`git2`].
-
    #[error(transparent)]
-
    Git(#[from] git2::Error),
-
}
-

-
#[derive(Debug, Error)]
-
#[error("failed to check if {head} is an ancestor of {canonical} due to: {source}")]
-
pub struct GraphDescendant {
-
    head: Oid,
-
    canonical: Oid,
-
    source: raw::Error,
-
}
-

-
#[derive(Debug, Error)]
-
#[error("the commit {commit} for {did} is missing from the repository {repo:?}")]
-
pub struct MissingObject {
-
    repo: PathBuf,
-
    did: Did,
-
    commit: Oid,
-
    source: raw::Error,
-
}
-

-
#[derive(Debug, Error)]
-
#[error("could not determine whether the commit {commit} for {did} is part of the repository {repo:?} due to: {source}")]
-
pub struct InvalidObject {
-
    repo: PathBuf,
-
    did: Did,
-
    commit: Oid,
-
    source: raw::Error,
-
}
-

-
#[derive(Debug, Error)]
-
#[error("the object {oid} for {did} in the repository {repo:?} is of unexpected type {kind:?}")]
-
pub struct InvalidObjectType {
-
    repo: PathBuf,
-
    did: Did,
-
    oid: Oid,
-
    kind: Option<git2::ObjectType>,
-
}
-

-
#[derive(Debug, Error)]
-
pub enum ConvergesError {
-
    #[error(transparent)]
-
    GraphDescendant(#[from] GraphDescendant),
-
    #[error(transparent)]
-
    MissingObject(#[from] MissingObject),
-
    #[error(transparent)]
-
    InvalidObject(#[from] InvalidObject),
-
    #[error(transparent)]
-
    InvalidObjectType(#[from] InvalidObjectType),
-
}
-

-
impl ConvergesError {
-
    pub fn graph_descendant(head: Oid, canonical: Oid, source: raw::Error) -> Self {
-
        Self::GraphDescendant(GraphDescendant {
-
            head,
-
            canonical,
-
            source,
-
        })
-
    }
-

-
    pub fn missing_object(repo: PathBuf, did: Did, commit: Oid, err: raw::Error) -> Self {
-
        Self::MissingObject(MissingObject {
-
            repo,
-
            did,
-
            commit,
-
            source: err,
-
        })
-
    }
-

-
    pub fn invalid_object(repo: PathBuf, did: Did, commit: Oid, err: raw::Error) -> Self {
-
        Self::InvalidObject(InvalidObject {
-
            repo,
-
            did,
-
            commit,
-
            source: err,
-
        })
-
    }
-

-
    pub fn invalid_object_kind(
-
        repo: PathBuf,
-
        did: Did,
-
        oid: Oid,
-
        kind: Option<git2::ObjectType>,
-
    ) -> Self {
-
        Self::InvalidObjectType(InvalidObjectType {
-
            repo,
-
            did,
-
            oid,
-
            kind,
-
        })
-
    }
-
}
-

impl<'a, 'b> Canonical<'a, 'b> {
    /// Construct the set of canonical tips given for the given `rule` and
    /// the reference `refname`.
added crates/radicle/src/git/canonical/error.rs
@@ -0,0 +1,122 @@
+
use std::path::PathBuf;
+

+
use thiserror::Error;
+

+
use crate::{git::raw, git::Oid, prelude::Did};
+

+
/// Error that can occur when calculation the [`Canonical::quorum`].
+
#[derive(Debug, Error)]
+
pub enum QuorumError {
+
    /// Could not determine a quorum [`Oid`], due to diverging tips.
+
    #[error("could not determine target commit for canonical reference '{refname}', found diverging commits {longest} and {head}, with base commit {base} and threshold {threshold}")]
+
    DivergingCommits {
+
        refname: String,
+
        threshold: usize,
+
        base: Oid,
+
        longest: Oid,
+
        head: Oid,
+
    },
+
    #[error("could not determine target tag for canonical reference '{refname}', found multiple candidates with threshold {threshold}")]
+
    DivergingTags {
+
        refname: String,
+
        threshold: usize,
+
        candidates: Vec<Oid>,
+
    },
+
    #[error("could not determine target for canonical reference '{refname}', found objects of different types")]
+
    DifferentTypes { refname: String },
+
    /// Could not determine a base candidate from the given set of delegates.
+
    #[error("could not determine target for canonical reference '{refname}', no object with at least {threshold} vote(s) found (threshold not met)")]
+
    NoCandidates { refname: String, threshold: usize },
+
    /// An error occurred from [`git2`].
+
    #[error(transparent)]
+
    Git(#[from] git2::Error),
+
}
+

+
#[derive(Debug, Error)]
+
#[error("failed to check if {head} is an ancestor of {canonical} due to: {source}")]
+
pub struct GraphDescendant {
+
    head: Oid,
+
    canonical: Oid,
+
    source: raw::Error,
+
}
+

+
#[derive(Debug, Error)]
+
#[error("the commit {commit} for {did} is missing from the repository {repo:?}")]
+
pub struct MissingObject {
+
    repo: PathBuf,
+
    did: Did,
+
    commit: Oid,
+
    source: raw::Error,
+
}
+

+
#[derive(Debug, Error)]
+
#[error("could not determine whether the commit {commit} for {did} is part of the repository {repo:?} due to: {source}")]
+
pub struct InvalidObject {
+
    repo: PathBuf,
+
    did: Did,
+
    commit: Oid,
+
    source: raw::Error,
+
}
+

+
#[derive(Debug, Error)]
+
#[error("the object {oid} for {did} in the repository {repo:?} is of unexpected type {kind:?}")]
+
pub struct InvalidObjectType {
+
    repo: PathBuf,
+
    did: Did,
+
    oid: Oid,
+
    kind: Option<git2::ObjectType>,
+
}
+

+
#[derive(Debug, Error)]
+
pub enum ConvergesError {
+
    #[error(transparent)]
+
    GraphDescendant(#[from] GraphDescendant),
+
    #[error(transparent)]
+
    MissingObject(#[from] MissingObject),
+
    #[error(transparent)]
+
    InvalidObject(#[from] InvalidObject),
+
    #[error(transparent)]
+
    InvalidObjectType(#[from] InvalidObjectType),
+
}
+

+
impl ConvergesError {
+
    pub(super) fn graph_descendant(head: Oid, canonical: Oid, source: raw::Error) -> Self {
+
        Self::GraphDescendant(GraphDescendant {
+
            head,
+
            canonical,
+
            source,
+
        })
+
    }
+

+
    pub(super) fn missing_object(repo: PathBuf, did: Did, commit: Oid, err: raw::Error) -> Self {
+
        Self::MissingObject(MissingObject {
+
            repo,
+
            did,
+
            commit,
+
            source: err,
+
        })
+
    }
+

+
    pub(super) fn invalid_object(repo: PathBuf, did: Did, commit: Oid, err: raw::Error) -> Self {
+
        Self::InvalidObject(InvalidObject {
+
            repo,
+
            did,
+
            commit,
+
            source: err,
+
        })
+
    }
+

+
    pub(super) fn invalid_object_kind(
+
        repo: PathBuf,
+
        did: Did,
+
        oid: Oid,
+
        kind: Option<git2::ObjectType>,
+
    ) -> Self {
+
        Self::InvalidObjectType(InvalidObjectType {
+
            repo,
+
            did,
+
            oid,
+
            kind,
+
        })
+
    }
+
}
modified crates/radicle/src/storage.rs
@@ -117,7 +117,7 @@ pub enum RepositoryError {
    #[error(transparent)]
    GitExt(#[from] git_ext::Error),
    #[error(transparent)]
-
    Quorum(#[from] canonical::QuorumError),
+
    Quorum(#[from] canonical::error::QuorumError),
    #[error(transparent)]
    Refs(#[from] refs::Error),
    #[error("missing canonical reference rule for default branch")]