From 6b303bdc97ee3749413885fc6c49ca9a6e14becf Mon Sep 17 00:00:00 2001 From: Fintan Halpenny Date: Mon, 7 Nov 2022 12:27:53 +0000 Subject: [PATCH] radicle-cob: Component conversions `TypeName` and `ObjectId` are expected to be used in reference names. Add `From` conversions to `Component` for both of these types to make it easier for using them with `git-ref-format`. Signed-off-by: Fintan Halpenny X-Clacks-Overhead: GNU Terry Pratchett --- radicle-cob/src/object.rs | 10 ++++++++++ radicle-cob/src/type_name.rs | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/radicle-cob/src/object.rs b/radicle-cob/src/object.rs index d23561e7..dfded07a 100644 --- a/radicle-cob/src/object.rs +++ b/radicle-cob/src/object.rs @@ -6,6 +6,7 @@ use std::{convert::TryFrom as _, fmt, str::FromStr}; use git_ext::Oid; +use git_ref_format::{Component, RefString}; use serde::{Deserialize, Serialize}; use thiserror::Error; @@ -83,3 +84,12 @@ impl<'de> Deserialize<'de> for ObjectId { Ok(ObjectId(oid)) } } + +impl From<&ObjectId> for Component<'_> { + fn from(id: &ObjectId) -> Self { + let refstr = RefString::try_from(id.0.to_string()) + .expect("collaborative object id's are valid ref strings"); + Component::from_refstring(refstr) + .expect("collaborative object id's are valid refname components") + } +} diff --git a/radicle-cob/src/type_name.rs b/radicle-cob/src/type_name.rs index 5eea9a7c..084913e7 100644 --- a/radicle-cob/src/type_name.rs +++ b/radicle-cob/src/type_name.rs @@ -5,6 +5,7 @@ use std::{fmt, str::FromStr}; +use git_ref_format::{Component, RefString}; use serde::{Deserialize, Serialize}; use thiserror::Error; @@ -53,6 +54,15 @@ impl FromStr for TypeName { } } +impl From<&TypeName> for Component<'_> { + fn from(name: &TypeName) -> Self { + let refstr = RefString::try_from(name.0.to_string()) + .expect("collaborative object type names are valid ref strings"); + Component::from_refstring(refstr) + .expect("collaborative object type names are valid refname components") + } +} + #[cfg(test)] mod test { use std::str::FromStr as _;