use std::path::{Path, PathBuf};
use serde::{Deserialize, Serialize};
use crate::{
action::{ActionError, Context},
action_impl::ActionImpl,
};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Mkdir {
pathname: PathBuf,
}
impl Mkdir {
pub fn new(pathname: PathBuf) -> Self {
Self { pathname }
}
pub fn pathname(&self) -> &Path {
&self.pathname
}
}
impl ActionImpl for Mkdir {
fn execute(&self, _context: &mut Context) -> Result<(), ActionError> {
if !self.pathname.exists() {
std::fs::create_dir(&self.pathname)
.map_err(|e| MkdirError::Mkdir(self.pathname.clone(), e))?;
}
Ok(())
}
}
#[derive(Debug, thiserror::Error)]
pub enum MkdirError {
#[error("failed to create directory {0}")]
Mkdir(PathBuf, #[source] std::io::Error),
}
impl From<MkdirError> for ActionError {
fn from(value: MkdirError) -> Self {
Self::Mkdir(value)
}
}