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    pub fn into_bstring(self) -> BString {
11        self.0
12    }
13    pub fn as_bstr(&self) -> &BStr {
15        self.0.as_ref()
16    }
17    pub fn to_path(&self) -> &Path {
19        gix_path::from_byte_slice(&self.0)
20    }
21    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
34pub 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}