git_ref/
namespace.rs

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