mod components;
use std::fmt;
use std::hash::Hasher;
pub use components::*;
use crate::typed::{Utf8TypedPath, Utf8TypedPathBuf};
use crate::{private, Encoding, UnixEncoding, Utf8Encoding, Utf8Path, Utf8PathBuf};
pub type Utf8UnixPath = Utf8Path<Utf8UnixEncoding>;
pub type Utf8UnixPathBuf = Utf8PathBuf<Utf8UnixEncoding>;
#[derive(Copy, Clone)]
pub struct Utf8UnixEncoding;
impl private::Sealed for Utf8UnixEncoding {}
impl<'a> Utf8Encoding<'a> for Utf8UnixEncoding {
type Components = Utf8UnixComponents<'a>;
fn label() -> &'static str {
"unix"
}
fn components(path: &'a str) -> Self::Components {
Utf8UnixComponents::new(path)
}
fn hash<H: Hasher>(path: &str, h: &mut H) {
UnixEncoding::hash(path.as_bytes(), h);
}
fn push(current_path: &mut String, path: &str) {
unsafe {
UnixEncoding::push(current_path.as_mut_vec(), path.as_bytes());
}
}
}
impl fmt::Debug for Utf8UnixEncoding {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Utf8UnixEncoding").finish()
}
}
impl fmt::Display for Utf8UnixEncoding {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Utf8UnixEncoding")
}
}
impl<T> Utf8Path<T>
where
T: for<'enc> Utf8Encoding<'enc>,
{
pub fn has_unix_encoding(&self) -> bool {
T::label() == Utf8UnixEncoding::label()
}
pub fn with_unix_encoding(&self) -> Utf8PathBuf<Utf8UnixEncoding> {
self.with_encoding()
}
}
impl Utf8UnixPath {
pub fn to_typed_path(&self) -> Utf8TypedPath {
Utf8TypedPath::unix(self)
}
pub fn to_typed_path_buf(&self) -> Utf8TypedPathBuf {
Utf8TypedPathBuf::from_unix(self)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn push_should_replace_current_path_with_provided_path_if_provided_path_is_absolute() {
let mut current_path = String::new();
Utf8UnixEncoding::push(&mut current_path, "/abc");
assert_eq!(current_path, "/abc");
let mut current_path = String::from("some/path");
Utf8UnixEncoding::push(&mut current_path, "/abc");
assert_eq!(current_path, "/abc");
let mut current_path = String::from("/some/path/");
Utf8UnixEncoding::push(&mut current_path, "/abc");
assert_eq!(current_path, "/abc");
}
#[test]
fn push_should_append_path_to_current_path_with_a_separator_if_provided_path_is_relative() {
let mut current_path = String::new();
Utf8UnixEncoding::push(&mut current_path, "abc");
assert_eq!(current_path, "abc");
let mut current_path = String::from("some/path");
Utf8UnixEncoding::push(&mut current_path, "abc");
assert_eq!(current_path, "some/path/abc");
let mut current_path = String::from("some/path/");
Utf8UnixEncoding::push(&mut current_path, "abc");
assert_eq!(current_path, "some/path/abc");
}
}