use std::error::Error; use std::fmt; use std::fs; use std::io; use symbols::Symbol; pub struct Dir where D: AsRef + fmt::Display { path: D } impl Dir where D: AsRef + fmt::Display { pub fn new(path: D) -> Self { Dir { path: path } } } impl Symbol for Dir where D: AsRef + fmt::Display { 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_all(self.path.as_ref()).map_err(|e| Box::new(e) as Box) } } impl fmt::Display for Dir where D: AsRef + fmt::Display { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>{ write!(f, "Dir {}", self.path) } }