gix_validate/
submodule.rs

1use bstr::{BStr, ByteSlice};
2
3///
4pub mod name {
5    /// The error used in [name()](super::name()).
6    #[derive(Debug, thiserror::Error)]
7    #[allow(missing_docs)]
8    pub enum Error {
9        #[error("Submodule names cannot be empty")]
10        Empty,
11        #[error("Submodules names must not contains '..'")]
12        ParentComponent,
13    }
14}
15
16/// Return the original `name` if it is valid, or the respective error indicating what was wrong with it.
17pub fn name(name: &BStr) -> Result<&BStr, name::Error> {
18    if name.is_empty() {
19        return Err(name::Error::Empty);
20    }
21    match name.find(b"..") {
22        Some(pos) => {
23            let &b = name.get(pos + 2).ok_or(name::Error::ParentComponent)?;
24            if b == b'/' || b == b'\\' {
25                Err(name::Error::ParentComponent)
26            } else {
27                Ok(name)
28            }
29        }
30        None => Ok(name),
31    }
32}