use std::{
env,
path::{Path, PathBuf},
};
use super::ObsString;
#[derive(Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct ObsPath {
path: PathBuf,
}
impl ObsPath {
pub fn new(path_str: &str) -> Self {
Self {
path: Path::new(path_str).into(),
}
}
pub fn from_relative(path_str: &str) -> Self {
let mut relative_path = env::current_exe().unwrap();
relative_path.pop();
let obs_path = Self {
path: relative_path,
};
let path_str = path_str.trim_matches('/');
obs_path.push(path_str)
}
pub fn push(mut self, value: &str) -> Self {
let split = value.split(['/', '\\'].as_ref());
for item in split {
if !item.is_empty() {
self.path.push(item);
}
}
self
}
pub fn pop(mut self) -> Self {
self.path.pop();
self
}
pub fn build(self) -> ObsString {
let mut bytes = self.path.display().to_string().replace("\\", "/");
if self.path.is_dir() {
bytes += "/";
}
let obs_string = ObsString::from(bytes.as_str());
drop(self);
obs_string
}
}
impl From<ObsPath> for ObsString {
fn from(val: ObsPath) -> Self {
val.build()
}
}
impl From<ObsPath> for PathBuf {
fn from(val: ObsPath) -> Self {
val.path
}
}