use std::borrow::Cow;
use std::path::PathBuf;
use crate::pack::{EnvScope, ExecOnFail, RequireOnFail, SymlinkKind};
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ExecResult {
PerformedChange,
WouldPerformChange,
AlreadySatisfied,
NoOp,
#[non_exhaustive]
Skipped {
pack_path: std::path::PathBuf,
actions_hash: String,
},
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PredicateOutcome {
Satisfied,
Unsatisfied,
}
#[non_exhaustive]
#[derive(Debug, Clone)]
pub enum StepKind {
Symlink {
src: PathBuf,
dst: PathBuf,
kind: SymlinkKind,
backup: bool,
normalize: bool,
},
Unlink {
dst: PathBuf,
},
Env {
name: String,
value: String,
scope: EnvScope,
},
Mkdir {
path: PathBuf,
mode: Option<String>,
},
Rmdir {
path: PathBuf,
backup: bool,
force: bool,
},
Require {
outcome: PredicateOutcome,
on_fail: RequireOnFail,
},
When {
branch_taken: bool,
nested_steps: Vec<ExecStep>,
},
Exec {
cmdline: String,
cwd: Option<PathBuf>,
on_fail: ExecOnFail,
shell: bool,
},
PackSkipped {
actions_hash: String,
},
}
#[non_exhaustive]
#[derive(Debug, Clone)]
pub struct ExecStep {
pub action_name: Cow<'static, str>,
pub result: ExecResult,
pub details: StepKind,
}
pub const ACTION_SYMLINK: &str = "symlink";
pub const ACTION_UNLINK: &str = "unlink";
pub const ACTION_ENV: &str = "env";
pub const ACTION_MKDIR: &str = "mkdir";
pub const ACTION_RMDIR: &str = "rmdir";
pub const ACTION_REQUIRE: &str = "require";
pub const ACTION_WHEN: &str = "when";
pub const ACTION_EXEC: &str = "exec";
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn skipped_carries_pack_path_and_hash() {
let r = ExecResult::Skipped {
pack_path: PathBuf::from("/tmp/packs/foo"),
actions_hash: "a".repeat(64),
};
match r {
ExecResult::Skipped { pack_path, actions_hash } => {
assert_eq!(pack_path, PathBuf::from("/tmp/packs/foo"));
assert_eq!(actions_hash.len(), 64);
}
_ => panic!("expected Skipped"),
}
}
#[test]
fn pack_skipped_round_trips() {
let k = StepKind::PackSkipped { actions_hash: "abc".into() };
match k {
StepKind::PackSkipped { actions_hash } => {
assert_eq!(actions_hash, "abc");
}
_ => panic!("expected PackSkipped"),
}
}
}