15
15
use cargo_toml:: { Manifest , Value } ;
16
16
use serde:: Deserialize ;
17
17
use std:: env;
18
+ use std:: io:: { Error , ErrorKind , Result } ;
18
19
use std:: path:: { Path , PathBuf } ;
19
20
20
21
const CONFIG_TABLE_NAME : & str = "config" ;
@@ -46,28 +47,34 @@ struct Toolchain {
46
47
mbed_archiver : Option < String > ,
47
48
}
48
49
49
- fn get_configuration_string ( parsec_config : & Value , key : & str ) -> String {
50
- let config_value = get_value_from_table ( parsec_config, key) ;
50
+ fn get_configuration_string ( parsec_config : & Value , key : & str ) -> Result < String > {
51
+ let config_value = get_value_from_table ( parsec_config, key) ? ;
51
52
match config_value {
52
- Value :: String ( string) => string. clone ( ) ,
53
- _ => panic ! ( "Cargo.toml does not contain configuration key: {}" , key) ,
53
+ Value :: String ( string) => Ok ( string. clone ( ) ) ,
54
+ _ => Err ( Error :: new (
55
+ ErrorKind :: InvalidInput ,
56
+ "Configuration key missing" ,
57
+ ) ) ,
54
58
}
55
59
}
56
60
57
- fn get_value_from_table < ' a > ( table : & ' a Value , key : & str ) -> & ' a Value {
61
+ fn get_value_from_table < ' a > ( table : & ' a Value , key : & str ) -> Result < & ' a Value > {
58
62
match table {
59
- Value :: Table ( table) => table. get ( key) . expect ( & format ! (
60
- "Config table does not contain configuration key: {}" ,
61
- key
63
+ Value :: Table ( table) => table. get ( key) . ok_or_else ( || {
64
+ println ! ( "Config table does not contain configuration key: {}" , key) ;
65
+ Error :: new ( ErrorKind :: InvalidInput , "Configuration key missing." )
66
+ } ) ,
67
+ _ => Err ( Error :: new (
68
+ ErrorKind :: InvalidInput ,
69
+ "Value provided is not a TOML table" ,
62
70
) ) ,
63
- _ => panic ! ( "Value provided is not a TOML table" ) ,
64
71
}
65
72
}
66
73
67
74
// Get the Mbed Crypto version to branch on from Cargo.toml file. Use that and MbedConfig to pass
68
75
// parameters to the setup_mbed_crypto.sh script which clones and builds Mbed Crypto and create
69
76
// a static library.
70
- fn setup_mbed_crypto ( mbed_config : & MbedConfig , mbed_version : & str ) {
77
+ fn setup_mbed_crypto ( mbed_config : & MbedConfig , mbed_version : & str ) -> Result < ( ) > {
71
78
let mut run_script = :: std:: process:: Command :: new ( SETUP_MBED_SCRIPT_PATH ) ;
72
79
run_script. arg ( mbed_version) . arg (
73
80
mbed_config
@@ -80,7 +87,15 @@ fn setup_mbed_crypto(mbed_config: &MbedConfig, mbed_version: &str) {
80
87
let mbed_compiler;
81
88
let mbed_archiver;
82
89
if std:: env:: var ( "TARGET" ) . unwrap ( ) == "aarch64-unknown-linux-gnu" {
83
- toolchain = mbed_config. aarch64_unknown_linux_gnu . as_ref ( ) . unwrap ( ) ;
90
+ toolchain = mbed_config
91
+ . aarch64_unknown_linux_gnu
92
+ . as_ref ( )
93
+ . ok_or_else ( || {
94
+ Error :: new (
95
+ ErrorKind :: InvalidInput ,
96
+ "The aarch64_unknown_linux_gnu subtable of mbed_config should exist" ,
97
+ )
98
+ } ) ?;
84
99
mbed_compiler = toolchain
85
100
. mbed_compiler
86
101
. clone ( )
@@ -90,7 +105,12 @@ fn setup_mbed_crypto(mbed_config: &MbedConfig, mbed_version: &str) {
90
105
. clone ( )
91
106
. unwrap_or ( DEFAULT_ARM64_MBED_ARCHIVER . to_string ( ) ) ;
92
107
} else {
93
- toolchain = mbed_config. native . as_ref ( ) . unwrap ( ) ;
108
+ toolchain = mbed_config. native . as_ref ( ) . ok_or_else ( || {
109
+ Error :: new (
110
+ ErrorKind :: InvalidInput ,
111
+ "The native subtable of mbed_config should exist" ,
112
+ )
113
+ } ) ?;
94
114
mbed_compiler = toolchain
95
115
. mbed_compiler
96
116
. clone ( )
@@ -106,14 +126,24 @@ fn setup_mbed_crypto(mbed_config: &MbedConfig, mbed_version: &str) {
106
126
107
127
if !run_script
108
128
. status ( )
109
- . expect ( "setup_mbed_crypto.sh script failed." )
129
+ . or_else ( |_| {
130
+ Err ( Error :: new (
131
+ ErrorKind :: Other ,
132
+ "setup_mbed_crypto.sh script failed" ,
133
+ ) )
134
+ } ) ?
110
135
. success ( )
111
136
{
112
- panic ! ( "setup_mbed_crypto.sh returned an error status." ) ;
137
+ Err ( Error :: new (
138
+ ErrorKind :: Other ,
139
+ "setup_mbed_crypto.sh returned an error status." ,
140
+ ) )
141
+ } else {
142
+ Ok ( ( ) )
113
143
}
114
144
}
115
145
116
- fn generate_mbed_bindings ( mbed_config : & MbedConfig , mbed_version : & str ) {
146
+ fn generate_mbed_bindings ( mbed_config : & MbedConfig , mbed_version : & str ) -> Result < ( ) > {
117
147
let mbed_include_dir = mbed_config
118
148
. mbed_path
119
149
. clone ( )
@@ -131,50 +161,75 @@ fn generate_mbed_bindings(mbed_config: &MbedConfig, mbed_version: &str) {
131
161
. header ( header)
132
162
. generate_comments ( false )
133
163
. generate ( )
134
- . expect ( "Unable to generate bindings to mbed crypto" ) ;
164
+ . or_else ( |_| {
165
+ Err ( Error :: new (
166
+ ErrorKind :: Other ,
167
+ "Unable to generate bindings to mbed crypto" ,
168
+ ) )
169
+ } ) ?;
135
170
136
171
let out_path = PathBuf :: from ( env:: var ( "OUT_DIR" ) . unwrap ( ) ) ;
137
- bindings
138
- . write_to_file ( out_path. join ( "psa_crypto_bindings.rs" ) )
139
- . expect ( & format ! ( "Couldn't write bindings to {:?}!" , out_path) ) ;
172
+ bindings. write_to_file ( out_path. join ( "psa_crypto_bindings.rs" ) )
140
173
}
141
174
142
175
// Get the compiler, the archiver and the location where to clone the Mbed Crypto repository.
143
- fn parse_config_file ( ) -> Configuration {
144
- let config_str = :: std:: fs:: read_to_string ( Path :: new ( BUILD_CONFIG_FILE_PATH ) )
145
- . expect ( "Could not read configuration file." ) ;
146
- toml:: from_str ( & config_str) . expect ( "Could not parse build configuration file." )
176
+ fn parse_config_file ( ) -> Result < Configuration > {
177
+ let config_str = :: std:: fs:: read_to_string ( Path :: new ( BUILD_CONFIG_FILE_PATH ) ) ?;
178
+ Ok ( toml:: from_str ( & config_str) . or_else ( |e| {
179
+ println ! ( "Error parsing build configuration file ({})." , e) ;
180
+ Err ( Error :: new (
181
+ ErrorKind :: InvalidInput ,
182
+ "Could not parse build configuration file." ,
183
+ ) )
184
+ } ) ?)
147
185
}
148
186
149
- fn main ( ) {
187
+ fn main ( ) -> Result < ( ) > {
150
188
// Parsing build-conf.toml
151
- let config = parse_config_file ( ) ;
189
+ let config = parse_config_file ( ) ? ;
152
190
153
191
// Parsing Cargo.toml
154
192
let toml_path = std:: path:: Path :: new ( "./Cargo.toml" ) ;
155
193
if !toml_path. exists ( ) {
156
- panic ! ( "Could not find Cargo.toml." ) ;
194
+ return Err ( Error :: new (
195
+ ErrorKind :: InvalidInput ,
196
+ "Could not find Cargo.toml." ,
197
+ ) ) ;
157
198
}
158
- let manifest = Manifest :: from_path ( & toml_path) . expect ( "Could not parse Cargo.toml." ) ;
159
-
160
- let package = manifest
161
- . package
162
- . expect ( "Cargo.toml does not contain package information." ) ;
163
- let metadata = package
164
- . metadata
165
- . expect ( "Cargo.toml does not contain package metadata." ) ;
166
- let parsec_config = get_value_from_table ( & metadata, CONFIG_TABLE_NAME ) ;
199
+ let manifest = Manifest :: from_path ( & toml_path) . or_else ( |e| {
200
+ println ! ( "Error parsing Cargo.toml ({})." , e) ;
201
+ Err ( Error :: new (
202
+ ErrorKind :: InvalidInput ,
203
+ "Could not parse Cargo.toml." ,
204
+ ) )
205
+ } ) ?;
206
+
207
+ let package = manifest. package . ok_or_else ( || {
208
+ Error :: new (
209
+ ErrorKind :: InvalidInput ,
210
+ "Cargo.toml does not contain package information." ,
211
+ )
212
+ } ) ?;
213
+ let metadata = package. metadata . ok_or_else ( || {
214
+ Error :: new (
215
+ ErrorKind :: InvalidInput ,
216
+ "Cargo.toml does not contain package metadata." ,
217
+ )
218
+ } ) ?;
219
+ let parsec_config = get_value_from_table ( & metadata, CONFIG_TABLE_NAME ) ?;
167
220
168
221
if cfg ! ( feature = "mbed-crypto-provider" ) {
169
- let mbed_config = config. mbed_config . expect ( & format ! (
170
- "Could not find mbed_config table in the {} file." ,
171
- BUILD_CONFIG_FILE_PATH
172
- ) ) ;
222
+ let mbed_config = config. mbed_config . ok_or_else ( || {
223
+ Error :: new (
224
+ ErrorKind :: InvalidInput ,
225
+ "Could not find mbed_config table in the config file." ,
226
+ )
227
+ } ) ?;
173
228
174
- let mbed_version = get_configuration_string ( & parsec_config, MBED_CRYPTO_VERSION_KEY ) ;
229
+ let mbed_version = get_configuration_string ( & parsec_config, MBED_CRYPTO_VERSION_KEY ) ? ;
175
230
176
- setup_mbed_crypto ( & mbed_config, & mbed_version) ;
177
- generate_mbed_bindings ( & mbed_config, & mbed_version) ;
231
+ setup_mbed_crypto ( & mbed_config, & mbed_version) ? ;
232
+ generate_mbed_bindings ( & mbed_config, & mbed_version) ? ;
178
233
179
234
// Request rustc to link the Mbed Crypto static library
180
235
println ! (
@@ -186,4 +241,6 @@ fn main() {
186
241
) ;
187
242
println ! ( "cargo:rustc-link-lib=static=mbedcrypto" ) ;
188
243
}
244
+
245
+ Ok ( ( ) )
189
246
}
0 commit comments