Skip to main content

gix_path/
relative_path.rs

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