use std::borrow::Borrow;
use std::path::{Component, Path, PathBuf};
use std::str::FromStr;
use thiserror::Error;
#[cfg(feature = "serde")]
use serde_with::{DeserializeFromStr, SerializeDisplay};
#[derive(Debug, Eq, Error, Ord, PartialEq, PartialOrd)]
pub enum PkgPathError {
#[error("Invalid path specified")]
InvalidPath,
}
#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(SerializeDisplay, DeserializeFromStr))]
pub struct PkgPath {
short: PathBuf,
full: PathBuf,
}
impl PkgPath {
pub fn new(path: &str) -> Result<Self, PkgPathError> {
let p = PathBuf::from(path);
let c: Vec<_> = p.components().collect();
match c.len() {
2 => match (c[0], c[1]) {
(Component::Normal(_), Component::Normal(_)) => {
let mut f = PathBuf::from("../../");
f.push(p.clone());
Ok(PkgPath { short: p, full: f })
}
_ => Err(PkgPathError::InvalidPath),
},
4 => match (c[0], c[1], c[2], c[3]) {
(
Component::ParentDir,
Component::ParentDir,
Component::Normal(_),
Component::Normal(_),
) => {
let mut s = PathBuf::from(c[2].as_os_str());
s.push(c[3].as_os_str());
Ok(PkgPath { short: s, full: p })
}
_ => Err(PkgPathError::InvalidPath),
},
_ => Err(PkgPathError::InvalidPath),
}
}
pub fn as_path(&self) -> &Path {
&self.short
}
pub fn as_full_path(&self) -> &Path {
&self.full
}
}
impl std::fmt::Display for PkgPath {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.short.display())
}
}
impl FromStr for PkgPath {
type Err = PkgPathError;
fn from_str(s: &str) -> Result<Self, PkgPathError> {
PkgPath::new(s)
}
}
impl AsRef<Path> for PkgPath {
fn as_ref(&self) -> &Path {
&self.short
}
}
impl crate::kv::FromKv for PkgPath {
fn from_kv(value: &str, span: crate::kv::Span) -> crate::kv::Result<Self> {
Self::new(value).map_err(|e| crate::kv::KvError::Parse {
message: e.to_string(),
span,
})
}
}
impl Borrow<Path> for PkgPath {
fn borrow(&self) -> &Path {
&self.short
}
}
impl TryFrom<&str> for PkgPath {
type Error = PkgPathError;
fn try_from(s: &str) -> Result<Self, Self::Error> {
Self::new(s)
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::ffi::OsStr;
fn assert_valid_foobar(s: &str) -> Result<(), PkgPathError> {
let p = PkgPath::new(s)?;
assert_eq!(p.as_path(), OsStr::new("foo/bar"));
assert_eq!(p.as_full_path(), OsStr::new("../../foo/bar"));
Ok(())
}
#[test]
fn pkgpath_test_good_input() -> Result<(), PkgPathError> {
assert_valid_foobar("foo/bar")?;
assert_valid_foobar("foo//bar")?;
assert_valid_foobar("foo//bar//")?;
assert_valid_foobar("../../foo/bar")?;
assert_valid_foobar("../../foo/bar/")?;
assert_valid_foobar("..//..//foo//bar//")?;
Ok(())
}
#[test]
fn pkgpath_test_bad_input() {
let err = Err(PkgPathError::InvalidPath);
assert_eq!(PkgPath::new(""), err);
assert_eq!(PkgPath::new("\0"), err);
assert_eq!(PkgPath::new("foo"), err);
assert_eq!(PkgPath::new("foo/"), err);
assert_eq!(PkgPath::new("./foo"), err);
assert_eq!(PkgPath::new("./foo/"), err);
assert_eq!(PkgPath::new("../foo"), err);
assert_eq!(PkgPath::new("../foo/"), err);
assert_eq!(PkgPath::new("../foo/bar"), err);
assert_eq!(PkgPath::new("../foo/bar/"), err);
assert_eq!(PkgPath::new("../foo/bar/ojnk"), err);
assert_eq!(PkgPath::new("../foo/bar/ojnk/"), err);
assert_eq!(PkgPath::new("../.."), err);
assert_eq!(PkgPath::new("../../"), err);
assert_eq!(PkgPath::new("../../foo"), err);
assert_eq!(PkgPath::new("../../foo/"), err);
assert_eq!(PkgPath::new("../../foo/bar/ojnk"), err);
assert_eq!(PkgPath::new("../../foo/bar/ojnk/"), err);
assert_eq!(PkgPath::new(".. /../foo/bar"), err);
}
#[test]
fn pkgpath_as_ref() -> Result<(), PkgPathError> {
let p = PkgPath::new("pkgtools/pkg_install")?;
let path: &Path = p.as_ref();
assert_eq!(path, Path::new("pkgtools/pkg_install"));
fn takes_asref(p: impl AsRef<Path>) -> bool {
p.as_ref().starts_with("pkgtools")
}
assert!(takes_asref(&p));
Ok(())
}
}