You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
798 B
36 lines
798 B
use crate::symbols::Symbol;
|
|
use std::error::Error;
|
|
use std::fs;
|
|
use std::io;
|
|
use std::path::Path;
|
|
|
|
#[derive(Debug)]
|
|
pub struct Dir<P> {
|
|
path: P,
|
|
}
|
|
|
|
impl<P> Dir<P> {
|
|
pub fn new(path: P) -> Self {
|
|
Self { path }
|
|
}
|
|
}
|
|
|
|
impl<P: AsRef<Path>> Symbol for Dir<P> {
|
|
fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
|
|
if !self.path.as_ref().exists() {
|
|
return Ok(false);
|
|
}
|
|
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>> {
|
|
fs::create_dir(self.path.as_ref()).map_err(|e| Box::new(e) as Box<dyn Error>)
|
|
}
|
|
}
|