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
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
    Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, SyntaxShape,
    Value,
};

#[derive(Clone)]
pub struct ErrorMake;

impl Command for ErrorMake {
    fn name(&self) -> &str {
        "error make"
    }

    fn signature(&self) -> Signature {
        Signature::build("error make")
            .optional("error_struct", SyntaxShape::Record, "the error to create")
            .category(Category::Core)
    }

    fn usage(&self) -> &str {
        "Create an error."
    }

    fn run(
        &self,
        engine_state: &EngineState,
        stack: &mut Stack,
        call: &Call,
        input: PipelineData,
    ) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
        let span = call.head;
        let ctrlc = engine_state.ctrlc.clone();
        let arg: Option<Value> = call.opt(engine_state, stack, 0)?;

        if let Some(arg) = arg {
            Ok(make_error(&arg)
                .map(|err| Value::Error { error: err })
                .unwrap_or_else(|| Value::Error {
                    error: ShellError::SpannedLabeledError(
                        "Creating error value not supported.".into(),
                        "unsupported error format".into(),
                        span,
                    ),
                })
                .into_pipeline_data())
        } else {
            input.map(
                move |value| {
                    make_error(&value)
                        .map(|err| Value::Error { error: err })
                        .unwrap_or_else(|| Value::Error {
                            error: ShellError::SpannedLabeledError(
                                "Creating error value not supported.".into(),
                                "unsupported error format".into(),
                                span,
                            ),
                        })
                },
                ctrlc,
            )
        }
    }

    fn examples(&self) -> Vec<Example> {
        vec![
            Example {
                description: "Create a custom error for a custom command",
                example: r#"def foo [x] {
      let span = (metadata $x).span;
      error make {msg: "this is fishy", label: {text: "fish right here", start: $span.start, end: $span.end } }
    }"#,
                result: None,
            },
            Example {
                description: "Create a simple custom error for a custom command",
                example: r#"def foo [x] {
      error make {msg: "this is fishy"}
    }"#,
                result: None,
            },
        ]
    }
}

fn make_error(value: &Value) -> Option<ShellError> {
    if let Value::Record { .. } = &value {
        let msg = value.get_data_by_key("msg");
        let label = value.get_data_by_key("label");

        match (msg, &label) {
            (Some(Value::String { val: message, .. }), Some(label)) => {
                let label_start = label.get_data_by_key("start");
                let label_end = label.get_data_by_key("end");
                let label_text = label.get_data_by_key("text");

                match (label_start, label_end, label_text) {
                    (
                        Some(Value::Int { val: start, .. }),
                        Some(Value::Int { val: end, .. }),
                        Some(Value::String {
                            val: label_text, ..
                        }),
                    ) => Some(ShellError::SpannedLabeledError(
                        message,
                        label_text,
                        Span {
                            start: start as usize,
                            end: end as usize,
                        },
                    )),
                    _ => None,
                }
            }
            (Some(Value::String { val: message, .. }), None) => {
                Some(ShellError::UnlabeledError(message))
            }
            _ => None,
        }
    } else {
        None
    }
}