1use std::{borrow::Cow, path::Path};
2
3pub fn is_window<T: AsRef<str>>(path: T) -> bool {
4 let mut chars = path.as_ref().chars();
5 matches!(
6 (chars.next().map(|v| v.is_ascii_alphabetic()), chars.next()),
7 (Some(true), Some(':'))
8 )
9}
10
11pub fn is_absolute<T: AsRef<str>>(path: T) -> bool {
12 let path = path.as_ref();
13 if is_window(&path) {
14 true
15 } else {
16 path.starts_with('/')
17 }
18}
19
20pub fn to_unix<T: AsRef<str>>(path: T) -> String {
21 path.as_ref().replace('\\', "/")
22}
23
24pub fn encode_url<T: AsRef<str>>(path: T) -> String {
25 let path = if is_window(&path) {
26 let (driver, tail) = path.as_ref().split_at(1);
27 format!(
28 "/{}{}",
29 driver.to_ascii_lowercase(),
30 tail.replace('\\', "/")
31 )
32 } else {
33 path.as_ref().to_string()
34 };
35 to_unix(path)
36 .split('/')
37 .map(urlencoding::encode)
38 .collect::<Vec<Cow<str>>>()
39 .join("/")
40}
41
42pub fn to_string<T: AsRef<Path>>(path: T) -> String {
43 let path = path.as_ref().display().to_string();
44 path.trim_end_matches(|v| v == '/' || v == '\\').to_string()
45}