Add some prerequisites
This commit is contained in:
parent
124b639e50
commit
f3b70607f1
6 changed files with 81 additions and 0 deletions
49
src/symbols/dir.rs
Normal file
49
src/symbols/dir.rs
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
use std::borrow::Cow;
|
||||
use std::error::Error;
|
||||
use std::fmt;
|
||||
use std::fs;
|
||||
use std::io;
|
||||
use std::io::{Read, Write};
|
||||
use std::ops::Deref;
|
||||
use std::path::Path;
|
||||
|
||||
use symbols::Symbol;
|
||||
|
||||
pub struct Dir<D> where D: AsRef<str> + fmt::Display {
|
||||
path: D
|
||||
}
|
||||
|
||||
impl<D> Dir<D> where D: AsRef<str> + fmt::Display {
|
||||
pub fn new(path: D) -> Self {
|
||||
Dir { path: path }
|
||||
}
|
||||
}
|
||||
|
||||
impl<D> Symbol for Dir<D> where D: AsRef<str> + fmt::Display {
|
||||
fn target_reached(&self) -> Result<bool, Box<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 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<Error>> {
|
||||
fs::create_dir_all(self.path.as_ref()).map_err(|e| Box::new(e) as Box<Error>)
|
||||
}
|
||||
}
|
||||
|
||||
impl<D> fmt::Display for Dir<D> where D: AsRef<str> + fmt::Display {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>{
|
||||
write!(f, "Dir {}", self.path)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue