use std::collections::TryReserveError;
use std::env::current_dir;
use std::ffi::{OsStr, OsString};
use std::fmt::{Arguments, Display};
use std::path::{Path, PathBuf};
use std::cmp::Eq;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug)]
pub struct PathBufD(PathBuf);
impl PathBufD {
pub fn new() -> Self {
Self(PathBuf::new())
}
pub fn with_capacity(capacity: usize) -> Self {
Self(PathBuf::with_capacity(capacity))
}
pub fn current() -> Self {
Self(current_dir().unwrap_or(PathBuf::new()))
}
pub fn as_path(&self) -> &Path {
self.0.as_path()
}
pub fn as_bytes(&self) -> Vec<u8> {
self.to_string().as_bytes().to_owned()
}
pub fn push<P>(&mut self, path: P) -> ()
where
P: AsRef<Path>,
{
self.0.push(path)
}
pub fn join<P>(&self, path: P) -> Self
where
P: AsRef<Path>,
{
Self(self.0.join(path))
}
pub fn pop(&mut self) -> bool {
self.0.pop()
}
pub fn set_file_name<S>(&mut self, file_name: S)
where
S: AsRef<OsStr>,
{
self.0.set_file_name(file_name);
}
pub fn set_extension<S>(&mut self, extension: S)
where
S: AsRef<OsStr>,
{
self.0.set_extension(extension);
}
pub fn as_mut_os_string(&mut self) -> &mut OsString {
self.0.as_mut_os_string()
}
pub fn into_os_string(self) -> OsString {
self.0.into_os_string()
}
pub fn into_boxed_path(self) -> Box<Path> {
self.0.into_boxed_path()
}
pub fn capacity(&self) -> usize {
self.0.capacity()
}
pub fn clear(&mut self) -> () {
self.0.clear()
}
pub fn reserve(&mut self, additional: usize) -> () {
self.0.reserve(additional)
}
pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
self.0.try_reserve(additional)
}
pub fn reserve_exact(&mut self, additional: usize) -> () {
self.0.reserve_exact(additional)
}
pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> {
self.0.try_reserve_exact(additional)
}
pub fn shrink_to_fit(&mut self) {
self.0.shrink_to_fit()
}
pub fn shrink_to(&mut self, min_capacity: usize) -> () {
self.0.shrink_to(min_capacity)
}
pub fn extend<P>(self, paths: &[P]) -> Self
where
P: AsRef<Path>,
{
let mut buf = self;
for path in paths {
buf.push(path)
}
buf
}
}
impl Default for PathBufD {
fn default() -> Self {
Self(PathBuf::default())
}
}
impl Display for PathBufD {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0.to_str().unwrap_or(""))
}
}
impl AsRef<Path> for PathBufD {
fn as_ref(&self) -> &Path {
self.as_path()
}
}
impl Into<PathBufD> for PathBuf {
fn into(self) -> PathBufD {
PathBufD(self)
}
}
impl From<PathBufD> for PathBuf {
fn from(value: PathBufD) -> Self {
value.0
}
}
pub fn pathbufd_fmt(args: Arguments) -> PathBufD {
let string = if let Some(s) = args.as_str() {
s
} else {
&args.to_string()
};
let mut pathbufd = PathBufD::new();
for split in string.split("/") {
if split.is_empty() {
pathbufd.push("/");
continue;
}
pathbufd.push(split);
}
return pathbufd;
}
#[macro_export]
macro_rules! pathd {
($($arg:tt)*) => {
pathbufd::pathbufd_fmt(std::format_args!($($arg)*)).to_string()
}
}