pub mod strict_path;
#[cfg(feature = "virtual-path")]
pub mod virtual_path;
#[cfg(test)]
mod tests;
use crate::StrictPathError;
use std::ffi::OsStr;
use std::path::PathBuf;
pub(crate) fn validate_extension(
extension: &OsStr,
context_path: &std::path::Path,
) -> Result<(), StrictPathError> {
for &byte in extension.as_encoded_bytes() {
if byte == b'/' {
return Err(StrictPathError::path_resolution_error(
context_path.to_path_buf(),
std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"extension must not contain path separators",
),
));
}
#[cfg(windows)]
if byte == b'\\' {
return Err(StrictPathError::path_resolution_error(
context_path.to_path_buf(),
std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"extension must not contain path separators",
),
));
}
if byte == 0 {
return Err(StrictPathError::path_resolution_error(
context_path.to_path_buf(),
std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"extension must not contain NUL bytes",
),
));
}
}
Ok(())
}
#[inline]
pub(crate) fn with_validated_extension(
base: &std::path::Path,
extension: &OsStr,
) -> Result<PathBuf, StrictPathError> {
validate_extension(extension, base)?;
Ok(base.with_extension(extension))
}