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 <fintan.halpenny@gmail.com>
X-Clacks-Overhead: GNU Terry Pratchett
This commit is contained in:
Fintan Halpenny 2022-11-07 12:27:53 +00:00
parent c829045b8d
commit 6b303bdc97
No known key found for this signature in database
GPG Key ID: 2552FB6F64066CB7
2 changed files with 20 additions and 0 deletions

View File

@ -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")
}
}

View File

@ -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 _;