Skip to main content

gix_path/
relative_path.rs

1use gix_error::{Result, validation};
2use std::path::Path;
3
4use bstr::{BStr, BString, ByteSlice};
5use gix_error::{ResultExt, bail};
6use gix_validate::path::component::Options;
7
8use crate::{from_byte_slice, os_str_into_bstr};
9
10pub(super) mod types {
11    use bstr::{BStr, ByteSlice};
12    /// A wrapper for `BStr`. It is used to enforce the following constraints:
13    ///
14    /// - The path separator always is `/`, independent of the platform.
15    /// - Only normal components are allowed.
16    /// - It is always represented as a bunch of bytes.
17    ///
18    /// Conversion errors for invalid components include the component bytes as `input`
19    /// [metadata](gix_error::Error::metadata()).
20    #[repr(transparent)]
21    pub struct RelativePath {
22        inner: BStr,
23    }
24
25    impl AsRef<[u8]> for RelativePath {
26        #[inline]
27        fn as_ref(&self) -> &[u8] {
28            self.inner.as_bytes()
29        }
30    }
31}
32use types::RelativePath;
33
34impl RelativePath {
35    fn new_unchecked(value: &BStr) -> &RelativePath {
36        // SAFETY: `RelativePath` is transparent and equivalent to a `&BStr` if provided as reference.
37        #[expect(unsafe_code)]
38        unsafe {
39            std::mem::transmute::<&BStr, &RelativePath>(value)
40        }
41    }
42}
43
44fn relative_path_from_value_and_path<'a>(path_bstr: &'a BStr, path: &Path) -> Result<&'a RelativePath> {
45    if path.is_absolute() {
46        bail!(validation("A RelativePath is not allowed to be absolute"));
47    }
48
49    let options = Options::default();
50
51    for component in path.components() {
52        let component = os_str_into_bstr(component.as_os_str())
53            .or_raise(|| validation("Relative path contains an invalid component encoding"))?;
54        gix_validate::path::component(component, None, options)
55            .or_raise(|| validation("Relative path contains an invalid component").with_input(component))?;
56    }
57
58    Ok(RelativePath::new_unchecked(path_bstr))
59}
60
61impl<'a> TryFrom<&'a str> for &'a RelativePath {
62    type Error = gix_error::Error;
63
64    fn try_from(value: &'a str) -> Result<Self> {
65        relative_path_from_value_and_path(value.into(), Path::new(value))
66    }
67}
68
69impl<'a> TryFrom<&'a BStr> for &'a RelativePath {
70    type Error = gix_error::Error;
71
72    fn try_from(value: &'a BStr) -> Result<Self> {
73        let path = from_byte_slice(value)?;
74        relative_path_from_value_and_path(value, path)
75    }
76}
77
78impl<'a> TryFrom<&'a [u8]> for &'a RelativePath {
79    type Error = gix_error::Error;
80
81    #[inline]
82    fn try_from(value: &'a [u8]) -> Result<Self> {
83        let path = from_byte_slice(value)?;
84        relative_path_from_value_and_path(value.as_bstr(), path)
85    }
86}
87
88impl<'a, const N: usize> TryFrom<&'a [u8; N]> for &'a RelativePath {
89    type Error = gix_error::Error;
90
91    #[inline]
92    fn try_from(value: &'a [u8; N]) -> Result<Self> {
93        let path = from_byte_slice(value.as_bstr())?;
94        relative_path_from_value_and_path(value.as_bstr(), path)
95    }
96}
97
98impl<'a> TryFrom<&'a BString> for &'a RelativePath {
99    type Error = gix_error::Error;
100
101    fn try_from(value: &'a BString) -> Result<Self> {
102        let path = from_byte_slice(value.as_bstr())?;
103        relative_path_from_value_and_path(value.as_bstr(), path)
104    }
105}