use std::path::Component;
use crate::{is_clean, is_dirty, sanitise};
pub trait PathExt {
fn is_clean(&self) -> bool;
fn is_dirty(&self) -> bool;
fn has_clean_file_name(&self) -> bool;
fn has_dirty_file_name(&self) -> bool;
}
impl PathExt for std::path::Path {
fn is_clean(&self) -> bool { self.components().all(|c| component_is_clean(&c)) }
fn is_dirty(&self) -> bool { self.components().any(|c| !component_is_clean(&c)) }
fn has_clean_file_name(&self) -> bool {
self.file_name().is_none_or(|file_name| is_clean(file_name.to_string_lossy()))
}
fn has_dirty_file_name(&self) -> bool {
self.file_name().is_some_and(|file_name| is_dirty(file_name.to_string_lossy()))
}
}
pub trait PathBufExt {
#[doc(alias = "sanitize_filename")]
fn sanitise_filename(&mut self, to: &str) -> Option<bool>;
}
impl PathBufExt for std::path::PathBuf {
fn sanitise_filename(&mut self, to: &str) -> Option<bool> {
if let Some(file_name) = self.file_name() {
if let Some(file_name) = file_name.to_str() {
self.set_file_name(sanitise(file_name, to));
Some(true)
} else {
Some(false)
}
} else {
None
}
}
}
fn component_is_clean(component: &Component) -> bool {
match component {
Component::Normal(component) => is_clean(component.to_string_lossy()),
_ => true,
}
}