gix_ref/
namespace.rs

1use std::path::{Path, PathBuf};
2
3use gix_object::bstr::{BStr, BString, ByteSlice, ByteVec};
4
5use crate::{FullName, FullNameRef, Namespace, PartialNameRef};
6
7impl Namespace {
8    /// Dissolve ourselves into the interior representation
9    pub fn into_bstring(self) -> BString {
10        self.0
11    }
12    /// Return ourselves as
13    pub fn as_bstr(&self) -> &BStr {
14        self.0.as_ref()
15    }
16    /// Return ourselves as a path for use within the filesystem.
17    pub fn to_path(&self) -> &Path {
18        gix_path::from_byte_slice(&self.0)
19    }
20    /// Append the given `prefix` to this namespace so it becomes usable for prefixed iteration.
21    pub fn into_namespaced_prefix(mut self, prefix: &Path) -> PathBuf {
22        let prefix = gix_path::into_bstr(prefix);
23        self.0.push_str(prefix.as_ref());
24        gix_path::to_native_path_on_windows(self.0).into_owned()
25    }
26    pub(crate) fn into_namespaced_name(mut self, name: &FullNameRef) -> FullName {
27        self.0.push_str(name.as_bstr());
28        FullName(self.0)
29    }
30}
31
32/// Given a `namespace` 'foo we output 'refs/namespaces/foo', and given 'foo/bar' we output 'refs/namespaces/foo/refs/namespaces/bar'.
33///
34/// For more information, consult the [git namespace documentation](https://git-scm.com/docs/gitnamespaces).
35pub fn expand<'a, Name, E>(namespace: Name) -> Result<Namespace, gix_validate::reference::name::Error>
36where
37    Name: TryInto<&'a PartialNameRef, Error = E>,
38    gix_validate::reference::name::Error: From<E>,
39{
40    let namespace = &namespace.try_into()?.0;
41    let mut out = BString::default();
42    for component in namespace.split_str(b"/") {
43        out.push_str("refs/namespaces/");
44        out.push_str(component);
45        out.push_str(b"/");
46    }
47    Ok(Namespace(out))
48}