// Copyright (c) Kim Altintop // Copyright (c) The Radicle Team //! Fully qualified Git refnames. use super::*; /// A fully qualified Git refname. #[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)] pub struct Qualified<'a>(pub(crate) Cow<'a, RefStr>); impl<'a> Qualified<'a> { /// Creates a qualified refname from `refs///`. pub fn from_components<'b, 'c, 'd, A, B, C>(a: A, b: B, tail: C) -> Self where A: Into>, B: Into>, C: IntoIterator>, { let mut inner = name::REFS.join(a.into()).and(b.into()); inner.extend(tail); Self(inner.into()) } /// Returns `refname` as qualified if it starts with `refs/` and has a category and name. pub fn from_refstr(refname: impl Into>) -> Option { let refname = refname.into(); let mut iter = refname.iter(); match (iter.next(), iter.next(), iter.next()) { (Some("refs"), Some(_), Some(_)) => Some(Self(refname)), _ => None, } } /// Returns the qualified refname as a string. #[inline] pub fn as_str(&self) -> &str { self.as_ref() } /// Joins another refname suffix. #[inline] pub fn join<'b, R>(&self, other: R) -> Qualified<'b> where R: AsRef, { Qualified(self.0.join(other).into()) } /// Appends a refspec pattern suffix. pub fn to_pattern

(&self, pattern: P) -> refspec::QualifiedPattern<'_> where P: AsRef, { refspec::QualifiedPattern(Cow::Owned(RefStr::to_pattern(self, pattern.as_ref()))) } /// Returns this refname as namespaced if it has a namespace prefix. #[inline] pub fn to_namespaced(&self) -> Option> { self.0.as_ref().into() } /// Adds `refs/namespaces//` before this refname. pub fn with_namespace<'b>(&self, namespace: Component<'b>) -> Namespaced<'a> { Namespaced(Cow::Owned( IntoIterator::into_iter([lit::Refs.into(), lit::Namespaces.into(), namespace]) .chain(self.0.components()) .collect(), )) } /// Returns the first three string components and the remaining iterator. #[allow(clippy::panic)] pub fn non_empty_iter(&self) -> (&str, &str, &str, name::Iter<'_>) { let mut iter = self.iter(); let (Some(first), Some(second), Some(third)) = (iter.next(), iter.next(), iter.next()) else { panic!("Qualified::non_empty_iter: missing component"); }; (first, second, third, iter) } /// Returns the first three components and the remaining iterator. #[allow(clippy::panic)] pub fn non_empty_components( &self, ) -> ( Component<'_>, Component<'_>, Component<'_>, name::Components<'_>, ) { let mut components = self.components(); let (Some(first), Some(second), Some(third)) = (components.next(), components.next(), components.next()) else { panic!("Qualified::non_empty_components: missing component"); }; (first, second, third, components) } /// Returns an owned qualified refname. #[inline] pub fn to_owned<'b>(&self) -> Qualified<'b> { Qualified(Cow::Owned(self.0.clone().into_owned())) } /// Converts this value into an owned qualified refname. #[inline] pub fn into_owned<'b>(self) -> Qualified<'b> { Qualified(Cow::Owned(self.0.into_owned())) } /// Converts this qualified refname into an owned refname. #[inline] pub fn into_refstring(self) -> RefString { self.into() } } impl Deref for Qualified<'_> { type Target = RefStr; #[inline] fn deref(&self) -> &Self::Target { &self.0 } } impl AsRef for Qualified<'_> { #[inline] fn as_ref(&self) -> &RefStr { self } } impl AsRef for Qualified<'_> { #[inline] fn as_ref(&self) -> &str { self.0.as_str() } } impl AsRef for Qualified<'_> { #[inline] fn as_ref(&self) -> &Self { self } } impl<'a> From> for Cow<'a, RefStr> { #[inline] fn from(qualified: Qualified<'a>) -> Self { qualified.0 } } impl From> for RefString { #[inline] fn from(qualified: Qualified<'_>) -> Self { qualified.0.into_owned() } } impl From<(lit::Refs, T, U)> for Qualified<'_> where T: AsRef, U: AsRef, { #[inline] fn from((refs, category, name): (lit::Refs, T, U)) -> Self { let refs: &RefStr = refs.into(); Self(Cow::Owned(refs.join(category).and(name))) } } impl From> for Qualified<'_> where T: AsRef, { #[inline] fn from((refs, heads, name): lit::RefsHeads) -> Self { Self(Cow::Owned( IntoIterator::into_iter([Component::from(refs), heads.into()]) .collect::() .and(name), )) } } impl From> for Qualified<'_> where T: AsRef, { #[inline] fn from((refs, tags, name): lit::RefsTags) -> Self { Self(Cow::Owned( IntoIterator::into_iter([Component::from(refs), tags.into()]) .collect::() .and(name), )) } } impl From> for Qualified<'_> where T: AsRef, { #[inline] fn from((refs, notes, name): lit::RefsNotes) -> Self { Self(Cow::Owned( IntoIterator::into_iter([Component::from(refs), notes.into()]) .collect::() .and(name), )) } } impl From> for Qualified<'_> where T: AsRef, { #[inline] fn from((refs, remotes, name): lit::RefsRemotes) -> Self { Self(Cow::Owned( IntoIterator::into_iter([Component::from(refs), remotes.into()]) .collect::() .and(name), )) } } impl Display for Qualified<'_> { #[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.0.fmt(f) } }