summaryrefslogtreecommitdiffstats
path: root/vendor/gix-validate/src/submodule.rs
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/gix-validate/src/submodule.rs')
-rw-r--r--vendor/gix-validate/src/submodule.rs32
1 files changed, 32 insertions, 0 deletions
diff --git a/vendor/gix-validate/src/submodule.rs b/vendor/gix-validate/src/submodule.rs
new file mode 100644
index 000000000..6811f4ff2
--- /dev/null
+++ b/vendor/gix-validate/src/submodule.rs
@@ -0,0 +1,32 @@
+use bstr::{BStr, ByteSlice};
+
+///
+pub mod name {
+ /// The error used in [name()](super::name()).
+ #[derive(Debug, thiserror::Error)]
+ #[allow(missing_docs)]
+ pub enum Error {
+ #[error("Submodule names cannot be empty")]
+ Empty,
+ #[error("Submodules names must not contains '..'")]
+ ParentComponent,
+ }
+}
+
+/// Return the original `name` if it is valid, or the respective error indicating what was wrong with it.
+pub fn name(name: &BStr) -> Result<&BStr, name::Error> {
+ if name.is_empty() {
+ return Err(name::Error::Empty);
+ }
+ match name.find(b"..") {
+ Some(pos) => {
+ let &b = name.get(pos + 2).ok_or(name::Error::ParentComponent)?;
+ if b == b'/' || b == b'\\' {
+ Err(name::Error::ParentComponent)
+ } else {
+ Ok(name)
+ }
+ }
+ None => Ok(name),
+ }
+}