Skip to main content

verbs/
hook_plan.rs

1// SPDX-License-Identifier: Apache-2.0
2//! Pure hook install planning (no FS / stdin I/O).
3
4/// How hook install obtains script bytes.
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub enum HookInstallSourceKind {
7    File,
8    Stdin,
9}
10
11/// Pure install source plan after CLI gathers flags.
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub enum HookInstallSourcePlan {
14    /// Proceed with the given source kind.
15    Proceed(HookInstallSourceKind),
16    /// Neither --from-file nor --from-stdin provided.
17    SourceRequired,
18    /// --from-stdin selected but content is empty.
19    EmptyStdin,
20}
21
22/// Plan install source from pure flags + stdin emptiness.
23///
24/// `from_file` is true when a path was supplied (content validity is I/O).
25/// `from_stdin` is true when stdin mode is selected.
26/// `stdin_empty` is only meaningful when `from_stdin` is true.
27pub fn plan_hook_install_source(
28    from_file: bool,
29    from_stdin: bool,
30    stdin_empty: bool,
31) -> HookInstallSourcePlan {
32    if from_file {
33        return HookInstallSourcePlan::Proceed(HookInstallSourceKind::File);
34    }
35    if from_stdin {
36        if stdin_empty {
37            return HookInstallSourcePlan::EmptyStdin;
38        }
39        return HookInstallSourcePlan::Proceed(HookInstallSourceKind::Stdin);
40    }
41    HookInstallSourcePlan::SourceRequired
42}
43
44/// Stable advice kind tokens for hook install refusals.
45pub fn hook_install_source_required_kind() -> &'static str {
46    "hook_install_source_required"
47}
48
49pub fn hook_install_empty_stdin_kind() -> &'static str {
50    "hook_install_empty_stdin"
51}
52
53pub fn hook_unknown_kind() -> &'static str {
54    "hook_unknown"
55}
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    #[test]
62    fn install_source_plans() {
63        assert_eq!(
64            plan_hook_install_source(true, false, true),
65            HookInstallSourcePlan::Proceed(HookInstallSourceKind::File)
66        );
67        assert_eq!(
68            plan_hook_install_source(false, true, false),
69            HookInstallSourcePlan::Proceed(HookInstallSourceKind::Stdin)
70        );
71        assert_eq!(
72            plan_hook_install_source(false, true, true),
73            HookInstallSourcePlan::EmptyStdin
74        );
75        assert_eq!(
76            plan_hook_install_source(false, false, false),
77            HookInstallSourcePlan::SourceRequired
78        );
79    }
80}