.cargo/
crates/
brain/
diff/
files/
highlight/
repo/
src/
git/
refs/
check.rs
4.0 KiB
lit.rs
5.7 KiB
name.rs
14.4 KiB
namespaced.rs
3.2 KiB
qualified.rs
6.2 KiB
refspec.rs
18.0 KiB
refs.rs
2.3 KiB
git.rs
57.0 KiB
jj.rs
4.7 KiB
lib.rs
12.6 KiB
oid.rs
3.9 KiB
Cargo.toml
330 B
signals/
tui/
tui-core/
tui-termion/
tui-widgets/
scripts/
src/
.gitignore
30 B
.gitsigners
198 B
AGENTS.md
1.9 KiB
CONTRIBUTING
1.7 KiB
Cargo.lock
5.4 KiB
Cargo.toml
1.9 KiB
LICENSE
1.1 KiB
README.md
245 B
STYLE
3.7 KiB
clippy.toml
213 B
crates/repo/src/git/refs/namespaced.rs
raw
| 1 | // Copyright (c) Kim Altintop <kim@eagain.st> |
| 2 | // Copyright (c) The Radicle Team |
| 3 | |
| 4 | //! Namespaced Git refnames. |
| 5 | use super::*; |
| 6 | |
| 7 | /// A qualified refname under a Git namespace. |
| 8 | #[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)] |
| 9 | pub struct Namespaced<'a>(pub(crate) Cow<'a, RefStr>); |
| 10 | |
| 11 | impl<'a> Namespaced<'a> { |
| 12 | /// Returns the namespace component. |
| 13 | #[allow(clippy::panic)] |
| 14 | pub fn namespace(&self) -> Component<'_> { |
| 15 | let Some(namespace) = self.components().nth(2) else { |
| 16 | panic!("Namespaced::namespace: missing namespace component"); |
| 17 | }; |
| 18 | namespace |
| 19 | } |
| 20 | |
| 21 | /// Removes the outer namespace. |
| 22 | pub fn strip_namespace<'b>(&self) -> Qualified<'b> { |
| 23 | let mut components = self.components().skip(3).collect::<RefString>(); |
| 24 | if components.as_str().is_empty() { |
| 25 | components = name::REFS.to_ref_string(); |
| 26 | } |
| 27 | Qualified(Cow::Owned(components)) |
| 28 | } |
| 29 | |
| 30 | /// Removes all nested namespaces. |
| 31 | pub fn strip_namespace_recursive<'b>(&self) -> Qualified<'b> { |
| 32 | let mut stripped = self.strip_namespace(); |
| 33 | while let Some(namespaced) = stripped.to_namespaced() { |
| 34 | stripped = namespaced.strip_namespace(); |
| 35 | } |
| 36 | stripped |
| 37 | } |
| 38 | |
| 39 | /// Returns an owned namespaced refname. |
| 40 | #[inline] |
| 41 | pub fn to_owned<'b>(&self) -> Namespaced<'b> { |
| 42 | Namespaced(Cow::Owned(self.0.clone().into_owned())) |
| 43 | } |
| 44 | |
| 45 | /// Converts this value into an owned namespaced refname. |
| 46 | #[inline] |
| 47 | pub fn into_owned<'b>(self) -> Namespaced<'b> { |
| 48 | Namespaced(Cow::Owned(self.0.into_owned())) |
| 49 | } |
| 50 | |
| 51 | /// Converts this value into a qualified refname. |
| 52 | #[inline] |
| 53 | pub fn into_qualified(self) -> Qualified<'a> { |
| 54 | Qualified(self.0) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | impl Deref for Namespaced<'_> { |
| 59 | type Target = RefStr; |
| 60 | |
| 61 | #[inline] |
| 62 | fn deref(&self) -> &Self::Target { |
| 63 | &self.0 |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | impl AsRef<RefStr> for Namespaced<'_> { |
| 68 | #[inline] |
| 69 | fn as_ref(&self) -> &RefStr { |
| 70 | self |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | impl AsRef<str> for Namespaced<'_> { |
| 75 | #[inline] |
| 76 | fn as_ref(&self) -> &str { |
| 77 | self.0.as_str() |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | impl<'a> From<Namespaced<'a>> for Qualified<'a> { |
| 82 | #[inline] |
| 83 | fn from(namespaced: Namespaced<'a>) -> Self { |
| 84 | Qualified(namespaced.0) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | impl<'a> From<&'a RefStr> for Option<Namespaced<'a>> { |
| 89 | fn from(refname: &'a RefStr) -> Self { |
| 90 | let mut components = refname.iter(); |
| 91 | match ( |
| 92 | components.next(), |
| 93 | components.next(), |
| 94 | components.next(), |
| 95 | components.next(), |
| 96 | ) { |
| 97 | (Some("refs"), Some("namespaces"), Some(_), Some("refs")) => { |
| 98 | Some(Namespaced(Cow::from(refname))) |
| 99 | } |
| 100 | _ => None, |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | impl<'a, T> From<lit::RefsNamespaces<'_, T>> for Namespaced<'static> |
| 106 | where |
| 107 | T: Into<Component<'a>>, |
| 108 | { |
| 109 | #[inline] |
| 110 | fn from((refs, namespaces, namespace, name): lit::RefsNamespaces<'_, T>) -> Self { |
| 111 | Self(Cow::Owned( |
| 112 | IntoIterator::into_iter([refs.into(), namespaces.into(), namespace.into()]) |
| 113 | .collect::<RefString>() |
| 114 | .and(name), |
| 115 | )) |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | impl Display for Namespaced<'_> { |
| 120 | #[inline] |
| 121 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 122 | self.0.fmt(f) |
| 123 | } |
| 124 | } |