use std::error::Error; use std::fmt; use std::fs; use std::io; use std::path::Path; use resources::Resource; use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner}; pub struct Dir where D: AsRef { path: D } impl Dir where D: AsRef { pub fn new(path: D) -> Self { Dir { path } } } impl Symbol for Dir where D: AsRef { fn target_reached(&self) -> Result> { 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 metadata.unwrap().is_dir() { Ok(true) } else { Err(Box::new(io::Error::new(io::ErrorKind::AlreadyExists, "Could not create a directory, non-directory file exists"))) } } fn execute(&self) -> Result<(), Box> { fs::create_dir(self.path.as_ref()).map_err(|e| Box::new(e) as Box) } fn get_prerequisites(&self) -> Vec { if let Some(parent) = Path::new(self.path.as_ref()).parent() { vec![ Resource::new("dir", parent.to_string_lossy()) ] } else { vec![] } } fn provides(&self) -> Option> { Some(vec![ Resource::new("dir", self.path.as_ref()) ]) } fn as_action<'a>(&'a self, runner: &'a SymbolRunner) -> Box { Box::new(SymbolAction::new(runner, self)) } fn into_action<'a>(self: Box, runner: &'a SymbolRunner) -> Box where Self: 'a { Box::new(OwnedSymbolAction::new(runner, *self)) } } impl fmt::Display for Dir where D: AsRef { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>{ write!(f, "Dir {}", self.path.as_ref()) } }