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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
use miette::Diagnostic;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use crate::{ast::Operator, Span, Type, Value};
#[derive(Debug, Clone, Error, Diagnostic, Serialize, Deserialize)]
pub enum ShellError {
#[error("Type mismatch during operation.")]
#[diagnostic(code(nu::shell::type_mismatch), url(docsrs))]
OperatorMismatch {
#[label = "type mismatch for operator"]
op_span: Span,
lhs_ty: Type,
#[label("{lhs_ty}")]
lhs_span: Span,
rhs_ty: Type,
#[label("{rhs_ty}")]
rhs_span: Span,
},
#[error("Operator overflow.")]
#[diagnostic(code(nu::shell::operator_overflow), url(docsrs), help("{2}"))]
OperatorOverflow(String, #[label = "{0}"] Span, String),
#[error("Pipeline mismatch.")]
#[diagnostic(code(nu::shell::pipeline_mismatch), url(docsrs))]
PipelineMismatch(
String,
#[label("expected: {0}")] Span,
#[label("value originates from here")] Span,
),
#[error("Input type not supported.")]
#[diagnostic(code(nu::shell::only_supports_this_input_type), url(docsrs))]
OnlySupportsThisInputType(
String,
String,
#[label("only {0} input data is supported")] Span,
#[label("input type: {1}")] Span,
),
#[error("Pipeline empty.")]
#[diagnostic(code(nu::shell::pipeline_mismatch), url(docsrs))]
PipelineEmpty(#[label("no input value was piped in")] Span),
#[error("Type mismatch.")]
#[diagnostic(code(nu::shell::type_mismatch), url(docsrs))]
TypeMismatch(String, #[label = "{0}"] Span),
#[error("Type mismatch.")]
#[diagnostic(code(nu::shell::type_mismatch), url(docsrs))]
TypeMismatchGenericMessage {
err_message: String,
#[label = "{err_message}"]
span: Span,
},
#[error("Unsupported operator: {0}.")]
#[diagnostic(code(nu::shell::unsupported_operator), url(docsrs))]
UnsupportedOperator(Operator, #[label = "unsupported operator"] Span),
#[error("Assignment operations require a variable.")]
#[diagnostic(code(nu::shell::assignment_requires_variable), url(docsrs))]
AssignmentRequiresVar(#[label = "needs to be a variable"] Span),
#[error("Assignment to an immutable variable.")]
#[diagnostic(code(nu::shell::assignment_requires_mutable_variable), url(docsrs))]
AssignmentRequiresMutableVar(#[label = "needs to be a mutable variable"] Span),
#[error("Unknown operator: {0}.")]
#[diagnostic(code(nu::shell::unknown_operator), url(docsrs))]
UnknownOperator(String, #[label = "unknown operator"] Span),
#[error("Missing parameter: {0}.")]
#[diagnostic(code(nu::shell::missing_parameter), url(docsrs))]
MissingParameter(String, #[label = "missing parameter: {0}"] Span),
#[error("Incompatible parameters.")]
#[diagnostic(code(nu::shell::incompatible_parameters), url(docsrs))]
IncompatibleParameters {
left_message: String,
#[label("{left_message}")]
left_span: Span,
right_message: String,
#[label("{right_message}")]
right_span: Span,
},
#[error("Delimiter error")]
#[diagnostic(code(nu::shell::delimiter_error), url(docsrs))]
DelimiterError(String, #[label("{0}")] Span),
#[error("Incompatible parameters.")]
#[diagnostic(code(nu::shell::incompatible_parameters), url(docsrs))]
IncompatibleParametersSingle(String, #[label = "{0}"] Span),
#[error("Feature not enabled.")]
#[diagnostic(code(nu::shell::feature_not_enabled), url(docsrs))]
FeatureNotEnabled(#[label = "feature not enabled"] Span),
#[error("Running external commands not supported")]
#[diagnostic(code(nu::shell::external_commands), url(docsrs))]
ExternalNotSupported(#[label = "external not supported"] Span),
#[error("Invalid Probability.")]
#[diagnostic(code(nu::shell::invalid_probability), url(docsrs))]
InvalidProbability(#[label = "invalid probability"] Span),
#[error("Invalid range {0}..{1}")]
#[diagnostic(code(nu::shell::invalid_range), url(docsrs))]
InvalidRange(String, String, #[label = "expected a valid range"] Span),
#[error("Nushell failed: {0}.")]
#[diagnostic(code(nu::shell::nushell_failed), url(docsrs))]
NushellFailed(String),
#[error("Nushell failed: {0}.")]
#[diagnostic(code(nu::shell::nushell_failed_spanned), url(docsrs))]
NushellFailedSpanned(String, String, #[label = "{1}"] Span),
#[error("Nushell failed: {0}.")]
#[diagnostic(code(nu::shell::nushell_failed_help), url(docsrs))]
NushellFailedHelp(String, #[help] String),
#[error("Nushell failed: {0}.")]
#[diagnostic(code(nu::shell::nushell_failed_spanned_help), url(docsrs))]
NushellFailedSpannedHelp(String, String, #[label = "{1}"] Span, #[help] String),
#[error("Variable not found")]
#[diagnostic(code(nu::shell::variable_not_found), url(docsrs))]
VariableNotFoundAtRuntime(#[label = "variable not found"] Span),
#[error("Environment variable '{0}' not found")]
#[diagnostic(code(nu::shell::env_variable_not_found), url(docsrs))]
EnvVarNotFoundAtRuntime(String, #[label = "environment variable not found"] Span),
#[error("Module '{0}' not found")]
#[diagnostic(code(nu::shell::module_not_found), url(docsrs))]
ModuleNotFoundAtRuntime(String, #[label = "module not found"] Span),
#[error("Module or overlay'{0}' not found")]
#[diagnostic(code(nu::shell::module_or_overlay_not_found), url(docsrs))]
ModuleOrOverlayNotFoundAtRuntime(String, #[label = "not a module or overlay"] Span),
#[error("Overlay '{0}' not found")]
#[diagnostic(code(nu::shell::overlay_not_found), url(docsrs))]
OverlayNotFoundAtRuntime(String, #[label = "overlay not found"] Span),
#[error("Not found.")]
#[diagnostic(code(nu::parser::not_found), url(docsrs))]
NotFound(#[label = "did not find anything under this name"] Span),
#[error("Can't convert to {0}.")]
#[diagnostic(code(nu::shell::cant_convert), url(docsrs))]
CantConvert(
String,
String,
#[label("can't convert {1} to {0}")] Span,
#[help] Option<String>,
),
#[error("Can't convert {1} `{2}` to {0}.")]
#[diagnostic(code(nu::shell::cant_convert_with_value), url(docsrs))]
CantConvertWithValue(
String,
String,
String,
#[label("can't be converted to {0}")] Span,
#[label("this {1} value...")] Span,
#[help] Option<String>,
),
#[error("{0} is not representable as a string.")]
#[diagnostic(
code(nu::shell::env_var_not_a_string),
url(docsrs),
help(
r#"The '{0}' environment variable must be a string or be convertible to a string.
Either make sure {0} is a string, or add a 'to_string' entry for it in ENV_CONVERSIONS."#
)
)]
EnvVarNotAString(String, #[label("value not representable as a string")] Span),
#[error("{0} cannot be set manually.")]
#[diagnostic(
code(nu::shell::automatic_env_var_set_manually),
url(docsrs),
help(
r#"The environment variable '{0}' is set automatically by Nushell and cannot not be set manually."#
)
)]
AutomaticEnvVarSetManually(String, #[label("cannot set '{0}' manually")] Span),
#[error("Division by zero.")]
#[diagnostic(code(nu::shell::division_by_zero), url(docsrs))]
DivisionByZero(#[label("division by zero")] Span),
#[error("Can't convert range to countable values")]
#[diagnostic(code(nu::shell::range_to_countable), url(docsrs))]
CannotCreateRange(#[label = "can't convert to countable values"] Span),
#[error("Row number too large (max: {0}).")]
#[diagnostic(code(nu::shell::access_beyond_end), url(docsrs))]
AccessBeyondEnd(usize, #[label = "index too large (max: {0})"] Span),
#[error("Inserted at wrong row number (should be {0}).")]
#[diagnostic(code(nu::shell::access_beyond_end), url(docsrs))]
InsertAfterNextFreeIndex(
usize,
#[label = "can't insert at index (the next available index is {0})"] Span,
),
#[error("Row number too large (empty content).")]
#[diagnostic(code(nu::shell::access_beyond_end), url(docsrs))]
AccessEmptyContent(#[label = "index too large (empty content)"] Span),
#[error("Row number too large.")]
#[diagnostic(code(nu::shell::access_beyond_end_of_stream), url(docsrs))]
AccessBeyondEndOfStream(#[label = "index too large"] Span),
#[error("Data cannot be accessed with a cell path")]
#[diagnostic(code(nu::shell::incompatible_path_access), url(docsrs))]
IncompatiblePathAccess(String, #[label("{0} doesn't support cell paths")] Span),
#[error("Cannot find column")]
#[diagnostic(code(nu::shell::column_not_found), url(docsrs))]
CantFindColumn(
String,
#[label = "cannot find column '{0}'"] Span,
#[label = "value originates here"] Span,
),
#[error("Column already exists")]
#[diagnostic(code(nu::shell::column_already_exists), url(docsrs))]
ColumnAlreadyExists(
String,
#[label = "column '{0}' already exists"] Span,
#[label = "value originates here"] Span,
),
#[error("Not a list value")]
#[diagnostic(code(nu::shell::not_a_list), url(docsrs))]
NotAList(
#[label = "value not a list"] Span,
#[label = "value originates here"] Span,
),
#[error("External command failed")]
#[diagnostic(code(nu::shell::external_command), url(docsrs), help("{1}"))]
ExternalCommand(String, String, #[label("{0}")] Span),
#[error("Unsupported input")]
#[diagnostic(code(nu::shell::unsupported_input), url(docsrs))]
UnsupportedInput(
String,
String,
#[label("{0}")] Span, #[label("input type: {1}")] Span,
),
#[error("Unable to parse datetime")]
#[diagnostic(
code(nu::shell::datetime_parse_error),
url(docsrs),
help(
r#"Examples of supported inputs:
* "5 pm"
* "2020/12/4"
* "2020.12.04 22:10 +2"
* "2020-04-12 22:10:57 +02:00"
* "2020-04-12T22:10:57.213231+02:00"
* "Tue, 1 Jul 2003 10:52:37 +0200""#
)
)]
DatetimeParseError(#[label("datetime parsing failed")] Span),
#[error("Network failure")]
#[diagnostic(code(nu::shell::network_failure), url(docsrs))]
NetworkFailure(String, #[label("{0}")] Span),
#[error("Command not found")]
#[diagnostic(code(nu::shell::command_not_found), url(docsrs))]
CommandNotFound(#[label("command not found")] Span),
#[error("Alias not found")]
#[diagnostic(code(nu::shell::alias_not_found), url(docsrs))]
AliasNotFound(#[label("alias not found")] Span),
#[error("Flag not found")]
#[diagnostic(code(nu::shell::flag_not_found), url(docsrs))]
FlagNotFound(String, #[label("{0} not found")] Span),
#[error("File not found")]
#[diagnostic(code(nu::shell::file_not_found), url(docsrs))]
FileNotFound(#[label("file not found")] Span),
#[error("File not found")]
#[diagnostic(code(nu::shell::file_not_found), url(docsrs))]
FileNotFoundCustom(String, #[label("{0}")] Span),
#[error("Plugin failed to load: {0}")]
#[diagnostic(code(nu::shell::plugin_failed_to_load), url(docsrs))]
PluginFailedToLoad(String),
#[error("Plugin failed to encode: {0}")]
#[diagnostic(code(nu::shell::plugin_failed_to_encode), url(docsrs))]
PluginFailedToEncode(String),
#[error("Plugin failed to decode: {0}")]
#[diagnostic(code(nu::shell::plugin_failed_to_decode), url(docsrs))]
PluginFailedToDecode(String),
#[error("I/O interrupted")]
#[diagnostic(code(nu::shell::io_interrupted), url(docsrs))]
IOInterrupted(String, #[label("{0}")] Span),
#[error("I/O error")]
#[diagnostic(code(nu::shell::io_error), url(docsrs), help("{0}"))]
IOError(String),
#[error("I/O error")]
#[diagnostic(code(nu::shell::io_error), url(docsrs))]
IOErrorSpanned(String, #[label("{0}")] Span),
#[error("Permission Denied")]
#[diagnostic(code(nu::shell::permission_denied), url(docsrs))]
PermissionDeniedError(String, #[label("{0}")] Span),
#[error("Out of memory")]
#[diagnostic(code(nu::shell::out_of_memory), url(docsrs))]
OutOfMemoryError(String, #[label("{0}")] Span),
#[error("Cannot change to directory")]
#[diagnostic(code(nu::shell::cannot_cd_to_directory), url(docsrs))]
NotADirectory(#[label("is not a directory")] Span),
#[error("Directory not found")]
#[diagnostic(code(nu::shell::directory_not_found), url(docsrs))]
DirectoryNotFound(#[label("directory not found")] Span, #[help] Option<String>),
#[error("Directory not found")]
#[diagnostic(code(nu::shell::directory_not_found_custom), url(docsrs))]
DirectoryNotFoundCustom(String, #[label("{0}")] Span),
#[error("Move not possible")]
#[diagnostic(code(nu::shell::move_not_possible), url(docsrs))]
MoveNotPossible {
source_message: String,
#[label("{source_message}")]
source_span: Span,
destination_message: String,
#[label("{destination_message}")]
destination_span: Span,
},
#[error("Move not possible")]
#[diagnostic(code(nu::shell::move_not_possible_single), url(docsrs))]
MoveNotPossibleSingle(String, #[label("{0}")] Span),
#[error("Create not possible")]
#[diagnostic(code(nu::shell::create_not_possible), url(docsrs))]
CreateNotPossible(String, #[label("{0}")] Span),
#[error("Not possible to change the access time")]
#[diagnostic(code(nu::shell::change_access_time_not_possible), url(docsrs))]
ChangeAccessTimeNotPossible(String, #[label("{0}")] Span),
#[error("Not possible to change the modified time")]
#[diagnostic(code(nu::shell::change_modified_time_not_possible), url(docsrs))]
ChangeModifiedTimeNotPossible(String, #[label("{0}")] Span),
#[error("Remove not possible")]
#[diagnostic(code(nu::shell::remove_not_possible), url(docsrs))]
RemoveNotPossible(String, #[label("{0}")] Span),
#[error("No file to be removed")]
NoFileToBeRemoved(),
#[error("No file to be moved")]
NoFileToBeMoved(),
#[error("No file to be copied")]
NoFileToBeCopied(),
#[error("Error trying to read file")]
#[diagnostic(code(nu::shell::error_reading_file), url(docsrs))]
ReadingFile(String, #[label("{0}")] Span),
#[error("Name not found")]
#[diagnostic(code(nu::shell::name_not_found), url(docsrs))]
DidYouMean(String, #[label("did you mean '{0}'?")] Span),
#[error("{0}")]
#[diagnostic(code(nu::shell::did_you_mean_custom), url(docsrs))]
DidYouMeanCustom(String, String, #[label("did you mean '{1}'?")] Span),
#[error("Non-UTF8 string")]
#[diagnostic(code(nu::parser::non_utf8), url(docsrs))]
NonUtf8(#[label = "non-UTF8 string"] Span),
#[error("Non-UTF8 string")]
#[diagnostic(code(nu::parser::non_utf8_custom), url(docsrs))]
NonUtf8Custom(String, #[label = "{0}"] Span),
#[error("Casting error")]
#[diagnostic(code(nu::shell::downcast_not_possible), url(docsrs))]
DowncastNotPossible(String, #[label("{0}")] Span),
#[error("Unsupported config value")]
#[diagnostic(code(nu::shell::unsupported_config_value), url(docsrs))]
UnsupportedConfigValue(String, String, #[label = "expected {0}, got {1}"] Span),
#[error("Missing config value")]
#[diagnostic(code(nu::shell::missing_config_value), url(docsrs))]
MissingConfigValue(String, #[label = "missing {0}"] Span),
#[error("Negative value passed when positive one is required")]
#[diagnostic(code(nu::shell::needs_positive_value), url(docsrs))]
NeedsPositiveValue(#[label = "use a positive value"] Span),
#[error("{0}")]
#[diagnostic()]
GenericError(
String,
String,
#[label("{1}")] Option<Span>,
#[help] Option<String>,
#[related] Vec<ShellError>,
),
#[error("{1}")]
#[diagnostic()]
OutsideSpannedLabeledError(#[source_code] String, String, String, #[label("{2}")] Span),
#[error("Deprecated command {0}")]
#[diagnostic(code(nu::shell::deprecated_command), url(docsrs))]
DeprecatedCommand(
String,
String,
#[label = "'{0}' is deprecated. Please use '{1}' instead."] Span,
),
#[error("Deprecated parameter {0}")]
#[diagnostic(code(nu::shell::deprecated_command), url(docsrs))]
DeprecatedParameter(
String,
String,
#[label = "Parameter '{0}' is deprecated. Please use '{1}' instead."] Span,
),
#[error("Non-Unicode input received.")]
#[diagnostic(code(nu::shell::non_unicode_input), url(docsrs))]
NonUnicodeInput,
#[error("Unexpected abbr component `{0}`.")]
#[diagnostic(code(nu::shell::unexpected_path_abbreviateion), url(docsrs))]
UnexpectedAbbrComponent(String),
#[error("Eval block failed with pipeline input")]
#[diagnostic(code(nu::shell::eval_block_with_input), url(docsrs))]
EvalBlockWithInput(#[label("source value")] Span, #[related] Vec<ShellError>),
#[error("Break used outside of loop")]
Break(#[label = "used outside of loop"] Span),
#[error("Continue used outside of loop")]
Continue(#[label = "used outside of loop"] Span),
#[error("Return used outside of function")]
Return(#[label = "used outside of function"] Span, Box<Value>),
#[error("Recursion limit ({recursion_limit}) reached")]
#[diagnostic(code(nu::shell::recursion_limit_reached), url(docsrs))]
RecursionLimitReached {
recursion_limit: u64,
#[label("This called itself too many times")]
span: Option<Span>,
},
}
impl From<std::io::Error> for ShellError {
fn from(input: std::io::Error) -> ShellError {
ShellError::IOError(format!("{:?}", input))
}
}
impl std::convert::From<Box<dyn std::error::Error>> for ShellError {
fn from(input: Box<dyn std::error::Error>) -> ShellError {
ShellError::IOError(input.to_string())
}
}
impl From<Box<dyn std::error::Error + Send + Sync>> for ShellError {
fn from(input: Box<dyn std::error::Error + Send + Sync>) -> ShellError {
ShellError::IOError(format!("{:?}", input))
}
}
pub fn into_code(err: &ShellError) -> Option<String> {
err.code().map(|code| code.to_string())
}
pub fn did_you_mean(possibilities: &[String], input: &str) -> Option<String> {
let possibilities: Vec<&str> = possibilities.iter().map(|s| s.as_str()).collect();
let suggestion =
crate::lev_distance::find_best_match_for_name_with_substrings(&possibilities, input, None)
.map(|s| s.to_string());
if let Some(suggestion) = &suggestion {
if suggestion.len() == 1 && suggestion.to_lowercase() != input.to_lowercase() {
return None;
}
}
suggestion
}
pub fn levenshtein_distance(a: &str, b: &str) -> usize {
crate::lev_distance::lev_distance(a, b, usize::max_value())
.expect("It is impossible to exceed the supplied limit since all types involved are usize.")
}
#[cfg(test)]
mod tests {
use super::did_you_mean;
#[test]
fn did_you_mean_examples() {
let all_cases = [
(
vec!["a", "b"],
vec![
("a", Some("a"), ""),
("A", Some("a"), ""),
(
"c",
None,
"Not helpful to suggest an arbitrary choice when none are close",
),
("ccccccccccccccccccccccc", None, "Not helpful to suggest an arbitrary choice when none are close"),
],
),
(
vec!["OS", "PWD", "PWDPWDPWDPWD"],
vec![
("pwd", Some("PWD"), "Exact case insensitive match yields a match"),
("pwdpwdpwdpwd", Some("PWDPWDPWDPWD"), "Exact case insensitive match yields a match"),
("PWF", Some("PWD"), "One-letter typo yields a match"),
("pwf", None, "Case difference plus typo yields no match"),
("Xwdpwdpwdpwd", None, "Case difference plus typo yields no match"),
]
),
(
vec!["foo", "bar", "baz"],
vec![
("fox", Some("foo"), ""),
("FOO", Some("foo"), ""),
("FOX", None, ""),
(
"ccc",
None,
"Not helpful to suggest an arbitrary choice when none are close",
),
(
"zzz",
None,
"'baz' does share a character, but rustc rule is edit distance must be <= 1/3 of the length of the user input",
),
],
),
(
vec!["aaaaaa"],
vec![
("XXaaaa", Some("aaaaaa"), "Distance of 2 out of 6 chars: close enough to meet rustc's rule"),
("XXXaaa", None, "Distance of 3 out of 6 chars: not close enough to meet rustc's rule"),
("XaaaaX", Some("aaaaaa"), "Distance of 2 out of 6 chars: close enough to meet rustc's rule"),
("XXaaaaXX", None, "Distance of 4 out of 6 chars: not close enough to meet rustc's rule")
]
),
];
for (possibilities, cases) in all_cases {
let possibilities: Vec<String> = possibilities.iter().map(|s| s.to_string()).collect();
for (input, expected_suggestion, discussion) in cases {
let suggestion = did_you_mean(&possibilities, input);
assert_eq!(
suggestion.as_deref(),
expected_suggestion,
"Expected the following reasoning to hold but it did not: '{}'",
discussion
);
}
}
}
}