1
- use crate :: commands:: { CommandInfo , Dialect , Occurence , COMMANDS } ;
1
+ use crate :: commands:: { CommandInfo , Occurence , COMMANDS } ;
2
2
use crate :: parser:: { FragmentContent , ParsedString } ;
3
3
use serde:: { Deserialize , Serialize } ;
4
4
use std:: collections:: { BTreeMap , HashMap } ;
5
5
6
+ #[ derive( Debug , PartialEq , Copy , Clone ) ]
7
+ pub enum Dialect {
8
+ NEWGRF ,
9
+ GAMESCRIPT ,
10
+ OPENTTD ,
11
+ }
12
+
6
13
#[ derive( Deserialize , Debug ) ]
7
14
pub struct LanguageConfig {
8
- pub dialect : String , //< "newgrf", "game-script", "openttd"
15
+ pub dialect : Dialect ,
9
16
pub cases : Vec < String > ,
10
17
pub genders : Vec < String > ,
11
18
pub plural_count : usize ,
@@ -32,17 +39,59 @@ pub struct ValidationResult {
32
39
pub normalized : Option < String > ,
33
40
}
34
41
35
- impl LanguageConfig {
36
- fn get_dialect ( & self ) -> Dialect {
37
- self . dialect . as_str ( ) . into ( )
42
+ impl Dialect {
43
+ pub fn allow_cases ( & self ) -> bool {
44
+ * self != Self :: GAMESCRIPT
45
+ }
46
+
47
+ pub fn allow_genders ( & self ) -> bool {
48
+ * self != Self :: GAMESCRIPT
38
49
}
39
50
40
- pub fn allow_cases ( & self ) -> bool {
41
- self . get_dialect ( ) != Dialect :: GAMESCRIPT
51
+ pub fn as_str ( & self ) -> & ' static str {
52
+ match self {
53
+ Self :: NEWGRF => "newgrf" ,
54
+ Self :: GAMESCRIPT => "game-script" ,
55
+ Self :: OPENTTD => "openttd" ,
56
+ }
57
+ }
58
+ }
59
+
60
+ impl TryFrom < & str > for Dialect {
61
+ type Error = String ;
62
+
63
+ fn try_from ( value : & str ) -> Result < Self , Self :: Error > {
64
+ match value {
65
+ "newgrf" => Ok ( Dialect :: NEWGRF ) ,
66
+ "game-script" => Ok ( Dialect :: GAMESCRIPT ) ,
67
+ "openttd" => Ok ( Dialect :: OPENTTD ) ,
68
+ _ => Err ( String :: from ( "Unknown dialect" ) ) ,
69
+ }
42
70
}
71
+ }
72
+
73
+ impl < ' de > Deserialize < ' de > for Dialect {
74
+ fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
75
+ where
76
+ D : serde:: Deserializer < ' de > ,
77
+ {
78
+ let string = String :: deserialize ( deserializer) ?;
79
+ let value = Dialect :: try_from ( string. as_str ( ) ) ;
80
+ value. map_err ( |_| {
81
+ serde:: de:: Error :: unknown_variant (
82
+ string. as_str ( ) ,
83
+ & [ "game-script" , "newgrf" , "openttd" ] ,
84
+ )
85
+ } )
86
+ }
87
+ }
43
88
44
- fn allow_genders ( & self ) -> bool {
45
- self . get_dialect ( ) != Dialect :: GAMESCRIPT
89
+ impl Serialize for Dialect {
90
+ fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
91
+ where
92
+ S : serde:: Serializer ,
93
+ {
94
+ serializer. serialize_str ( self . as_str ( ) )
46
95
}
47
96
}
48
97
@@ -90,7 +139,7 @@ pub fn validate_base(config: &LanguageConfig, base: &String) -> ValidationResult
90
139
}
91
140
} else {
92
141
sanitize_whitespace ( & mut base) ;
93
- normalize_string ( & config. get_dialect ( ) , & mut base) ;
142
+ normalize_string ( & config. dialect , & mut base) ;
94
143
ValidationResult {
95
144
errors : errs,
96
145
normalized : Some ( base. compile ( ) ) ,
@@ -130,7 +179,7 @@ pub fn validate_translation(
130
179
Ok ( parsed) => parsed,
131
180
} ;
132
181
if case != "default" {
133
- if !config. allow_cases ( ) {
182
+ if !config. dialect . allow_cases ( ) {
134
183
return ValidationResult {
135
184
errors : vec ! [ ValidationError {
136
185
severity: Severity :: Error ,
@@ -177,7 +226,7 @@ pub fn validate_translation(
177
226
}
178
227
} else {
179
228
sanitize_whitespace ( & mut translation) ;
180
- normalize_string ( & config. get_dialect ( ) , & mut translation) ;
229
+ normalize_string ( & config. dialect , & mut translation) ;
181
230
ValidationResult {
182
231
errors : errs,
183
232
normalized : Some ( translation. compile ( ) ) ,
@@ -299,9 +348,8 @@ fn validate_string(
299
348
test : & ParsedString ,
300
349
base : Option < & ParsedString > ,
301
350
) -> Vec < ValidationError > {
302
- let dialect = config. get_dialect ( ) ;
303
351
let signature: StringSignature ;
304
- match get_signature ( & dialect, base. unwrap_or ( test) ) {
352
+ match get_signature ( & config . dialect , base. unwrap_or ( test) ) {
305
353
Ok ( sig) => signature = sig,
306
354
Err ( msgs) => {
307
355
if base. is_some ( ) {
@@ -333,12 +381,12 @@ fn validate_string(
333
381
let opt_info =
334
382
opt_expected
335
383
. filter ( |ex| ex. get_norm_name ( ) == cmd. name )
336
- . or ( COMMANDS
337
- . into_iter ( )
338
- . find ( |ci| ci . name == cmd . name && ci . dialects . contains ( & dialect ) ) ) ;
384
+ . or ( COMMANDS . into_iter ( ) . find ( |ci| {
385
+ ci . name == cmd . name && ci . dialects . contains ( & config . dialect )
386
+ } ) ) ;
339
387
if let Some ( info) = opt_info {
340
388
if let Some ( c) = & cmd. case {
341
- if !config. allow_cases ( ) {
389
+ if !config. dialect . allow_cases ( ) {
342
390
errors. push ( ValidationError {
343
391
severity : Severity :: Error ,
344
392
pos_begin : Some ( fragment. pos_begin ) ,
@@ -442,7 +490,7 @@ fn validate_string(
442
490
front = 2 ;
443
491
}
444
492
FragmentContent :: Gender ( g) => {
445
- if !config. allow_genders ( ) || config. genders . len ( ) < 2 {
493
+ if !config. dialect . allow_genders ( ) || config. genders . len ( ) < 2 {
446
494
errors. push ( ValidationError {
447
495
severity : Severity :: Error ,
448
496
pos_begin : Some ( fragment. pos_begin ) ,
@@ -497,7 +545,8 @@ fn validate_string(
497
545
_ => panic ! ( ) ,
498
546
} ;
499
547
let opt_ref_pos = cmd. indexref . or ( opt_ref_pos) ;
500
- if cmd. name == "G" && ( !config. allow_genders ( ) || config. genders . len ( ) < 2 ) {
548
+ if cmd. name == "G" && ( !config. dialect . allow_genders ( ) || config. genders . len ( ) < 2 )
549
+ {
501
550
errors. push ( ValidationError {
502
551
severity : Severity :: Error ,
503
552
pos_begin : Some ( fragment. pos_begin ) ,
@@ -881,7 +930,7 @@ mod tests {
881
930
#[ test]
882
931
fn test_validate_empty ( ) {
883
932
let config = LanguageConfig {
884
- dialect : String :: from ( "openttd" ) ,
933
+ dialect : Dialect :: OPENTTD ,
885
934
cases : vec ! [ ] ,
886
935
genders : vec ! [ ] ,
887
936
plural_count : 0 ,
@@ -898,7 +947,7 @@ mod tests {
898
947
#[ test]
899
948
fn test_validate_invalid ( ) {
900
949
let config = LanguageConfig {
901
- dialect : String :: from ( "openttd" ) ,
950
+ dialect : Dialect :: OPENTTD ,
902
951
cases : vec ! [ ] ,
903
952
genders : vec ! [ ] ,
904
953
plural_count : 0 ,
@@ -935,7 +984,7 @@ mod tests {
935
984
#[ test]
936
985
fn test_validate_positional ( ) {
937
986
let config = LanguageConfig {
938
- dialect : String :: from ( "openttd" ) ,
987
+ dialect : Dialect :: OPENTTD ,
939
988
cases : vec ! [ ] ,
940
989
genders : vec ! [ ] ,
941
990
plural_count : 0 ,
@@ -1036,7 +1085,7 @@ mod tests {
1036
1085
#[ test]
1037
1086
fn test_validate_front ( ) {
1038
1087
let config = LanguageConfig {
1039
- dialect : String :: from ( "openttd" ) ,
1088
+ dialect : Dialect :: OPENTTD ,
1040
1089
cases : vec ! [ ] ,
1041
1090
genders : vec ! [ String :: from( "a" ) , String :: from( "b" ) ] ,
1042
1091
plural_count : 0 ,
@@ -1119,7 +1168,7 @@ mod tests {
1119
1168
#[ test]
1120
1169
fn test_validate_position_references ( ) {
1121
1170
let config = LanguageConfig {
1122
- dialect : String :: from ( "openttd" ) ,
1171
+ dialect : Dialect :: OPENTTD ,
1123
1172
cases : vec ! [ String :: from( "x" ) , String :: from( "y" ) ] ,
1124
1173
genders : vec ! [ String :: from( "a" ) , String :: from( "b" ) ] ,
1125
1174
plural_count : 2 ,
@@ -1293,7 +1342,7 @@ mod tests {
1293
1342
#[ test]
1294
1343
fn test_validate_nochoices ( ) {
1295
1344
let config = LanguageConfig {
1296
- dialect : String :: from ( "openttd" ) ,
1345
+ dialect : Dialect :: OPENTTD ,
1297
1346
cases : vec ! [ ] ,
1298
1347
genders : vec ! [ ] ,
1299
1348
plural_count : 1 ,
@@ -1342,7 +1391,7 @@ mod tests {
1342
1391
#[ test]
1343
1392
fn test_validate_gschoices ( ) {
1344
1393
let config = LanguageConfig {
1345
- dialect : String :: from ( "game-script" ) ,
1394
+ dialect : Dialect :: GAMESCRIPT ,
1346
1395
cases : vec ! [ String :: from( "x" ) , String :: from( "y" ) ] ,
1347
1396
genders : vec ! [ String :: from( "a" ) , String :: from( "b" ) ] ,
1348
1397
plural_count : 2 ,
@@ -1391,7 +1440,7 @@ mod tests {
1391
1440
#[ test]
1392
1441
fn test_validate_choices ( ) {
1393
1442
let config = LanguageConfig {
1394
- dialect : String :: from ( "openttd" ) ,
1443
+ dialect : Dialect :: OPENTTD ,
1395
1444
cases : vec ! [ String :: from( "x" ) , String :: from( "y" ) ] ,
1396
1445
genders : vec ! [ String :: from( "a" ) , String :: from( "b" ) ] ,
1397
1446
plural_count : 2 ,
@@ -1455,7 +1504,7 @@ mod tests {
1455
1504
#[ test]
1456
1505
fn test_validate_nonpositional ( ) {
1457
1506
let config = LanguageConfig {
1458
- dialect : String :: from ( "openttd" ) ,
1507
+ dialect : Dialect :: OPENTTD ,
1459
1508
cases : vec ! [ ] ,
1460
1509
genders : vec ! [ ] ,
1461
1510
plural_count : 0 ,
0 commit comments