radicle: introduce cob::TypedId
The combination of a COB's `TypeName` and `ObjectId` is useful for parsing a Radicle COB refname, i.e. `refs/cobs/<typename>/<object id>`. Introduce `TypedId` for this and provide helper functions for parsing from the git refname types, as well as being able to tell what kind of COB it is. Signed-off-by: Fintan Halpenny <fintan.halpenny@gmail.com> X-Clacks-Overhead: GNU Terry Pratchett
This commit is contained in:
parent
8728d02c70
commit
25ff5876ec
|
|
@ -18,3 +18,48 @@ pub use radicle_cob::{
|
|||
Store, TypeName, Update, Updated, Version,
|
||||
};
|
||||
pub use radicle_cob::{create, get, git, list, remove, update};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct TypedId {
|
||||
pub id: ObjectId,
|
||||
pub type_name: TypeName,
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum ParseIdentifierError {
|
||||
#[error(transparent)]
|
||||
TypeName(#[from] TypeNameParse),
|
||||
#[error(transparent)]
|
||||
ObjectId(#[from] object::ParseObjectId),
|
||||
}
|
||||
|
||||
impl TypedId {
|
||||
pub fn is_issue(&self) -> bool {
|
||||
self.type_name == *issue::TYPENAME
|
||||
}
|
||||
|
||||
pub fn is_patch(&self) -> bool {
|
||||
self.type_name == *patch::TYPENAME
|
||||
}
|
||||
|
||||
pub fn from_namespaced(
|
||||
n: &crate::git::Namespaced,
|
||||
) -> Result<Option<Self>, ParseIdentifierError> {
|
||||
Self::from_qualified(&n.strip_namespace())
|
||||
}
|
||||
|
||||
pub fn from_qualified(q: &crate::git::Qualified) -> Result<Option<Self>, ParseIdentifierError> {
|
||||
match q.non_empty_iter() {
|
||||
("refs", "cobs", type_name, mut id) => {
|
||||
let Some(id) = id.next() else {
|
||||
return Ok(None);
|
||||
};
|
||||
Ok(Some(Self {
|
||||
id: id.parse()?,
|
||||
type_name: type_name.parse()?,
|
||||
}))
|
||||
}
|
||||
_ => Ok(None),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue