use std::fs::canonicalize;
use std::str::FromStr;
#[derive(Debug, Clone)]
pub struct DirPath(String);
impl FromStr for DirPath {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let path = canonicalize(s).map_err(|_| format!("Path {} does not exist", s))?;
if !path.is_dir() {
return Err(format!("{} is not a directory", s));
}
Ok(Self(path.to_string_lossy().to_string()))
}
}
impl DirPath {
pub fn str(&self) -> &str {
&self.0
}
}
#[derive(Debug, Clone)]
pub struct FilePath(String);
impl FromStr for FilePath {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let path = canonicalize(s).map_err(|_| format!("Path {} does not exist", s))?;
if !path.is_file() {
return Err(format!("{} is not a file", s));
}
Ok(Self(path.to_string_lossy().to_string()))
}
}
impl FilePath {
pub fn str(&self) -> &str {
&self.0
}
}
#[cfg(test)]
mod test {
use super::*;
mod dir_path {
use super::*;
#[test]
fn path_not_exist() {
let path = "not_a_path".parse::<DirPath>().unwrap_err();
assert_eq!(path, "Path not_a_path does not exist");
}
#[test]
fn path_is_not_dir() {
let error = "Cargo.toml".parse::<DirPath>().unwrap_err();
assert_eq!(error, "Cargo.toml is not a directory");
}
#[test]
fn path_is_dir() {
let path = "/home".parse::<DirPath>().unwrap();
assert_eq!(path.str(), "/home");
}
}
mod file_path {
use super::*;
#[test]
fn path_not_exist() {
let path = "not_a_path".parse::<FilePath>().unwrap_err();
assert_eq!(path, "Path not_a_path does not exist");
}
#[test]
fn path_is_not_file() {
let error = "/usr/bin".parse::<FilePath>().unwrap_err();
assert_eq!(error, "/usr/bin is not a file");
}
#[test]
fn path_is_file() {
let path = "/init".parse::<FilePath>().unwrap();
assert_eq!(path.str(), "/init");
}
}
}