1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#[cfg(feature = "error_reporting")]
use peace::miette::{self, SourceSpan};

use crate::ShCmd;

/// Error while managing command execution.
#[cfg_attr(feature = "error_reporting", derive(peace::miette::Diagnostic))]
#[derive(Debug, thiserror::Error)]
pub enum ShCmdError {
    /// Failed to execute command.
    #[error("Failed to execute command: `{}`", sh_cmd)]
    #[cfg_attr(
        feature = "error_reporting",
        diagnostic(code(peace_item_spec_sh_cmd::cmd_exec_fail))
    )]
    CmdExecFail {
        /// The command that failed to be executed.
        sh_cmd: ShCmd,
        /// The command that failed to be executed as a string.
        #[cfg(feature = "error_reporting")]
        #[source_code]
        sh_cmd_string: String,
        /// Underlying IO error.
        #[source]
        error: std::io::Error,
    },

    /// Command produced non-UTF-8 stdout output.
    #[error("Command produced non-UTF-8 stdout output: `{}`", sh_cmd)]
    #[cfg_attr(
        feature = "error_reporting",
        diagnostic(code(peace_item_spec_sh_cmd::stdout_non_utf8)),
        help(
            "Update the command to something that outputs UTF8: `{}`\n\
            Perhaps encode the output using `base64`",
            sh_cmd
        )
    )]
    StdoutNonUtf8 {
        /// The command whose stdout is not a valid UTF-8 string.
        sh_cmd: ShCmd,
        /// Lossy UTF-8 conversion of stdout.
        #[cfg_attr(feature = "error_reporting", source_code)]
        stdout_lossy: String,
        /// Span where the invalid bytes occur.
        #[cfg(feature = "error_reporting")]
        #[label]
        invalid_span: SourceSpan,
        /// Underlying Utf8 error.
        #[source]
        error: std::str::Utf8Error,
    },

    /// Command produced non-UTF-8 stderr output.
    #[error("Command produced non-UTF-8 stderr output: `{}`", sh_cmd)]
    #[cfg_attr(
        feature = "error_reporting",
        diagnostic(code(peace_item_spec_sh_cmd::stderr_non_utf8)),
        help(
            "Update the command to something that outputs UTF8: `{}`\n\
            Perhaps encode the output using `base64`",
            sh_cmd
        )
    )]
    StderrNonUtf8 {
        /// The command whose stderr is not a valid UTF-8 string.
        sh_cmd: ShCmd,
        /// Lossy UTF-8 conversion of stderr.
        #[cfg_attr(feature = "error_reporting", source_code)]
        stderr_lossy: String,
        /// Span where the invalid bytes occur.
        #[cfg(feature = "error_reporting")]
        #[label]
        invalid_span: SourceSpan,
        /// Underlying Utf8 error.
        #[source]
        error: std::str::Utf8Error,
    },

    /// Ensure check shell command did not output "true" or "false".
    #[error(
        r#"Ensure check shell command did not return "true" or "false": `{}`"#,
        sh_cmd
    )]
    #[cfg_attr(
        feature = "error_reporting",
        diagnostic(code(peace_item_spec_sh_cmd::ensure_check_value_not_boolean)),
        help(
            r#"Update the command to return "true" if execution is required, or "false" if not."#
        )
    )]
    EnsureCheckValueNotBoolean {
        /// The ensure check shell command.
        sh_cmd: ShCmd,
        /// The ensure check shell command as a string.
        #[cfg(feature = "error_reporting")]
        #[source_code]
        sh_cmd_string: String,
        /// Stdout.
        stdout: Option<String>,
    },

    /// Clean check shell command did not output "true" or "false".
    #[error(
        r#"Clean check shell command did not return "true" or "false": `{}`"#,
        sh_cmd
    )]
    #[cfg_attr(
        feature = "error_reporting",
        diagnostic(code(peace_item_spec_sh_cmd::clean_check_value_not_boolean)),
        help(
            r#"Update the command to return "true" if execution is required, or "false" if not."#
        )
    )]
    CleanCheckValueNotBoolean {
        /// The clean check shell command.
        sh_cmd: ShCmd,
        /// The clean check shell command as a string.
        #[cfg(feature = "error_reporting")]
        #[source_code]
        sh_cmd_string: String,
        /// Stdout.
        stdout: Option<String>,
    },

    // === Framework errors === //
    /// A `peace` runtime error occurred.
    #[error("A `peace` runtime error occurred.")]
    PeaceRtError(
        #[cfg_attr(feature = "error_reporting", diagnostic_source)]
        #[source]
        #[from]
        peace::rt_model::Error,
    ),
}