radicle: refactor Canonical

Changes for the `Canonical` type:
- Reorder arguments so that `delegates` and `threshold` are grouped
  after `refname`
- Add `Canonical::is_empty` to detect when `Canonical` was constructed
  with no tips
- Consume `Canonical` when calling `Canonical::quorum` to indicate
  that it should be the final step
This commit is contained in:
Fintan Halpenny 2024-08-14 13:26:51 +01:00
parent b2bcd561cf
commit ab62dce683
1 changed files with 13 additions and 5 deletions

View File

@ -99,8 +99,8 @@ impl Canonical {
{
Self::reference(
repo,
delegates,
&lit::refs_heads(project.default_branch()).into(),
delegates,
threshold,
)
}
@ -109,8 +109,8 @@ impl Canonical {
/// the reference `name`.
pub fn reference<S>(
repo: &S,
refname: &Qualified,
delegates: &NonEmpty<Did>,
name: &Qualified,
threshold: usize,
) -> Result<Self, raw::Error>
where
@ -118,14 +118,14 @@ impl Canonical {
{
let mut tips = BTreeMap::new();
for delegate in delegates.iter() {
match repo.reference_oid(delegate, name) {
match repo.reference_oid(delegate, refname) {
Ok(tip) => {
tips.insert(*delegate, tip);
}
Err(e) if super::ext::is_not_found_err(&e) => {
log::warn!(
target: "radicle",
"Missing `refs/namespaces/{}/{name}` while calculating the canonical reference",
"Missing `refs/namespaces/{}/{refname}` while calculating the canonical reference",
delegate.as_key()
);
}
@ -139,6 +139,14 @@ impl Canonical {
pub fn tips(&self) -> impl Iterator<Item = (&Did, &Oid)> {
self.tips.iter()
}
/// Returns `true` is there were no tips found for any of the delegates for
/// the given reference.
///
/// N.b. this may be the case when a new reference is being created.
pub fn is_empty(&self) -> bool {
self.tips.is_empty()
}
}
/// Check that a given `target` converges with any of the provided `tips`.
@ -176,7 +184,7 @@ impl Canonical {
///
/// Also returns an error if `heads` is empty or `threshold` cannot be
/// satisified with the number of heads given.
pub fn quorum(&self, repo: &raw::Repository) -> Result<Oid, QuorumError> {
pub fn quorum(self, repo: &raw::Repository) -> Result<Oid, QuorumError> {
let mut candidates = BTreeMap::<_, usize>::new();
// Build a list of candidate commits and count how many "votes" each of them has.