// Copyright © 2019-2020 The 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 { fn into_namespace(self) -> ext::RefLike { self.into() } } #[derive(Debug, Clone, PartialEq, Eq)] pub struct Namespace(Urn); impl AsNamespace for Namespace where R: urn::HasProtocol, for<'a> &'a R: Into, { } impl<'a, R> AsNamespace for &'a Namespace where R: urn::HasProtocol, &'a R: Into, { } impl From> for Namespace { fn from(urn: Urn) -> Self { Self(Urn { path: None, ..urn }) } } impl From<&Urn> for Namespace { fn from(urn: &Urn) -> Self { Self(Urn { path: None, id: urn.id.clone(), }) } } impl Display for Namespace where R: urn::HasProtocol, for<'a> &'a R: Into, { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.write_str(ext::RefLike::from(self).as_str()) } } impl From> for ext::RefLike where R: urn::HasProtocol, for<'a> &'a R: Into, { fn from(ns: Namespace) -> Self { Self::from(ns.0) } } impl<'a, R> From<&'a Namespace> for ext::RefLike where R: urn::HasProtocol, &'a R: Into, { fn from(ns: &'a Namespace) -> Self { Self::from(&ns.0) } } impl From> for ext::RefspecPattern where R: urn::HasProtocol, for<'a> &'a R: Into, { fn from(ns: Namespace) -> Self { ext::RefLike::from(ns).into() } } impl<'a, R> From<&'a Namespace> for ext::RefspecPattern where R: urn::HasProtocol, &'a R: Into, { fn from(ns: &'a Namespace) -> Self { ext::RefLike::from(ns).into() } } impl From> for Urn { fn from(ns: Namespace) -> Self { ns.0 } }