mod variation;
mod macros;
mod path_ext;
mod lazy_result;
pub mod hlist;
pub use path_ext::*;
pub use macros::*;
pub use lazy_result::*;
pub use variation::*;
use std::path::{Path, PathBuf, Component};
pub fn slugify(string: &str) -> String {
let mut output = String::with_capacity(string.len());
let mut need_dash = false;
for ch in string.chars() {
for b in deunicode::deunicode_char(ch).unwrap_or("-").bytes() {
match b {
b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' | b'_' => {
if need_dash {
output.push('-');
need_dash = false;
}
output.push(b.to_ascii_lowercase() as char);
}
_ => {
need_dash = !output.is_empty();
}
}
}
}
output
}
pub fn diff_paths<P, B>(path: P, base: B) -> Option<PathBuf>
where P: AsRef<Path>, B: AsRef<Path>
{
let (path, base) = (path.as_ref(), base.as_ref());
if path.has_root() != base.has_root() {
return None;
}
let mut ita = path.components();
let mut itb = base.components();
let mut comps: Vec<Component> = vec![];
loop {
match (ita.next(), itb.next()) {
(None, None) => break,
(Some(a), None) => {
comps.push(a);
comps.extend(ita.by_ref());
break;
}
(None, _) => comps.push(Component::ParentDir),
(Some(a), Some(b)) if comps.is_empty() && a == b => (),
(Some(a), Some(b)) if b == Component::CurDir => comps.push(a),
(Some(_), Some(b)) if b == Component::ParentDir => return None,
(Some(a), Some(_)) => {
comps.push(Component::ParentDir);
for _ in itb {
comps.push(Component::ParentDir);
}
comps.push(a);
comps.extend(ita.by_ref());
break;
}
}
}
Some(comps.iter().map(|c| c.as_os_str()).collect())
}
pub fn is_template(input: &str) -> bool {
let mut slice = input.as_bytes();
while let Some(i) = memchr::memchr(b'{', slice) {
match slice.get(i + 1) {
Some(b'{') | Some(b'%') => return true,
Some(_) => slice = &slice[(i + 1)..],
None => return false,
}
}
false
}
#[cfg(test)]
mod slug_tests {
#[test]
fn test_slugify() {
use crate::util::slugify;
assert_eq!(slugify("My Test String!!!1!1"), "my-test-string-1-1");
assert_eq!(slugify("test\nit now!"), "test-it-now");
assert_eq!(slugify(" --test_-_cool- - "), "test_-_cool");
assert_eq!(slugify("Æúű--cool?"), "aeuu-cool");
assert_eq!(slugify("You & Me"), "you-me");
assert_eq!(slugify(" user@-- example.com "), "user-example-com");
}
}