use lazy_regex::*;
pub(crate) mod common;
pub(crate) mod flexible;
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Debug)]
pub enum FlexPathVariant {
Common,
Windows,
}
impl FlexPathVariant {
pub(crate) const NATIVE: Self = {
#[cfg(target_os = "windows")] {
Self::Windows
}
#[cfg(not(target_os = "windows"))] {
Self::Common
}
};
pub const fn native() -> Self {
Self::NATIVE
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct FlexPath(String, FlexPathVariant);
impl FlexPath {
pub fn new(path: &str, variant: FlexPathVariant) -> Self {
Self(flexible::resolve_one(path, variant), variant)
}
pub fn new_common(path: &str) -> Self {
Self(flexible::resolve_one(path, FlexPathVariant::Common), FlexPathVariant::Common)
}
pub fn new_native(path: &str) -> Self {
Self(flexible::resolve_one(path, FlexPathVariant::NATIVE), FlexPathVariant::NATIVE)
}
pub fn from_n<'a, T: IntoIterator<Item = &'a str>>(paths: T, variant: FlexPathVariant) -> Self {
Self(flexible::resolve_n(paths, variant), variant)
}
pub fn from_n_common<'a, T: IntoIterator<Item = &'a str>>(paths: T) -> Self {
Self::from_n(paths, FlexPathVariant::Common)
}
pub fn from_n_native<'a, T: IntoIterator<Item = &'a str>>(paths: T) -> Self {
Self::from_n(paths, FlexPathVariant::NATIVE)
}
pub fn variant(&self) -> FlexPathVariant {
self.1
}
pub fn is_absolute(&self) -> bool {
flexible::is_absolute(&self.0, self.1)
}
pub fn resolve(&self, path2: &str) -> FlexPath {
FlexPath(flexible::resolve(&self.0, path2, self.1), self.1)
}
pub fn resolve_n<'a, T: IntoIterator<Item = &'a str>>(&self, paths: T) -> FlexPath {
FlexPath(flexible::resolve(&self.0, &flexible::resolve_n(paths, self.1), self.1), self.1)
}
pub fn relative(&self, to_path: &str) -> String {
flexible::relative(&self.0, to_path, self.1)
}
pub fn change_extension(&self, extension: &str) -> FlexPath {
Self(change_extension(&self.0, extension), self.1)
}
pub fn change_last_extension(&self, extension: &str) -> FlexPath {
Self(change_last_extension(&self.0, extension), self.1)
}
pub fn has_extension(&self, extension: &str) -> bool {
has_extension(&self.0, extension)
}
pub fn has_extensions<'a, T: IntoIterator<Item = &'a str>>(&self, extensions: T) -> bool {
has_extensions(&self.0, extensions)
}
pub fn base_name(&self) -> String {
base_name(&self.0)
}
pub fn base_name_without_ext<'a, T>(&self, extensions: T) -> String
where T: IntoIterator<Item = &'a str>
{
base_name_without_ext(&self.0, extensions)
}
pub fn to_string_with_flex_separator(&self) -> String {
if self.variant() == FlexPathVariant::Windows {
self.0.replace('/', "\\")
} else {
self.0.clone()
}
}
}
impl ToString for FlexPath {
fn to_string(&self) -> String {
self.0.clone()
}
}
static STARTS_WITH_PATH_SEPARATOR: Lazy<Regex> = lazy_regex!(r"^[/\\]");
fn change_extension(path: &str, extension: &str) -> String {
let extension = (if extension.starts_with('.') { "" } else { "." }).to_owned() + extension;
if regex_find!(r"(\.[^\.]+)+$", path).is_none() {
return path.to_owned() + &extension;
}
regex_replace!(r"(\.[^\.]+)+$", path, |_, _| &extension).into_owned()
}
fn change_last_extension(path: &str, extension: &str) -> String {
let extension = (if extension.starts_with('.') { "" } else { "." }).to_owned() + extension;
assert!(
extension[1..].find('.').is_none(),
"The argument to hydroperfox_filepaths::change_last_extension() must only contain one extension; got {}",
extension
);
if regex_find!(r"(\..+)$", path).is_none() {
return path.to_owned() + &extension;
}
regex_replace!(r"(\..+)$", path, |_, _| &extension).into_owned()
}
fn extension_arg(extension: &str) -> String {
(if extension.starts_with('.') { "" } else { "." }).to_owned() + extension
}
fn has_extension(path: &str, extension: &str) -> bool {
let extension = (if extension.starts_with('.') { "" } else { "." }).to_owned() + extension;
path.ends_with(&extension_arg(&extension))
}
fn has_extensions<'a, T: IntoIterator<Item = &'a str>>(path: &str, extensions: T) -> bool {
extensions.into_iter().any(|ext| has_extension(path, ext))
}
fn base_name(path: &str) -> String {
path.split('/').last().map_or("", |s| s).to_owned()
}
fn base_name_without_ext<'a, T>(path: &str, extensions: T) -> String
where T: IntoIterator<Item = &'a str>
{
let extensions = extensions.into_iter().map(extension_arg).collect::<Vec<String>>();
path.split('/').last().map_or("".to_owned(), |base| {
regex_replace!(r"(\.[^\.]+)+$", base, |_, prev_ext: &str| {
(if extensions.iter().any(|ext| ext == prev_ext) { "" } else { prev_ext }).to_owned()
}).into_owned()
})
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn extension_and_base_name() {
assert!(FlexPath::new_common("a.x").has_extensions([".x", ".y"]));
assert_eq!("a.y", FlexPath::new_common("a.x").change_extension(".y").to_string());
assert_eq!("a.0", FlexPath::new_common("a.x.y").change_extension(".0").to_string());
assert_eq!("a.0.1", FlexPath::new_common("a.x.y").change_extension(".0.1").to_string());
assert_eq!("qux.html", FlexPath::new_common("foo/qux.html").base_name());
assert_eq!("qux", FlexPath::new_common("foo/qux.html").base_name_without_ext([".html"]));
}
#[test]
fn resolution() {
assert_eq!("a", FlexPath::from_n_common(["a/b/.."]).to_string());
assert_eq!("a", FlexPath::from_n_common(["a", "b", ".."]).to_string());
assert_eq!("/a/b", FlexPath::new_common("/c").resolve("/a/b").to_string());
assert_eq!("a", FlexPath::new_common("a/b").resolve("..").to_string());
assert_eq!("a/b", FlexPath::new_common("a/b/").to_string());
assert_eq!("a/b", FlexPath::new_common("a//b").to_string());
let windows = FlexPathVariant::Windows;
assert_eq!(r"\\Whack/a/Box", FlexPath::from_n(["foo", r"\\Whack////a//Box", "..", "Box"], windows).to_string());
assert_eq!("C:/a", FlexPath::new("C:/", windows).resolve("a").to_string());
assert_eq!("D:/", FlexPath::new("C:/", windows).resolve("D:/").to_string());
assert_eq!("D:/a", FlexPath::new("D:/a", windows).to_string());
assert_eq!("C:/a/f/b", FlexPath::new("a", windows).resolve("C:/a///f//b").to_string());
}
#[test]
fn relativity() {
assert_eq!("", FlexPath::new_common("/a/b").relative("/a/b"));
assert_eq!("c", FlexPath::new_common("/a/b").relative("/a/b/c"));
assert_eq!("../../c/d", FlexPath::new_common("/a/b/c").relative("/a/c/d"));
assert_eq!("..", FlexPath::new_common("/a/b/c").relative("/a/b"));
assert_eq!("../..", FlexPath::new_common("/a/b/c").relative("/a"));
assert_eq!("..", FlexPath::new_common("/a").relative("/"));
assert_eq!("a", FlexPath::new_common("/").relative("/a"));
assert_eq!("", FlexPath::new_common("/").relative("/"));
assert_eq!("../../c/d", FlexPath::new_common("/a/b").relative("/c/d"));
assert_eq!("../c", FlexPath::new_common("/a/b").relative("/a/c"));
let windows = FlexPathVariant::Windows;
assert_eq!("", FlexPath::new("C:/", windows).relative("C:/"));
assert_eq!("", FlexPath::new("C:/foo", windows).relative("C:/foo"));
assert_eq!(r"\\foo", FlexPath::new("C:/", windows).relative(r"\\foo"));
assert_eq!("../../foo", FlexPath::new(r"\\a/b", windows).relative(r"\\foo"));
assert_eq!("D:/", FlexPath::new("C:/", windows).relative(r"D:"));
}
}