@@ -11,58 +11,29 @@ fn main() {
11
11
12
12
let mut rust_code = String :: from ( "// Auto-generated code from versions.toml\n \n " ) ;
13
13
14
- fn generate_constants ( value : & Value , prefix : & str , code : & mut String ) {
15
- match value {
16
- Value :: Table ( table) => {
17
- for ( key, val) in table {
18
- let new_prefix = if prefix. is_empty ( ) {
19
- key. to_string ( )
20
- } else {
21
- format ! ( "{}_{}" , prefix, key)
22
- } ;
23
- generate_constants ( val, & new_prefix, code) ;
24
- }
25
- }
26
- Value :: Array ( arr) => {
27
- code. push_str ( & format ! (
28
- "pub const {}: &[&str] = &[{}];\n " ,
29
- prefix. to_uppercase( ) ,
30
- arr. iter( )
31
- . map( |v| format!( "\" {}\" " , v. as_str( ) . unwrap_or( "" ) ) )
32
- . collect:: <Vec <_>>( )
33
- . join( ", " )
34
- ) ) ;
35
- }
36
- _ => {
37
- let const_value = match value {
38
- Value :: String ( s) => format ! ( "\" {}\" " , s) ,
39
- Value :: Integer ( n) => n. to_string ( ) ,
40
- Value :: Float ( f) => f. to_string ( ) ,
41
- Value :: Boolean ( b) => b. to_string ( ) ,
42
- _ => "\" \" " . to_string ( ) ,
14
+ match config {
15
+ Value :: Table ( table) => {
16
+ for ( key, val) in table {
17
+ match val {
18
+ Value :: String ( s) => {
19
+ let const_value = format ! ( "\" {}\" " , s) ;
20
+ rust_code. push_str ( & format ! (
21
+ "pub const {}: &str = {};\n " ,
22
+ key. to_uppercase( ) ,
23
+ const_value
24
+ ) ) ;
25
+ }
26
+ _ => {
27
+ panic ! ( "Invalid value type in versions.toml: {:?}" , val) ;
28
+ }
43
29
} ;
44
- code. push_str ( & format ! (
45
- "pub const {}: {} = {};\n " ,
46
- prefix. to_uppercase( ) ,
47
- if value. is_str( ) {
48
- "&str"
49
- } else if value. is_integer( ) {
50
- "i64"
51
- } else if value. is_float( ) {
52
- "f64"
53
- } else if value. is_bool( ) {
54
- "bool"
55
- } else {
56
- "&str"
57
- } ,
58
- const_value
59
- ) ) ;
60
30
}
61
31
}
32
+ _ => {
33
+ panic ! ( "Invalid value type in versions.toml: {:?}" , config) ;
34
+ }
62
35
}
63
36
64
- generate_constants ( & config, "" , & mut rust_code) ;
65
-
66
37
let out_dir = env:: var_os ( "OUT_DIR" ) . unwrap ( ) ;
67
38
let dest_path = Path :: new ( & out_dir) . join ( "versions.rs" ) ;
68
39
fs:: write ( & dest_path, rust_code) . expect ( "Failed to write generated code" ) ;
0 commit comments