Files
radicle-git/radicle-git-types/src/namespace.rs
T
Fintan Halpenny ce7dc3890a meta: use long crate names
To help with clarity and potential publishing of the crates, the full
`radicle-` prefix is used for the crate names.

Note that it wasn't chosen got `git-ref-format` and `git-trailers`
since they are truly general libraries (bar the feature flag for link
specific reference names).

Signed-off-by: Fintan Halpenny <fintan.halpenny@gmail.com>
2022-09-15 17:58:14 +01:00

106 lines
2.1 KiB
Rust

// Copyright © 2019-2020 The Radicle Foundation <hello@radicle.foundation>
//
// This file is part of radicle-link, distributed under the GPLv3 with Radicle
// Linking Exception. For full terms see the included LICENSE file.
use std::fmt::{self, Display};
use git_ext as ext;
use multihash::Multihash;
use crate::urn::{self, Urn};
pub trait AsNamespace: Into<ext::RefLike> {
fn into_namespace(self) -> ext::RefLike {
self.into()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Namespace<R>(Urn<R>);
impl<R> AsNamespace for Namespace<R>
where
R: urn::HasProtocol,
for<'a> &'a R: Into<Multihash>,
{
}
impl<'a, R> AsNamespace for &'a Namespace<R>
where
R: urn::HasProtocol,
&'a R: Into<Multihash>,
{
}
impl<R> From<Urn<R>> for Namespace<R> {
fn from(urn: Urn<R>) -> Self {
Self(Urn { path: None, ..urn })
}
}
impl<R: Clone> From<&Urn<R>> for Namespace<R> {
fn from(urn: &Urn<R>) -> Self {
Self(Urn {
path: None,
id: urn.id.clone(),
})
}
}
impl<R> Display for Namespace<R>
where
R: urn::HasProtocol,
for<'a> &'a R: Into<Multihash>,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(ext::RefLike::from(self).as_str())
}
}
impl<R> From<Namespace<R>> for ext::RefLike
where
R: urn::HasProtocol,
for<'a> &'a R: Into<Multihash>,
{
fn from(ns: Namespace<R>) -> Self {
Self::from(ns.0)
}
}
impl<'a, R> From<&'a Namespace<R>> for ext::RefLike
where
R: urn::HasProtocol,
&'a R: Into<Multihash>,
{
fn from(ns: &'a Namespace<R>) -> Self {
Self::from(&ns.0)
}
}
impl<R> From<Namespace<R>> for ext::RefspecPattern
where
R: urn::HasProtocol,
for<'a> &'a R: Into<Multihash>,
{
fn from(ns: Namespace<R>) -> Self {
ext::RefLike::from(ns).into()
}
}
impl<'a, R> From<&'a Namespace<R>> for ext::RefspecPattern
where
R: urn::HasProtocol,
&'a R: Into<Multihash>,
{
fn from(ns: &'a Namespace<R>) -> Self {
ext::RefLike::from(ns).into()
}
}
impl<R> From<Namespace<R>> for Urn<R> {
fn from(ns: Namespace<R>) -> Self {
ns.0
}
}