Skip to main content

ambient_ci/action_impl/
mkdir.rs

1use std::path::{Path, PathBuf};
2
3use serde::{Deserialize, Serialize};
4
5use crate::{
6    action::{ActionError, Context},
7    action_impl::ActionImpl,
8};
9
10/// Create a directory.
11///
12/// This is meant for internal use by Ambient. It can't be used in any kind
13/// of plan, pre-plan, or post-plan. It can be used in a runnable plan.
14/// It is generated by Ambient to set up execution of a runnable plan.
15#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
16pub struct Mkdir {
17    pathname: PathBuf,
18}
19
20impl Mkdir {
21    /// Create a new `Mkdir` action.
22    pub fn new(pathname: PathBuf) -> Self {
23        Self { pathname }
24    }
25
26    /// Directory to be created.
27    pub fn pathname(&self) -> &Path {
28        &self.pathname
29    }
30}
31
32impl ActionImpl for Mkdir {
33    fn execute(&self, _context: &mut Context) -> Result<(), ActionError> {
34        if !self.pathname.exists() {
35            std::fs::create_dir(&self.pathname)
36                .map_err(|e| MkdirError::Mkdir(self.pathname.clone(), e))?;
37        }
38        Ok(())
39    }
40}
41
42/// Errors from the Mkdir action
43#[derive(Debug, thiserror::Error)]
44pub enum MkdirError {
45    /// Can't create a directory.
46    #[error("failed to create directory {0}")]
47    Mkdir(PathBuf, #[source] std::io::Error),
48}
49
50impl From<MkdirError> for ActionError {
51    fn from(value: MkdirError) -> Self {
52        Self::Mkdir(value)
53    }
54}