Skip to main content

nu_protocol/errors/
parse_error.rs

1#![allow(unused_assignments)]
2use crate::{Span, Type, ast::RedirectionSource, did_you_mean};
3use miette::Diagnostic;
4use serde::{Deserialize, Serialize};
5use std::{
6    fmt::Display,
7    str::{Utf8Error, from_utf8},
8};
9use thiserror::Error;
10
11#[derive(Clone, Debug, Error, Diagnostic, Serialize, Deserialize, PartialEq)]
12pub enum ParseError {
13    /// The parser encountered unexpected tokens, when the code should have
14    /// finished. You should remove these or finish adding what you intended
15    /// to add.
16    #[error("Extra tokens in code.")]
17    #[diagnostic(code(nu::parser::extra_tokens), help("Try removing them."))]
18    ExtraTokens(#[label = "extra tokens"] Span),
19
20    #[error("Invalid characters after closing delimiter")]
21    #[diagnostic(
22        code(nu::parser::extra_token_after_closing_delimiter),
23        help("Try removing them.")
24    )]
25    ExtraTokensAfterClosingDelimiter(#[label = "invalid characters"] Span),
26
27    #[error("Extra positional argument.")]
28    #[diagnostic(code(nu::parser::extra_positional), help("Usage: {0}"))]
29    ExtraPositional(String, #[label = "extra positional argument"] Span),
30
31    #[error("Required positional parameter after optional parameter")]
32    #[diagnostic(code(nu::parser::required_after_optional))]
33    RequiredAfterOptional(
34        String,
35        #[label = "required parameter {0} after optional parameter"] Span,
36    ),
37
38    #[error("Unexpected end of code.")]
39    #[diagnostic(code(nu::parser::unexpected_eof))]
40    UnexpectedEof(String, #[label("expected closing {0}")] Span),
41
42    #[error("Unclosed delimiter.")]
43    #[diagnostic(code(nu::parser::unclosed_delimiter))]
44    Unclosed(String, #[label("unclosed {0}")] Span),
45
46    #[error("Unbalanced delimiter.")]
47    #[diagnostic(code(nu::parser::unbalanced_delimiter))]
48    Unbalanced(String, String, #[label("unbalanced {0} and {1}")] Span),
49
50    #[error("Parse mismatch during operation.")]
51    #[diagnostic(code(nu::parser::parse_mismatch))]
52    Expected(&'static str, #[label("expected {0}")] Span),
53
54    #[error("Parse mismatch during operation.")]
55    #[diagnostic(code(nu::parser::parse_mismatch_with_full_string_msg))]
56    ExpectedWithStringMsg(String, #[label("expected {0}")] Span),
57
58    #[error("Parse mismatch during operation.")]
59    #[diagnostic(code(nu::parser::parse_mismatch_with_did_you_mean))]
60    ExpectedWithDidYouMean(&'static str, DidYouMean, #[label("expected {0}. {1}")] Span),
61
62    #[error("Command does not support {0} input.")]
63    #[diagnostic(code(nu::parser::input_type_mismatch))]
64    InputMismatch(String, #[label("command doesn't support {0} input")] Span),
65
66    #[error("Command output doesn't match {0}.")]
67    #[diagnostic(code(nu::parser::output_type_mismatch))]
68    OutputMismatch(
69        Type,
70        String,
71        #[label("expected {0}, but command outputs {1}")] Span,
72    ),
73
74    #[error("Type mismatch during operation.")]
75    #[diagnostic(code(nu::parser::type_mismatch))]
76    Mismatch(String, String, #[label("expected {0}, found {1}")] Span), // expected, found, span
77
78    #[error("The '&&' operator is not supported in Nushell")]
79    #[diagnostic(
80        code(nu::parser::shell_andand),
81        help("use ';' instead of the shell '&&', or 'and' instead of the boolean '&&'")
82    )]
83    ShellAndAnd(#[label("instead of '&&', use ';' or 'and'")] Span),
84
85    #[error("The '||' operator is not supported in Nushell")]
86    #[diagnostic(
87        code(nu::parser::shell_oror),
88        help("use 'try' instead of the shell '||', or 'or' instead of the boolean '||'")
89    )]
90    ShellOrOr(#[label("instead of '||', use 'try' or 'or'")] Span),
91
92    #[error("The '2>' shell operation is 'err>' in Nushell.")]
93    #[diagnostic(code(nu::parser::shell_err))]
94    ShellErrRedirect(#[label("use 'err>' instead of '2>' in Nushell")] Span),
95
96    #[error("The '2>&1' shell operation is 'out+err>' in Nushell.")]
97    #[diagnostic(
98        code(nu::parser::shell_outerr),
99        help("Nushell redirection will write all of stdout before stderr.")
100    )]
101    ShellOutErrRedirect(#[label("use 'out+err>' instead of '2>&1' in Nushell")] Span),
102
103    #[error("Multiple redirections provided for {0}.")]
104    #[diagnostic(code(nu::parser::multiple_redirections))]
105    MultipleRedirections(
106        RedirectionSource,
107        #[label = "first redirection"] Span,
108        #[label = "second redirection"] Span,
109    ),
110
111    #[error("Unexpected redirection.")]
112    #[diagnostic(code(nu::parser::unexpected_redirection))]
113    UnexpectedRedirection {
114        #[label = "redirecting nothing"]
115        span: Span,
116    },
117
118    /// One or more of the values have types not supported by the operator.
119    #[error("The '{op}' operator does not work on values of type '{unsupported}'.")]
120    #[diagnostic(code(nu::parser::operator_unsupported_type))]
121    OperatorUnsupportedType {
122        op: &'static str,
123        unsupported: Type,
124        #[label = "does not support '{unsupported}'"]
125        op_span: Span,
126        #[label("{unsupported}")]
127        unsupported_span: Span,
128        #[help]
129        help: Option<&'static str>,
130    },
131
132    /// The operator supports the types of both values, but not the specific combination of their types.
133    #[error("Types '{lhs}' and '{rhs}' are not compatible for the '{op}' operator.")]
134    #[diagnostic(code(nu::parser::operator_incompatible_types))]
135    OperatorIncompatibleTypes {
136        op: &'static str,
137        lhs: Type,
138        rhs: Type,
139        #[label = "does not operate between '{lhs}' and '{rhs}'"]
140        op_span: Span,
141        #[label("{lhs}")]
142        lhs_span: Span,
143        #[label("{rhs}")]
144        rhs_span: Span,
145        #[help]
146        help: Option<&'static str>,
147    },
148
149    #[error("Capture of mutable variable.")]
150    #[diagnostic(code(nu::parser::expected_keyword))]
151    CaptureOfMutableVar(#[label("capture of mutable variable")] Span),
152
153    #[error("Expected keyword.")]
154    #[diagnostic(code(nu::parser::expected_keyword))]
155    ExpectedKeyword(String, #[label("expected {0}")] Span),
156
157    #[error("Unexpected keyword.")]
158    #[diagnostic(
159        code(nu::parser::unexpected_keyword),
160        help("'{0}' keyword is allowed only in a module.")
161    )]
162    UnexpectedKeyword(String, #[label("unexpected {0}")] Span),
163
164    #[error("Can't create alias to parser keyword.")]
165    #[diagnostic(
166        code(nu::parser::cant_alias_keyword),
167        help("Only the following keywords can be aliased: {0}.")
168    )]
169    CantAliasKeyword(String, #[label("not supported in alias")] Span),
170
171    #[error("Can't create alias to expression.")]
172    #[diagnostic(
173        code(nu::parser::cant_alias_expression),
174        help("Only command calls can be aliased.")
175    )]
176    CantAliasExpression(String, #[label("aliasing {0} is not supported")] Span),
177
178    #[error("Unknown operator")]
179    #[diagnostic(code(nu::parser::unknown_operator), help("{1}"))]
180    UnknownOperator(
181        &'static str,
182        &'static str,
183        #[label("Operator '{0}' not supported")] Span,
184    ),
185
186    #[error("Statement used in pipeline.")]
187    #[diagnostic(
188        code(nu::parser::unexpected_keyword),
189        help(
190            "'{0}' keyword is not allowed in pipeline. Use '{0}' by itself, outside of a pipeline."
191        )
192    )]
193    BuiltinCommandInPipeline(String, #[label("not allowed in pipeline")] Span),
194
195    #[error("{0} statement used in pipeline.")]
196    #[diagnostic(
197        code(nu::parser::unexpected_keyword),
198        help(
199            "Assigning '{1}' to '{2}' does not produce a value to be piped. If the pipeline result is meant to be assigned to '{2}', use '{0} {2} = ({1} | ...)'."
200        )
201    )]
202    AssignInPipeline(String, String, String, #[label("'{0}' in pipeline")] Span),
203
204    #[error("`{0}` used as variable name.")]
205    #[diagnostic(
206        code(nu::parser::name_is_builtin_var),
207        help(
208            "'{0}' is the name of a builtin Nushell variable and cannot be used as a variable name"
209        )
210    )]
211    NameIsBuiltinVar(String, #[label("already a builtin variable")] Span),
212
213    #[error("Incorrect value")]
214    #[diagnostic(code(nu::parser::incorrect_value), help("{2}"))]
215    IncorrectValue(String, #[label("unexpected {0}")] Span, String),
216
217    #[error("Invalid binary string.")]
218    #[diagnostic(code(nu::parser::invalid_binary_string), help("{1}"))]
219    InvalidBinaryString(#[label("invalid binary string")] Span, String),
220
221    #[error("Multiple rest params.")]
222    #[diagnostic(code(nu::parser::multiple_rest_params))]
223    MultipleRestParams(#[label = "multiple rest params"] Span),
224
225    #[error("Variable not found.")]
226    #[diagnostic(code(nu::parser::variable_not_found))]
227    VariableNotFound(DidYouMean, #[label = "variable not found. {0}"] Span),
228
229    #[error("Use $env.{0} instead of ${0}.")]
230    #[diagnostic(code(nu::parser::env_var_not_var))]
231    EnvVarNotVar(String, #[label = "use $env.{0} instead of ${0}"] Span),
232
233    #[error("Variable name not supported.")]
234    #[diagnostic(code(nu::parser::variable_not_valid))]
235    VariableNotValid(#[label = "variable name can't contain spaces or quotes"] Span),
236
237    #[error("Alias name not supported.")]
238    #[diagnostic(code(nu::parser::variable_not_valid))]
239    AliasNotValid(
240        #[label = "alias name can't be a number, a filesize, or contain #, ^, or %"] Span,
241    ),
242
243    #[error("Command name not supported.")]
244    #[diagnostic(code(nu::parser::variable_not_valid))]
245    CommandDefNotValid(
246        #[label = "command name can't be a number, a filesize, or contain #, ^, or %"] Span,
247    ),
248
249    #[error("Module not found.")]
250    #[diagnostic(
251        code(nu::parser::module_not_found),
252        help(
253            "module files and their paths must be available before your script is run as parsing occurs before anything is evaluated"
254        )
255    )]
256    ModuleNotFound(#[label = "module {1} not found"] Span, String),
257
258    #[error("Missing mod.nu file.")]
259    #[diagnostic(
260        code(nu::parser::module_missing_mod_nu_file),
261        help(
262            "Directory {0} is missing a mod.nu file.\n\nWhen importing a directory as a Nushell module, it needs to contain a mod.nu file (can be empty). Alternatively, you can use .nu files in the directory as modules individually."
263        )
264    )]
265    ModuleMissingModNuFile(
266        String,
267        #[label = "module directory is missing a mod.nu file"] Span,
268    ),
269
270    #[error("Circular import.")]
271    #[diagnostic(code(nu::parser::circular_import), help("{0}"))]
272    CircularImport(String, #[label = "detected circular import"] Span),
273
274    #[error("Can't export {0} named same as the module.")]
275    #[diagnostic(
276        code(nu::parser::named_as_module),
277        help(
278            "Module {1} can't export {0} named the same as the module. Either change the module name, or export `{2}` {0}."
279        )
280    )]
281    NamedAsModule(
282        String,
283        String,
284        String,
285        #[label = "can't export from module {1}"] Span,
286    ),
287
288    #[error("Module already contains 'main' command.")]
289    #[diagnostic(
290        code(nu::parser::module_double_main),
291        help("Tried to add 'main' command to module '{0}' but it has already been added.")
292    )]
293    ModuleDoubleMain(
294        String,
295        #[label = "module '{0}' already contains 'main'"] Span,
296    ),
297
298    #[error("Can't export alias defined as 'main'.")]
299    #[diagnostic(
300        code(nu::parser::export_main_alias_not_allowed),
301        help(
302            "Exporting aliases as 'main' is not allowed. Either rename the alias or convert it to a custom command."
303        )
304    )]
305    ExportMainAliasNotAllowed(#[label = "can't export from module"] Span),
306
307    #[error("Active overlay not found.")]
308    #[diagnostic(code(nu::parser::active_overlay_not_found))]
309    ActiveOverlayNotFound(#[label = "not an active overlay"] Span),
310
311    #[error("Overlay prefix mismatch.")]
312    #[diagnostic(
313        code(nu::parser::overlay_prefix_mismatch),
314        help(
315            "Overlay {0} already exists {1} a prefix. To add it again, do it {1} the --prefix flag."
316        )
317    )]
318    OverlayPrefixMismatch(
319        String,
320        String,
321        #[label = "already exists {1} a prefix"] Span,
322    ),
323
324    #[error("Module or overlay not found.")]
325    #[diagnostic(
326        code(nu::parser::module_or_overlay_not_found),
327        help(
328            "Requires either an existing overlay, a module, or an import pattern defining a module."
329        )
330    )]
331    ModuleOrOverlayNotFound(#[label = "not a module or an overlay"] Span),
332
333    #[error("Cannot remove the last overlay.")]
334    #[diagnostic(
335        code(nu::parser::cant_remove_last_overlay),
336        help("At least one overlay must always be active.")
337    )]
338    CantRemoveLastOverlay(#[label = "this is the last overlay, can't remove it"] Span),
339
340    #[error("Cannot hide default overlay.")]
341    #[diagnostic(
342        code(nu::parser::cant_hide_default_overlay),
343        help("'{0}' is a default overlay. Default overlays cannot be hidden.")
344    )]
345    CantHideDefaultOverlay(String, #[label = "can't hide overlay"] Span),
346
347    #[error("Cannot add overlay.")]
348    #[diagnostic(code(nu::parser::cant_add_overlay_help), help("{0}"))]
349    CantAddOverlayHelp(String, #[label = "cannot add this overlay"] Span),
350
351    #[error("Duplicate command definition within a block.")]
352    #[diagnostic(code(nu::parser::duplicate_command_def))]
353    DuplicateCommandDef(#[label = "defined more than once"] Span),
354
355    #[error("Unknown command.")]
356    #[diagnostic(
357        code(nu::parser::unknown_command),
358        // TODO: actual suggestions like "Did you mean `foo`?"
359    )]
360    UnknownCommand(#[label = "unknown command"] Span),
361
362    #[error("Non-UTF8 string.")]
363    #[diagnostic(code(nu::parser::non_utf8))]
364    NonUtf8(#[label = "non-UTF8 string"] Span),
365
366    #[error("The `{0}` command doesn't have flag `{1}`.")]
367    #[diagnostic(code(nu::parser::unknown_flag), help("{3}"))]
368    UnknownFlag(String, String, #[label = "unknown flag"] Span, String),
369
370    #[error("Unknown type.")]
371    #[diagnostic(code(nu::parser::unknown_type))]
372    UnknownType(#[label = "unknown type"] Span),
373
374    #[error("Missing flag argument.")]
375    #[diagnostic(code(nu::parser::missing_flag_param))]
376    MissingFlagParam(String, #[label = "flag missing {0} argument"] Span),
377
378    #[error("Only the last flag in a short flag batch can take an argument.")]
379    #[diagnostic(code(nu::parser::only_last_flag_in_batch_can_take_arg))]
380    OnlyLastFlagInBatchCanTakeArg(#[label = "only the last flag can take args"] Span),
381
382    #[error("Missing required positional argument.")]
383    #[diagnostic(
384        code(nu::parser::missing_positional),
385        help("Usage: {2}. Use `--help` for more information.")
386    )]
387    MissingPositional(String, #[label("missing {0}")] Span, String),
388
389    #[error("Missing argument to `{1}`.")]
390    #[diagnostic(code(nu::parser::keyword_missing_arg))]
391    KeywordMissingArgument(
392        String,
393        String,
394        #[label("missing {0} value that follows {1}")] Span,
395    ),
396
397    #[error("Missing type.")]
398    #[diagnostic(code(nu::parser::missing_type))]
399    MissingType(#[label = "expected type"] Span),
400
401    #[error("Type mismatch.")]
402    #[diagnostic(code(nu::parser::type_mismatch))]
403    TypeMismatch(Type, Type, #[label("expected {0}, found {1}")] Span), // expected, found, span
404
405    #[error("Type mismatch.")]
406    #[diagnostic(code(nu::parser::type_mismatch_help), help("{3}"))]
407    TypeMismatchHelp(Type, Type, #[label("expected {0}, found {1}")] Span, String), // expected, found, span, help
408
409    #[error("Missing required flag.")]
410    #[diagnostic(code(nu::parser::missing_required_flag))]
411    MissingRequiredFlag(String, #[label("missing required flag {0}")] Span),
412
413    #[error("Incomplete math expression.")]
414    #[diagnostic(code(nu::parser::incomplete_math_expression))]
415    IncompleteMathExpression(#[label = "incomplete math expression"] Span),
416
417    #[error("Unknown state.")]
418    #[diagnostic(code(nu::parser::unknown_state))]
419    UnknownState(String, #[label("{0}")] Span),
420
421    #[error("Internal error.")]
422    #[diagnostic(code(nu::parser::unknown_state))]
423    InternalError(String, #[label("{0}")] Span),
424
425    #[error("Parser incomplete.")]
426    #[diagnostic(code(nu::parser::parser_incomplete))]
427    IncompleteParser(#[label = "parser support missing for this expression"] Span),
428
429    #[error("Rest parameter needs a name.")]
430    #[diagnostic(code(nu::parser::rest_needs_name))]
431    RestNeedsName(#[label = "needs a parameter name"] Span),
432
433    #[error("Parameter not correct type.")]
434    #[diagnostic(code(nu::parser::parameter_mismatch_type))]
435    ParameterMismatchType(
436        String,
437        String,
438        String,
439        #[label = "parameter {0} needs to be '{1}' instead of '{2}'"] Span,
440    ),
441
442    #[error("Default values should be constant expressions.")]
443    #[diagnostic(code(nu::parser::non_constant_default_value))]
444    NonConstantDefaultValue(#[label = "expected a constant value"] Span),
445
446    #[error("Extra columns.")]
447    #[diagnostic(code(nu::parser::extra_columns))]
448    ExtraColumns(
449        usize,
450        #[label("expected {0} column{}", if *.0 == 1 { "" } else { "s" })] Span,
451    ),
452
453    #[error("Missing columns.")]
454    #[diagnostic(code(nu::parser::missing_columns))]
455    MissingColumns(
456        usize,
457        #[label("expected {0} column{}", if *.0 == 1 { "" } else { "s" })] Span,
458    ),
459
460    #[error("{0}")]
461    #[diagnostic(code(nu::parser::assignment_mismatch))]
462    AssignmentMismatch(String, String, #[label("{1}")] Span),
463
464    #[error("Wrong import pattern structure.")]
465    #[diagnostic(code(nu::parser::wrong_import_pattern))]
466    WrongImportPattern(String, #[label = "{0}"] Span),
467
468    #[error("Export not found.")]
469    #[diagnostic(code(nu::parser::export_not_found))]
470    ExportNotFound(#[label = "could not find imports"] Span),
471
472    #[error("File not found")]
473    #[diagnostic(
474        code(nu::parser::sourced_file_not_found),
475        help("sourced files need to be available before your script is run")
476    )]
477    SourcedFileNotFound(String, #[label("File not found: {0}")] Span),
478
479    #[error("File not found")]
480    #[diagnostic(
481        code(nu::parser::registered_file_not_found),
482        help("registered files need to be available before your script is run")
483    )]
484    RegisteredFileNotFound(String, #[label("File not found: {0}")] Span),
485
486    #[error("File not found")]
487    #[diagnostic(code(nu::parser::file_not_found))]
488    FileNotFound(String, #[label("File not found: {0}")] Span),
489
490    #[error("Plugin not found")]
491    #[diagnostic(
492        code(nu::parser::plugin_not_found),
493        help(
494            "plugins need to be added to the plugin registry file before your script is run (see `plugin add`)"
495        )
496    )]
497    PluginNotFound {
498        name: String,
499        #[label("Plugin not found: {name}")]
500        name_span: Span,
501        #[label("in this registry file")]
502        plugin_config_span: Option<Span>,
503    },
504
505    #[error("Invalid literal")] // <problem> in <entity>.
506    #[diagnostic()]
507    InvalidLiteral(String, String, #[label("{0} in {1}")] Span),
508
509    #[error("{0}")]
510    #[diagnostic()]
511    LabeledError(String, String, #[label("{1}")] Span),
512
513    #[error("{error}")]
514    #[diagnostic(help("{help}"))]
515    LabeledErrorWithHelp {
516        error: String,
517        label: String,
518        help: String,
519        #[label("{label}")]
520        span: Span,
521    },
522
523    #[error("Redirection can not be used with {0}.")]
524    #[diagnostic()]
525    RedirectingBuiltinCommand(
526        &'static str,
527        #[label("not allowed here")] Span,
528        #[label("...and here")] Option<Span>,
529    ),
530
531    #[error("This command does not have a ...rest parameter")]
532    #[diagnostic(
533        code(nu::parser::unexpected_spread_arg),
534        help(
535            "To spread arguments, the command needs to define a multi-positional parameter in its signature, such as ...rest"
536        )
537    )]
538    UnexpectedSpreadArg(String, #[label = "unexpected spread argument"] Span),
539
540    /// Invalid assignment left-hand side
541    ///
542    /// ## Resolution
543    ///
544    /// Assignment requires that you assign to a mutable variable or cell path.
545    #[error("Assignment to an immutable variable.")]
546    #[diagnostic(
547        code(nu::parser::assignment_requires_mutable_variable),
548        help("declare the variable with `mut`, or shadow it again with `let`")
549    )]
550    AssignmentRequiresMutableVar(#[label("needs to be a mutable variable")] Span),
551
552    /// Invalid assignment left-hand side
553    ///
554    /// ## Resolution
555    ///
556    /// Assignment requires that you assign to a variable or variable cell path.
557    #[error("Assignment operations require a variable.")]
558    #[diagnostic(
559        code(nu::parser::assignment_requires_variable),
560        help("try assigning to a variable or a cell path of a variable")
561    )]
562    AssignmentRequiresVar(#[label("needs to be a variable")] Span),
563
564    #[error("Attributes must be followed by a definition.")]
565    #[diagnostic(
566        code(nu::parser::attribute_requires_definition),
567        help("try following this line with a `def` or `extern` definition")
568    )]
569    AttributeRequiresDefinition(#[label("must be followed by a definition")] Span),
570}
571
572impl ParseError {
573    pub fn span(&self) -> Span {
574        match self {
575            ParseError::ExtraTokens(s) => *s,
576            ParseError::ExtraPositional(_, s) => *s,
577            ParseError::UnexpectedEof(_, s) => *s,
578            ParseError::Unclosed(_, s) => *s,
579            ParseError::Unbalanced(_, _, s) => *s,
580            ParseError::Expected(_, s) => *s,
581            ParseError::ExpectedWithStringMsg(_, s) => *s,
582            ParseError::ExpectedWithDidYouMean(_, _, s) => *s,
583            ParseError::Mismatch(_, _, s) => *s,
584            ParseError::OperatorUnsupportedType { op_span, .. } => *op_span,
585            ParseError::OperatorIncompatibleTypes { op_span, .. } => *op_span,
586            ParseError::ExpectedKeyword(_, s) => *s,
587            ParseError::UnexpectedKeyword(_, s) => *s,
588            ParseError::CantAliasKeyword(_, s) => *s,
589            ParseError::CantAliasExpression(_, s) => *s,
590            ParseError::BuiltinCommandInPipeline(_, s) => *s,
591            ParseError::AssignInPipeline(_, _, _, s) => *s,
592            ParseError::NameIsBuiltinVar(_, s) => *s,
593            ParseError::CaptureOfMutableVar(s) => *s,
594            ParseError::IncorrectValue(_, s, _) => *s,
595            ParseError::InvalidBinaryString(s, _) => *s,
596            ParseError::MultipleRestParams(s) => *s,
597            ParseError::VariableNotFound(_, s) => *s,
598            ParseError::EnvVarNotVar(_, s) => *s,
599            ParseError::VariableNotValid(s) => *s,
600            ParseError::AliasNotValid(s) => *s,
601            ParseError::CommandDefNotValid(s) => *s,
602            ParseError::ModuleNotFound(s, _) => *s,
603            ParseError::ModuleMissingModNuFile(_, s) => *s,
604            ParseError::NamedAsModule(_, _, _, s) => *s,
605            ParseError::ModuleDoubleMain(_, s) => *s,
606            ParseError::ExportMainAliasNotAllowed(s) => *s,
607            ParseError::CircularImport(_, s) => *s,
608            ParseError::ModuleOrOverlayNotFound(s) => *s,
609            ParseError::ActiveOverlayNotFound(s) => *s,
610            ParseError::OverlayPrefixMismatch(_, _, s) => *s,
611            ParseError::CantRemoveLastOverlay(s) => *s,
612            ParseError::CantHideDefaultOverlay(_, s) => *s,
613            ParseError::CantAddOverlayHelp(_, s) => *s,
614            ParseError::DuplicateCommandDef(s) => *s,
615            ParseError::UnknownCommand(s) => *s,
616            ParseError::NonUtf8(s) => *s,
617            ParseError::UnknownFlag(_, _, s, _) => *s,
618            ParseError::RequiredAfterOptional(_, s) => *s,
619            ParseError::UnknownType(s) => *s,
620            ParseError::MissingFlagParam(_, s) => *s,
621            ParseError::OnlyLastFlagInBatchCanTakeArg(s) => *s,
622            ParseError::MissingPositional(_, s, _) => *s,
623            ParseError::KeywordMissingArgument(_, _, s) => *s,
624            ParseError::MissingType(s) => *s,
625            ParseError::TypeMismatch(_, _, s) => *s,
626            ParseError::TypeMismatchHelp(_, _, s, _) => *s,
627            ParseError::InputMismatch(_, s) => *s,
628            ParseError::OutputMismatch(_, _, s) => *s,
629            ParseError::MissingRequiredFlag(_, s) => *s,
630            ParseError::IncompleteMathExpression(s) => *s,
631            ParseError::UnknownState(_, s) => *s,
632            ParseError::InternalError(_, s) => *s,
633            ParseError::IncompleteParser(s) => *s,
634            ParseError::RestNeedsName(s) => *s,
635            ParseError::ParameterMismatchType(_, _, _, s) => *s,
636            ParseError::NonConstantDefaultValue(s) => *s,
637            ParseError::ExtraColumns(_, s) => *s,
638            ParseError::MissingColumns(_, s) => *s,
639            ParseError::AssignmentMismatch(_, _, s) => *s,
640            ParseError::WrongImportPattern(_, s) => *s,
641            ParseError::ExportNotFound(s) => *s,
642            ParseError::SourcedFileNotFound(_, s) => *s,
643            ParseError::RegisteredFileNotFound(_, s) => *s,
644            ParseError::FileNotFound(_, s) => *s,
645            ParseError::PluginNotFound { name_span, .. } => *name_span,
646            ParseError::LabeledError(_, _, s) => *s,
647            ParseError::ShellAndAnd(s) => *s,
648            ParseError::ShellOrOr(s) => *s,
649            ParseError::ShellErrRedirect(s) => *s,
650            ParseError::ShellOutErrRedirect(s) => *s,
651            ParseError::MultipleRedirections(_, _, s) => *s,
652            ParseError::UnexpectedRedirection { span } => *span,
653            ParseError::UnknownOperator(_, _, s) => *s,
654            ParseError::InvalidLiteral(_, _, s) => *s,
655            ParseError::LabeledErrorWithHelp { span: s, .. } => *s,
656            ParseError::RedirectingBuiltinCommand(_, s, _) => *s,
657            ParseError::UnexpectedSpreadArg(_, s) => *s,
658            ParseError::ExtraTokensAfterClosingDelimiter(s) => *s,
659            ParseError::AssignmentRequiresVar(s) => *s,
660            ParseError::AssignmentRequiresMutableVar(s) => *s,
661            ParseError::AttributeRequiresDefinition(s) => *s,
662        }
663    }
664}
665
666#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
667pub struct DidYouMean(Option<String>);
668
669fn did_you_mean_impl(possibilities_bytes: &[&[u8]], input_bytes: &[u8]) -> Option<String> {
670    let input = from_utf8(input_bytes).ok()?;
671    let possibilities = possibilities_bytes
672        .iter()
673        .map(|p| from_utf8(p))
674        .collect::<Result<Vec<&str>, Utf8Error>>()
675        .ok()?;
676    did_you_mean(&possibilities, input)
677}
678impl DidYouMean {
679    pub fn new(possibilities_bytes: &[&[u8]], input_bytes: &[u8]) -> DidYouMean {
680        DidYouMean(did_you_mean_impl(possibilities_bytes, input_bytes))
681    }
682}
683
684impl From<Option<String>> for DidYouMean {
685    fn from(value: Option<String>) -> Self {
686        Self(value)
687    }
688}
689
690impl Display for DidYouMean {
691    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
692        if let Some(suggestion) = &self.0 {
693            write!(f, "Did you mean '{suggestion}'?")
694        } else {
695            write!(f, "")
696        }
697    }
698}