forked from technique-lang/technique
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessages.rs
More file actions
963 lines (894 loc) · 32.1 KB
/
Copy pathmessages.rs
File metadata and controls
963 lines (894 loc) · 32.1 KB
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
use crate::problem::Present;
use technique::{formatting::Render, language::*, parsing::ParsingError};
/// Generate problem and detail messages for parsing errors using AST construction
pub fn generate_error_message<'i>(error: &ParsingError, renderer: &dyn Render) -> (String, String) {
match error {
ParsingError::IllegalParserState(_, _) => (
"Illegal parser state".to_string(),
"Internal parser error. This should not have happened! Sorry.".to_string(),
),
ParsingError::Unimplemented(_, _) => (
"Feature not yet implemented".to_string(),
"This feature is planned but not yet available.".to_string(),
),
ParsingError::Unrecognized(_, _) => (
"Unrecognized input".to_string(),
"The parser encountered unexpected content".to_string(),
),
ParsingError::Expected(_, _, value) => (
format!("Expected {}", value),
format!(
"The parser was looking for {} but found something else.",
value
),
),
ParsingError::ExpectedMatchingChar(_, _, subject, start, end) => (
format!("Expected matching character '{}'", end),
format!(
r#"
The parser was expecting {} enclosed by '{}' and '{}' but
there was no more input remaining in the current scope.
"#,
subject, start, end
)
.trim_ascii()
.to_string(),
),
ParsingError::MissingParenthesis(_, _) => {
let examples = vec![Descriptive::Binding(
Box::new(Descriptive::Application(Invocation {
target: Target::Local(Identifier("mix_pangalactic_gargle_blaster")),
parameters: None,
})),
vec![Identifier("zaphod"), Identifier("trillian")],
)];
(
"Lists of binding variables must be enclosed in parentheses".to_string(),
format!(
r#"
If you bind the result of an invocation to more than one variable, you must
enclose those names in parenthesis. For example:
{}
"#,
examples[0].present(renderer),
)
.trim_ascii()
.to_string(),
)
}
ParsingError::UnclosedInterpolation(_, _) => (
"Unclosed string interpolation".to_string(),
r#"
Every '{' that starts an interpolation within a string must have a
corresponding '}' to mark where the interpolation ends and the string
literal resumes.
"#
.trim_ascii()
.to_string(),
),
ParsingError::InvalidHeader(_, _) => {
// Format the sample metadata using the same code as the formatter
let mut formatted_example = String::new();
formatted_example
.push_str(&renderer.style(crate::formatting::Syntax::Header, "% technique v1\n"));
formatted_example.push_str(&renderer.style(
crate::formatting::Syntax::Header,
"! «license»; © «copyright»\n",
));
formatted_example
.push_str(&renderer.style(crate::formatting::Syntax::Header, "& «domain»"));
(
"Invalid header".to_string(),
format!(
r#"
The metadata describing a Technique file must follow this format:
{}
The first line are the magic bytes identifying the Technique file format and
current language version.
The second line lists the System Package Data Exchange (SPDX) information
about the ownership of the Technique in this file and permissions associated
with it. The line is optional but if present it starts with a «license»
declaration, conventionally an SPDX identifier like {}, {}, or
{}. A declaration of the «copyright» holder can optionally follow
the license statement, separated from it by a semicolon. Copyright statements
typically list the year and then the name of the person or entity holding the
copyright.
The third line optionally specifies the domain or kind of Technique this is,
to be used when rendering the Technique. Common domains include
{}, {}, and {}.
"#,
formatted_example,
renderer.style(crate::formatting::Syntax::Header, "MIT"),
renderer.style(crate::formatting::Syntax::Header, "CC-BY 4.0"),
renderer.style(crate::formatting::Syntax::Header, "Proprietary"),
renderer.style(crate::formatting::Syntax::Header, "checklist"),
renderer.style(crate::formatting::Syntax::Header, "nasa-esa-iss,v4.0"),
renderer.style(crate::formatting::Syntax::Header, "recipe")
),
)
}
ParsingError::InvalidCharacter(_, _, c) => (
format!("Invalid character '{}'", c),
"This character is not allowed here.".to_string(),
),
ParsingError::UnexpectedEndOfInput(_, _) => (
"Unexpected end of input".to_string(),
"The file ended before the parser expected it to".to_string(),
),
ParsingError::InvalidIdentifier(_, _, _) => {
let examples = vec![
Procedure {
name: Identifier("make_coffee"),
parameters: None,
signature: None,
elements: Vec::new(),
},
Procedure {
name: Identifier("attempt1"),
parameters: None,
signature: None,
elements: Vec::new(),
},
Procedure {
name: Identifier("i"),
parameters: None,
signature: None,
elements: Vec::new(),
},
Procedure {
name: Identifier("l33t_hax0r"),
parameters: None,
signature: None,
elements: Vec::new(),
},
];
(
"Invalid identifier".to_string(),
format!(
r#"
Identifiers must start with a lowercase letter and contain only lower case
letters, numbers, and underscores. Valid examples include:
{}
{}
{}
{}
"#,
examples[0].present(renderer),
examples[1].present(renderer),
examples[2].present(renderer),
examples[3].present(renderer)
)
.trim_ascii()
.to_string(),
)
}
ParsingError::InvalidForma(_, _) => {
let examples = vec![
Forma("Coffee"),
Forma("Ingredients"),
Forma("PatientRecord"),
];
(
"Invalid forma name".to_string(),
format!(
r#"
The names of Forma (the basic types in Technique) must start with an uppercase
letter and cannot contain dashes, underscores, spaces, or other punctuation.
For example:
{}
{}
{}
"#,
examples[0].present(renderer),
examples[1].present(renderer),
examples[2].present(renderer)
)
.trim_ascii()
.to_string(),
)
}
ParsingError::InvalidGenus(_, _) => {
let examples = vec![
Genus::Single(Forma("Coffee")),
Genus::Tuple(vec![Forma("Beans"), Forma("Water")]),
Genus::Naked(vec![Forma("Beans"), Forma("Water")]),
Genus::List(Forma("Patient")),
Genus::Unit,
];
(
"Invalid genus".to_string(),
format!(
r#"
Genus are the full types in Technique. They are either simple (just the name
of a Forma) or compound (lists or tuples of Forma). Some examples:
{}
{}
{}
{}
{}
Tuples can be enclosed in parenthesis or "naked"; semantically they are the
same. The final example is the syntax for the Unit genus, used when something
doesn't have an input or result, per se.
"#,
examples[0].present(renderer),
examples[1].present(renderer),
examples[2].present(renderer),
examples[3].present(renderer),
examples[4].present(renderer)
)
.trim_ascii()
.to_string(),
)
}
ParsingError::InvalidSignature(_, _) => {
let examples = vec![
Signature {
requires: Genus::Single(Forma("A")),
provides: Genus::Single(Forma("B")),
},
Signature {
requires: Genus::Tuple(vec![Forma("Beans"), Forma("Milk")]),
provides: Genus::Single(Forma("Coffee")),
},
Signature {
requires: Genus::List(Forma("FunctionalRequirement")),
provides: Genus::Single(Forma("Architecture")),
},
];
(
"Invalid signature".to_string(),
format!(
r#"
Procedure signatures follow the pattern requires -> provides, where requires
and provides are genus. Some examples:
{}
{}
{}
Signatures are optional on procedure declarations but if present must follow
this form.
"#,
examples[0].present(renderer),
examples[1].present(renderer),
examples[2].present(renderer)
)
.trim_ascii()
.to_string(),
)
}
ParsingError::InvalidDeclaration(_, _) => {
let examples = vec![
Procedure {
name: Identifier("f"),
parameters: None,
signature: None,
elements: Vec::new(),
},
Procedure {
name: Identifier("implementation"),
parameters: None,
signature: None,
elements: Vec::new(),
},
Procedure {
name: Identifier("make_coffee"),
parameters: None,
signature: None,
elements: Vec::new(),
},
Procedure {
name: Identifier("f"),
parameters: None,
signature: Some(Signature {
requires: Genus::Single(Forma("A")),
provides: Genus::Single(Forma("B")),
}),
elements: Vec::new(),
},
Procedure {
name: Identifier("implementation"),
parameters: None,
signature: Some(Signature {
requires: Genus::Single(Forma("Design")),
provides: Genus::Single(Forma("Product")),
}),
elements: Vec::new(),
},
Procedure {
name: Identifier("make_coffee"),
parameters: None,
signature: Some(Signature {
requires: Genus::Naked(vec![Forma("Beans"), Forma("Milk")]),
provides: Genus::Single(Forma("Coffee")),
}),
elements: Vec::new(),
},
Procedure {
name: Identifier("make_coffee"),
parameters: None,
signature: Some(Signature {
requires: Genus::Tuple(vec![Forma("Beans"), Forma("Milk")]),
provides: Genus::Single(Forma("Coffee")),
}),
elements: Vec::new(),
},
Procedure {
name: Identifier("make_coffee"),
parameters: Some(vec![Identifier("b"), Identifier("m")]),
signature: Some(Signature {
requires: Genus::Naked(vec![Forma("Beans"), Forma("Milk")]),
provides: Genus::Single(Forma("Coffee")),
}),
elements: Vec::new(),
},
];
(
"Invalid procedure declaration".to_string(),
format!(
r#"
Procedures are declared by specifying an identifier as a name, followed by a
colon:
{}
{}
{}
A procedure can optionally have a signature, as in the following examples:
{}
{}
{}
{}
Finally, variables can be assigned for the names of the input parameters:
{}
"#,
examples[0].present(renderer),
examples[1].present(renderer),
examples[2].present(renderer),
examples[3].present(renderer),
examples[4].present(renderer),
examples[5].present(renderer),
examples[6].present(renderer),
examples[7].present(renderer)
)
.trim_ascii()
.to_string(),
)
}
ParsingError::InvalidParameters(_, _) => {
let examples = vec![
Procedure {
name: Identifier("create_bypass"),
parameters: Some(vec![Identifier("a"), Identifier("b")]),
signature: None,
elements: Vec::new(),
},
Procedure {
name: Identifier("bulldoze"),
parameters: Some(vec![Identifier("c")]),
signature: None,
elements: Vec::new(),
},
Procedure {
name: Identifier("lawsuit"),
parameters: None,
signature: Some(Signature {
requires: Genus::Single(Forma("Council")),
provides: Genus::List(Forma("Penny")),
}),
elements: Vec::new(),
},
Procedure {
name: Identifier("lawsuit"),
parameters: Some(vec![Identifier("c")]),
signature: Some(Signature {
requires: Genus::Single(Forma("Council")),
provides: Genus::List(Forma("Penny")),
}),
elements: Vec::new(),
},
];
(
"Parameters must be enclosed in parentheses".to_string(),
format!(
r#"
Parameters to a procedure must be variables, and enclosed in parentheses. For
example:
{}
{}
Naming the input genus is optional, however; these are both valid procedure
declarations (and in fact the same):
{}
{}
"#,
examples[0].present(renderer),
examples[1].present(renderer),
examples[2].present(renderer),
examples[3].present(renderer)
)
.trim_ascii()
.to_string(),
)
}
ParsingError::InvalidSection(_, _) => {
// Roman numeral sections don't have AST representation
(
"Invalid section heading".to_string(),
format!(
r#"
Section headings use capital Roman numerals, followed by optional title:
I. First Section
II. Second Section
III.
Conventionally such a title would be in Proper Case but that is left up to the
author of the Technique.
"#
)
.trim_ascii()
.to_string(),
)
}
ParsingError::InvalidInvocation(_, _) => {
let examples = vec![
Invocation {
target: Target::Local(Identifier("make_coffee")),
parameters: None,
},
Invocation {
target: Target::Local(Identifier("check_vitals")),
parameters: Some(vec![Expression::Variable(Identifier("patient"))]),
},
];
(
"Invalid procedure invocation".to_string(),
format!(
r#"
To denote the invocation of another procedure, use angle brackets:
{}
If the procedure takes parameters they can be specified in parenthesis:
{}
"#,
examples[0].present(renderer),
examples[1].present(renderer)
)
.trim_ascii()
.to_string(),
)
}
ParsingError::InvalidFunction(_, _) => {
let examples = vec![
Function {
target: Identifier("exec"),
parameters: vec![Expression::String(vec![Piece::Text("ls -la")])],
},
Function {
target: Identifier("now"),
parameters: vec![],
},
Function {
target: Identifier("calculate"),
parameters: vec![
Expression::Variable(Identifier("a")),
Expression::Variable(Identifier("b")),
],
},
];
(
"Invalid function call".to_string(),
format!(
r#"
Function calls in code blocks are made by specifying the name of the function
to be executed followed by parentheses, supplying values, variables, or other
expressions as parameters as required:
{}
{}
{}
"#,
examples[0].present(renderer),
examples[1].present(renderer),
examples[2].present(renderer)
)
.trim_ascii()
.to_string(),
)
}
ParsingError::InvalidCodeBlock(_, _) => {
let examples = vec![
Expression::Execution(Function {
target: Identifier("exec"),
parameters: vec![Expression::String(vec![Piece::Text("command")])],
}),
Expression::Repeat(Box::new(Expression::Number(Numeric::Integral(5)))),
Expression::Foreach(
vec![Identifier("patient")],
Box::new(Expression::Variable(Identifier("patients"))),
),
];
(
"Invalid code block".to_string(),
format!(
r#"
Inline code blocks are enclosed in braces:
{{ {} }}
{{ {} }}
{{ {} }}
"#,
examples[0].present(renderer),
examples[1].present(renderer),
examples[2].present(renderer)
)
.trim_ascii()
.to_string(),
)
}
ParsingError::InvalidMultiline(_, _) => (
"Invalid multi-line string".to_string(),
r#"
Multi-line strings can be written by surrounding the content in triple
backticks:
```
In those days spirits were brave, the stakes were high, men were real men,
women were real women and small furry creatures from Alpha Centauri were
real small furry creatures from Alpha Centauri.
```
The leading and trailing newline will be trimmed. In addition, any whitespace
indenting the string will be removed. So
```bash
if [ -f /etc/passwd ]
then
echo "Found the password file"
fi
```
would result in a string with the `if` on the left margin but `echo` indented
by 4 spaces. This example also shows that the «language» of the content can be
specified. Doing so does not change the meaning of the multi-line string, but
it may be used by output templates when rendering the procedure.
"#
.trim_ascii()
.to_string(),
),
ParsingError::InvalidStep(_, _) => (
"Invalid step format".to_string(),
r#"
Steps must start with a number or lower-case letter (in the case of dependent
steps and sub-steps, respectively) followed by a '.':
1. First step
2. Second step
a. First substep
b. Second substep
Steps or substeps that can execute in parallel can instead be marked with a
dash. They can be done in either order, or concurrently:
- Do one thing
- And another
"#
.trim_ascii()
.to_string(),
),
ParsingError::InvalidSubstep(_, _) => (
"Invalid substep format".to_string(),
r#"
Substeps can be nested below top-level dependent steps or top-level parallel
steps. So both of these are valid:
1. First top-level step.
a. First substep in first dependent step.
b. Second substep in first dependent step.
and
- First top-level step to be done in any order.
a. First substep in first parallel step.
b. Second substep in first parallel step.
The ordinal must be a lowercase letter and not a roman numeral. By convention
substeps are indented by 4 characters, but that is not required.
Note also that the substeps can be consecutively numbered, which allows each
substep to be uniquely identified when they are grouped under different
parallel steps, but again this is not compulsory.
"#
.trim_ascii()
.to_string(),
),
ParsingError::InvalidAttribute(_, _) => {
let examples = vec![
Scope::AttributeBlock {
attributes: vec![
Attribute::Role(Identifier("president_of_the_galaxy")),
Attribute::Role(Identifier("femme_fatale")),
],
subscopes: vec![],
},
Scope::AttributeBlock {
attributes: vec![
Attribute::Place(Identifier("milliways")),
Attribute::Role(Identifier("waiter")),
Attribute::Role(Identifier("dish_of_the_day")),
],
subscopes: vec![],
},
];
(
"Invalid attribute assignment".to_string(),
format!(
r#"
Multiple attributes (be they role or place assignments) must be joined using
the '+' operator, for example:
{}
{}
Note that an attribute creates a scope, so sub-steps and code blocks can be
nested underneath a role or place assignment.
"#,
examples[0].present(renderer),
examples[1].present(renderer)
)
.trim_ascii()
.to_string(),
)
}
ParsingError::InvalidForeach(_, _) => {
let examples = vec![
Expression::Foreach(
vec![Identifier("patient")],
Box::new(Expression::Variable(Identifier("patients"))),
),
Expression::Foreach(
vec![Identifier("name"), Identifier("value")],
Box::new(Expression::Variable(Identifier("data"))),
),
];
(
"Invalid foreach loop".to_string(),
format!(
r#"
Loops follow this pattern:
{{ {} }}
{{ {} }}
In the first example `patients` would be a list; in the latter case `data` is
a list of tuples.
"#,
examples[0].present(renderer),
examples[1].present(renderer)
)
.trim_ascii()
.to_string(),
)
}
ParsingError::InvalidResponse(_, _) => {
let examples = vec![
vec![
Response {
value: "Rock",
condition: None,
},
Response {
value: "Paper",
condition: None,
},
Response {
value: "Scissors",
condition: None,
},
],
vec![Response {
value: "Confirmed",
condition: None,
}],
vec![
Response {
value: "Yes",
condition: Some("but with explanation"),
},
Response {
value: "No",
condition: None,
},
],
];
(
"Invalid response format".to_string(),
format!(
r#"
The fixed choices that are valid for the result of a given step can be
enumerated. These responses are each enclosed in single quotes, separated by
the '|' character:
{}
{}
{}
By convention the response values are Proper Case.
"#,
examples[0]
.iter()
.map(|r| r.present(renderer))
.collect::<Vec<_>>()
.join(" | "),
examples[1][0].present(renderer),
examples[2]
.iter()
.map(|r| r.present(renderer))
.collect::<Vec<_>>()
.join(" | ")
)
.trim_ascii()
.to_string(),
)
}
ParsingError::InvalidIntegral(_, _) => {
let examples = vec![
Numeric::Integral(42),
Numeric::Integral(-123),
Numeric::Integral(0),
Numeric::Integral(9223372036854775807),
];
(
"Invalid integer literal".to_string(),
format!(
r#"
Integer literals must be positive of negative whole numbers:
{}
{}
{}
{}
Integers cannot contain decimal points or units."#,
examples[0].present(renderer),
examples[1].present(renderer),
examples[2].present(renderer),
examples[3].present(renderer)
)
.trim_ascii()
.to_string(),
)
}
ParsingError::InvalidQuantity(_, _) => {
let examples = vec![
Numeric::Scientific(Quantity {
mantissa: Decimal {
number: 42,
precision: 1,
},
uncertainty: None,
magnitude: None,
symbol: "kg",
}),
Numeric::Scientific(Quantity {
mantissa: Decimal {
number: 100,
precision: 0,
},
uncertainty: None,
magnitude: None,
symbol: "m/s",
}),
Numeric::Scientific(Quantity {
mantissa: Decimal {
number: 420,
precision: 0,
},
uncertainty: Some(Decimal {
number: 10,
precision: 0,
}),
magnitude: None,
symbol: "kg",
}),
Numeric::Scientific(Quantity {
mantissa: Decimal {
number: 42,
precision: 1,
},
uncertainty: None,
magnitude: Some(2),
symbol: "kg",
}),
];
(
"Invalid quantity format".to_string(),
format!(
r#"
Quantities are measurements with units:
{}
{}
They can optionally have uncertainty:
{}
a magnitude:
{}"#,
examples[0].present(renderer),
examples[1].present(renderer),
examples[2].present(renderer),
examples[3].present(renderer)
)
.trim_ascii()
.to_string(),
)
}
ParsingError::InvalidQuantityDecimal(_, _) => (
"Invalid number in quantity".to_string(),
r#"
The numeric part of a quantity may be positive or negative, and may have a
decimal point:
42
-123.45
0.001
Values less than 1 must have a leading '0' before the decimal."#
.trim_ascii()
.to_string(),
),
ParsingError::InvalidQuantityUncertainty(_, _) => (
"Invalid uncertainty in quantity".to_string(),
r#"
Uncertainty values must be positive numbers:
4.2 ± 0.1 kg (valid)
100 +/- 5 m/s (valid)
You can use '±' or `+/-`, followed by a decimal."#
.trim_ascii()
.to_string(),
),
ParsingError::InvalidQuantityMagnitude(_, _) => (
"Invalid magnitude format".to_string(),
r#"
The magnitude of a quantity can be expressed in the usual scientific format
× 10ⁿ; for ease of writing you can use ASCII to write * or x and 10^n, as in
the following examples:
× 10^24
× 10²⁴
* 10^-6
x 10^12
The base must be 10, and the exponent must be an integer."#
.trim_ascii()
.to_string(),
),
ParsingError::InvalidQuantitySymbol(_, _) => {
let examples = vec![
Numeric::Scientific(Quantity {
mantissa: Decimal {
number: 84,
precision: 0,
},
uncertainty: None,
magnitude: None,
symbol: "kg",
}),
Numeric::Scientific(Quantity {
mantissa: Decimal {
number: 30,
precision: 1,
},
uncertainty: None,
magnitude: Some(8),
symbol: "m/s",
}),
Numeric::Scientific(Quantity {
mantissa: Decimal {
number: 16,
precision: 0,
},
uncertainty: Some(Decimal {
number: 15,
precision: 1,
}),
magnitude: None,
symbol: "°C",
}),
Numeric::Scientific(Quantity {
mantissa: Decimal {
number: 3126,
precision: 1,
},
uncertainty: None,
magnitude: None,
symbol: "μs",
}),
];
(
"Invalid character in quantity units".to_string(),
format!(
r#"
Symbols used to denote units can contain:
Letters 'A'..'z' {}
Rates '/': {}
Degrees '°': {}
SI prefixes 'μ': {}
Hyphens, underscores, spaces, or subscripts are not valid in unit symbols.
"#,
examples[0].present(renderer),
examples[1].present(renderer),
examples[2].present(renderer),
examples[3].present(renderer)
)
.trim_ascii()
.to_string(),
)
}
}
}