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.
70 lines
1.7 KiB
70 lines
1.7 KiB
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<D: AsRef<Path>> {
|
|
path: D,
|
|
}
|
|
|
|
impl<D: AsRef<Path>> Dir<D> {
|
|
pub fn new(path: D) -> Self {
|
|
Self { path }
|
|
}
|
|
}
|
|
|
|
impl<D: AsRef<Path>> Symbol for Dir<D> {
|
|
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>)
|
|
}
|
|
|
|
fn get_prerequisites(&self) -> Vec<Resource> {
|
|
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().to_str().unwrap(),
|
|
)])
|
|
}
|
|
|
|
fn as_action<'a>(&'a self, runner: &'a dyn SymbolRunner) -> Box<dyn Action + 'a> {
|
|
Box::new(SymbolAction::new(runner, self))
|
|
}
|
|
|
|
fn into_action<'a>(self: Box<Self>, runner: &'a dyn SymbolRunner) -> Box<dyn Action + 'a>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
Box::new(OwnedSymbolAction::new(runner, *self))
|
|
}
|
|
}
|
|
|
|
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().display())
|
|
}
|
|
}
|