Radish alpha
r
rad:z2UcCU1LgMshWvXj6hXSDDrwB8q8M
Radicle Job Collaborative Object
Radicle
Git
refactor: introduce `JobId`
Fintan Halpenny committed 10 months ago
commit 1ce12295c23c773b8f828326e3bc77bc66788c30
parent e917ef0
1 file changed +55 -11
modified src/lib.rs
@@ -50,6 +50,7 @@
#![deny(missing_docs)]

use std::collections::HashMap;
+
use std::fmt;
use std::ops::{Deref, DerefMut};
use std::str::FromStr;

@@ -74,6 +75,48 @@ pub mod error;
pub static TYPENAME: Lazy<TypeName> =
    Lazy::new(|| FromStr::from_str("xyz.radworks.job").expect("type name is valid"));

+
/// The identifier for a given [`Job`] collaborative object.
+
///
+
/// When a [`Job`] is created, through [`Jobs::create`], the identifier is
+
/// also returned as part of [`JobMut::id`].
+
///
+
/// Identifiers can be used to retrieve a [`Job`] or [`JobMut`] through
+
/// [`Jobs::get`] and [`Jobs::get_mut`], respectively.
+
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
+
pub struct JobId(ObjectId);
+

+
impl JobId {
+
    fn as_object_id(&self) -> &ObjectId {
+
        &self.0
+
    }
+
}
+

+
impl fmt::Display for JobId {
+
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+
        f.write_str(&self.0.to_string())
+
    }
+
}
+

+
impl FromStr for JobId {
+
    type Err = <ObjectId as FromStr>::Err;
+

+
    fn from_str(s: &str) -> Result<Self, Self::Err> {
+
        ObjectId::from_str(s).map(Self)
+
    }
+
}
+

+
impl From<JobId> for ObjectId {
+
    fn from(JobId(oid): JobId) -> Self {
+
        oid
+
    }
+
}
+

+
impl From<ObjectId> for JobId {
+
    fn from(oid: ObjectId) -> Self {
+
        Self(oid)
+
    }
+
}
+

/// A `Job` describes a generic task run for a given commit [`Oid`] by a set of
/// nodes.
///
@@ -556,8 +599,8 @@ where
    }

    /// Get a [`Job`].
-
    pub fn get(&self, id: &ObjectId) -> Result<Option<Job>, store::Error> {
-
        self.raw.get(id)
+
    pub fn get(&self, id: &JobId) -> Result<Option<Job>, store::Error> {
+
        self.raw.get(id.as_object_id())
    }
}

@@ -566,11 +609,11 @@ where
    R: ReadRepository + SignRepository + cob::Store<Namespace = NodeId>,
{
    /// Get a [`JobMut`].
-
    pub fn get_mut<'g, C>(&'g mut self, id: &ObjectId) -> Result<JobMut<'a, 'g, R>, store::Error> {
+
    pub fn get_mut<'g, C>(&'g mut self, id: &JobId) -> Result<JobMut<'a, 'g, R>, store::Error> {
        let job = self
            .raw
-
            .get(id)?
-
            .ok_or_else(move || store::Error::NotFound(TYPENAME.clone(), *id))?;
+
            .get(id.as_object_id())?
+
            .ok_or_else(move || store::Error::NotFound(TYPENAME.clone(), (*id).into()))?;

        Ok(JobMut {
            id: *id,
@@ -599,7 +642,7 @@ where
        )?;

        Ok(JobMut {
-
            id,
+
            id: id.into(),
            job,
            store: self,
        })
@@ -610,7 +653,7 @@ where
/// applying actions to it.
pub struct JobMut<'a, 'g, R> {
    /// Git object that the [`Job`] applies to.
-
    pub id: ObjectId,
+
    pub id: JobId,

    job: Job,
    store: &'g mut Jobs<'a, R>,
@@ -629,12 +672,12 @@ where
    R: WriteRepository + cob::Store<Namespace = NodeId>,
{
    /// Create a new `JobMut`.
-
    pub fn new(id: ObjectId, job: Job, store: &'g mut Jobs<'a, R>) -> Self {
+
    pub fn new(id: JobId, job: Job, store: &'g mut Jobs<'a, R>) -> Self {
        Self { id, job, store }
    }

    /// The COB identifier for the underlying [`Job`].
-
    pub fn id(&self) -> &ObjectId {
+
    pub fn id(&self) -> &JobId {
        &self.id
    }

@@ -643,7 +686,7 @@ where
        self.job = self
            .store
            .get(&self.id)?
-
            .ok_or_else(|| store::Error::NotFound(TYPENAME.clone(), self.id))?;
+
            .ok_or_else(|| store::Error::NotFound(TYPENAME.clone(), *self.id.as_object_id()))?;

        Ok(())
    }
@@ -690,7 +733,8 @@ where
        let mut tx = Transaction::default();
        operations(&mut tx)?;

-
        let (job, commit) = tx.0.commit(message, self.id, &mut self.store.raw, signer)?;
+
        let (job, commit) =
+
            tx.0.commit(message, self.id.into(), &mut self.store.raw, signer)?;
        self.job = job;

        Ok(commit)