gix_ref/
namespace.rs

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