Command runner args, Paths and AsRef

This commit is contained in:
Adrian Heine 2019-09-24 22:53:15 +02:00
parent 5d5e9dfcb4
commit 3ccf64fac1
32 changed files with 696 additions and 721 deletions

View file

@ -7,44 +7,29 @@ use std::path::Path;
use resources::Resource;
use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
pub struct Dir<D>
where
D: AsRef<str>,
{
pub struct Dir<D: AsRef<Path>> {
path: D,
}
impl<D> Dir<D>
where
D: AsRef<str>,
{
impl<D: AsRef<Path>> Dir<D> {
pub fn new(path: D) -> Self {
Dir { path }
}
}
impl<D> Symbol for Dir<D>
where
D: AsRef<str>,
{
impl<D: AsRef<Path>> Symbol for Dir<D> {
fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
let metadata = fs::metadata(self.path.as_ref());
// Check if dir exists
if let Err(e) = metadata {
return if e.kind() == io::ErrorKind::NotFound {
Ok(false)
} else {
Err(Box::new(e))
};
if !self.path.as_ref().exists() {
return Ok(false);
}
if metadata.unwrap().is_dir() {
Ok(true)
} else {
Err(Box::new(io::Error::new(
let metadata = fs::metadata(self.path.as_ref())?;
if !metadata.is_dir() {
return Err(Box::new(io::Error::new(
io::ErrorKind::AlreadyExists,
"Could not create a directory, non-directory file exists",
)))
)));
}
Ok(true)
}
fn execute(&self) -> Result<(), Box<dyn Error>> {
@ -52,15 +37,18 @@ where
}
fn get_prerequisites(&self) -> Vec<Resource> {
if let Some(parent) = Path::new(self.path.as_ref()).parent() {
vec![Resource::new("dir", parent.to_string_lossy())]
if let Some(parent) = self.path.as_ref().parent() {
vec![Resource::new("dir", parent.to_str().unwrap())]
} else {
vec![]
}
}
fn provides(&self) -> Option<Vec<Resource>> {
Some(vec![Resource::new("dir", self.path.as_ref())])
Some(vec![Resource::new(
"dir",
self.path.as_ref().to_str().unwrap(),
)])
}
fn as_action<'a>(&'a self, runner: &'a dyn SymbolRunner) -> Box<dyn Action + 'a> {
@ -75,11 +63,8 @@ where
}
}
impl<D> fmt::Display for Dir<D>
where
D: AsRef<str>,
{
impl<D: AsRef<Path>> fmt::Display for Dir<D> {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "Dir {}", self.path.as_ref())
write!(f, "Dir {}", self.path.as_ref().display())
}
}