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,20 +39,58 @@ 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 ( )
38
- }
39
-
42
+ impl Dialect {
40
43
pub fn allow_cases ( & self ) -> bool {
41
- self . get_dialect ( ) != Dialect :: GAMESCRIPT
44
+ * self != Self :: GAMESCRIPT
42
45
}
43
46
44
47
fn allow_genders ( & self ) -> bool {
45
- self . get_dialect ( ) != Dialect :: GAMESCRIPT
48
+ * self != Self :: GAMESCRIPT
49
+ }
50
+
51
+ fn as_str ( & self ) -> & ' static str {
52
+ match self {
53
+ Self :: NEWGRF => "newgrf" ,
54
+ Self :: GAMESCRIPT => "game-script" ,
55
+ Self :: OPENTTD => "openttd" ,
56
+ }
46
57
}
47
58
}
48
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
+ }
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 ( |_| serde:: de:: Error :: unknown_variant ( string. as_str ( ) , & [ "game-script" , "newgrf" , "openttd" ] ) )
81
+ }
82
+ }
83
+
84
+ impl Serialize for Dialect {
85
+ fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
86
+ where
87
+ S : serde:: Serializer ,
88
+ {
89
+ serializer. serialize_str ( self . as_str ( ) )
90
+ }
91
+ }
92
+
93
+
49
94
impl Serialize for Severity {
50
95
fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
51
96
where
@@ -90,7 +135,7 @@ pub fn validate_base(config: &LanguageConfig, base: &String) -> ValidationResult
90
135
}
91
136
} else {
92
137
sanitize_whitespace ( & mut base) ;
93
- normalize_string ( & config. get_dialect ( ) , & mut base) ;
138
+ normalize_string ( & config. dialect , & mut base) ;
94
139
ValidationResult {
95
140
errors : errs,
96
141
normalized : Some ( base. compile ( ) ) ,
@@ -130,7 +175,7 @@ pub fn validate_translation(
130
175
Ok ( parsed) => parsed,
131
176
} ;
132
177
if case != "default" {
133
- if !config. allow_cases ( ) {
178
+ if !config. dialect . allow_cases ( ) {
134
179
return ValidationResult {
135
180
errors : vec ! [ ValidationError {
136
181
severity: Severity :: Error ,
@@ -177,7 +222,7 @@ pub fn validate_translation(
177
222
}
178
223
} else {
179
224
sanitize_whitespace ( & mut translation) ;
180
- normalize_string ( & config. get_dialect ( ) , & mut translation) ;
225
+ normalize_string ( & config. dialect , & mut translation) ;
181
226
ValidationResult {
182
227
errors : errs,
183
228
normalized : Some ( translation. compile ( ) ) ,
@@ -299,9 +344,8 @@ fn validate_string(
299
344
test : & ParsedString ,
300
345
base : Option < & ParsedString > ,
301
346
) -> Vec < ValidationError > {
302
- let dialect = config. get_dialect ( ) ;
303
347
let signature: StringSignature ;
304
- match get_signature ( & dialect, base. unwrap_or ( test) ) {
348
+ match get_signature ( & config . dialect , base. unwrap_or ( test) ) {
305
349
Ok ( sig) => signature = sig,
306
350
Err ( msgs) => {
307
351
if base. is_some ( ) {
@@ -335,10 +379,10 @@ fn validate_string(
335
379
. filter ( |ex| ex. get_norm_name ( ) == cmd. name )
336
380
. or ( COMMANDS
337
381
. into_iter ( )
338
- . find ( |ci| ci. name == cmd. name && ci. dialects . contains ( & dialect) ) ) ;
382
+ . find ( |ci| ci. name == cmd. name && ci. dialects . contains ( & config . dialect ) ) ) ;
339
383
if let Some ( info) = opt_info {
340
384
if let Some ( c) = & cmd. case {
341
- if !config. allow_cases ( ) {
385
+ if !config. dialect . allow_cases ( ) {
342
386
errors. push ( ValidationError {
343
387
severity : Severity :: Error ,
344
388
pos_begin : Some ( fragment. pos_begin ) ,
@@ -442,7 +486,7 @@ fn validate_string(
442
486
front = 2 ;
443
487
}
444
488
FragmentContent :: Gender ( g) => {
445
- if !config. allow_genders ( ) || config. genders . len ( ) < 2 {
489
+ if !config. dialect . allow_genders ( ) || config. genders . len ( ) < 2 {
446
490
errors. push ( ValidationError {
447
491
severity : Severity :: Error ,
448
492
pos_begin : Some ( fragment. pos_begin ) ,
@@ -497,7 +541,7 @@ fn validate_string(
497
541
_ => panic ! ( ) ,
498
542
} ;
499
543
let opt_ref_pos = cmd. indexref . or ( opt_ref_pos) ;
500
- if cmd. name == "G" && ( !config. allow_genders ( ) || config. genders . len ( ) < 2 ) {
544
+ if cmd. name == "G" && ( !config. dialect . allow_genders ( ) || config. genders . len ( ) < 2 ) {
501
545
errors. push ( ValidationError {
502
546
severity : Severity :: Error ,
503
547
pos_begin : Some ( fragment. pos_begin ) ,
@@ -881,7 +925,7 @@ mod tests {
881
925
#[ test]
882
926
fn test_validate_empty ( ) {
883
927
let config = LanguageConfig {
884
- dialect : String :: from ( "openttd" ) ,
928
+ dialect : Dialect :: OPENTTD ,
885
929
cases : vec ! [ ] ,
886
930
genders : vec ! [ ] ,
887
931
plural_count : 0 ,
@@ -898,7 +942,7 @@ mod tests {
898
942
#[ test]
899
943
fn test_validate_invalid ( ) {
900
944
let config = LanguageConfig {
901
- dialect : String :: from ( "openttd" ) ,
945
+ dialect : Dialect :: OPENTTD ,
902
946
cases : vec ! [ ] ,
903
947
genders : vec ! [ ] ,
904
948
plural_count : 0 ,
@@ -935,7 +979,7 @@ mod tests {
935
979
#[ test]
936
980
fn test_validate_positional ( ) {
937
981
let config = LanguageConfig {
938
- dialect : String :: from ( "openttd" ) ,
982
+ dialect : Dialect :: OPENTTD ,
939
983
cases : vec ! [ ] ,
940
984
genders : vec ! [ ] ,
941
985
plural_count : 0 ,
@@ -1036,7 +1080,7 @@ mod tests {
1036
1080
#[ test]
1037
1081
fn test_validate_front ( ) {
1038
1082
let config = LanguageConfig {
1039
- dialect : String :: from ( "openttd" ) ,
1083
+ dialect : Dialect :: OPENTTD ,
1040
1084
cases : vec ! [ ] ,
1041
1085
genders : vec ! [ String :: from( "a" ) , String :: from( "b" ) ] ,
1042
1086
plural_count : 0 ,
@@ -1119,7 +1163,7 @@ mod tests {
1119
1163
#[ test]
1120
1164
fn test_validate_position_references ( ) {
1121
1165
let config = LanguageConfig {
1122
- dialect : String :: from ( "openttd" ) ,
1166
+ dialect : Dialect :: OPENTTD ,
1123
1167
cases : vec ! [ String :: from( "x" ) , String :: from( "y" ) ] ,
1124
1168
genders : vec ! [ String :: from( "a" ) , String :: from( "b" ) ] ,
1125
1169
plural_count : 2 ,
@@ -1293,7 +1337,7 @@ mod tests {
1293
1337
#[ test]
1294
1338
fn test_validate_nochoices ( ) {
1295
1339
let config = LanguageConfig {
1296
- dialect : String :: from ( "openttd" ) ,
1340
+ dialect : Dialect :: OPENTTD ,
1297
1341
cases : vec ! [ ] ,
1298
1342
genders : vec ! [ ] ,
1299
1343
plural_count : 1 ,
@@ -1342,7 +1386,7 @@ mod tests {
1342
1386
#[ test]
1343
1387
fn test_validate_gschoices ( ) {
1344
1388
let config = LanguageConfig {
1345
- dialect : String :: from ( "game-script" ) ,
1389
+ dialect : Dialect :: GAMESCRIPT ,
1346
1390
cases : vec ! [ String :: from( "x" ) , String :: from( "y" ) ] ,
1347
1391
genders : vec ! [ String :: from( "a" ) , String :: from( "b" ) ] ,
1348
1392
plural_count : 2 ,
@@ -1391,7 +1435,7 @@ mod tests {
1391
1435
#[ test]
1392
1436
fn test_validate_choices ( ) {
1393
1437
let config = LanguageConfig {
1394
- dialect : String :: from ( "openttd" ) ,
1438
+ dialect : Dialect :: OPENTTD ,
1395
1439
cases : vec ! [ String :: from( "x" ) , String :: from( "y" ) ] ,
1396
1440
genders : vec ! [ String :: from( "a" ) , String :: from( "b" ) ] ,
1397
1441
plural_count : 2 ,
@@ -1455,7 +1499,7 @@ mod tests {
1455
1499
#[ test]
1456
1500
fn test_validate_nonpositional ( ) {
1457
1501
let config = LanguageConfig {
1458
- dialect : String :: from ( "openttd" ) ,
1502
+ dialect : Dialect :: OPENTTD ,
1459
1503
cases : vec ! [ ] ,
1460
1504
genders : vec ! [ ] ,
1461
1505
plural_count : 0 ,
0 commit comments