1
1
use cargo_metadata:: Message ;
2
2
use clap:: { App , Arg , SubCommand } ;
3
- use espflash:: { Config , Error , Flasher , PartitionTable } ;
4
- use miette:: { bail, miette, IntoDiagnostic , Result , WrapErr } ;
3
+ use error:: Error ;
4
+ use espflash:: { Config , Flasher , PartitionTable } ;
5
+ use miette:: { IntoDiagnostic , Result , WrapErr } ;
5
6
use serial:: { BaudRate , SerialPort } ;
6
7
7
8
use std:: {
@@ -12,6 +13,8 @@ use std::{
12
13
} ;
13
14
14
15
mod cargo_config;
16
+ mod error;
17
+
15
18
use cargo_config:: has_build_std;
16
19
17
20
fn main ( ) -> Result < ( ) > {
@@ -121,7 +124,7 @@ fn main() -> Result<()> {
121
124
println ! ( "Serial port: {}" , port) ;
122
125
println ! ( "Connecting...\n " ) ;
123
126
let mut serial = serial:: open ( & port)
124
- . map_err ( Error :: from)
127
+ . map_err ( espflash :: Error :: from)
125
128
. wrap_err_with ( || format ! ( "Failed to open serial port {}" , port) ) ?;
126
129
serial
127
130
. reconfigure ( & |settings| {
@@ -161,11 +164,8 @@ fn main() -> Result<()> {
161
164
let partition_table = if let Some ( path) = matches. value_of ( "partition_table" ) {
162
165
let path = fs:: canonicalize ( path) . into_diagnostic ( ) ?;
163
166
let data = fs:: read_to_string ( path) . into_diagnostic ( ) ?;
164
-
165
- match PartitionTable :: try_from_str ( data) {
166
- Ok ( t) => Some ( t) ,
167
- Err ( e) => bail ! ( "{}" , e) ,
168
- }
167
+ let table = PartitionTable :: try_from_str ( data) ?;
168
+ Some ( table)
169
169
} else {
170
170
None
171
171
} ;
@@ -192,11 +192,7 @@ fn build(release: bool, example: Option<&str>, features: Option<&str>) -> Result
192
192
// cross-compilation. If it has not been set then we cannot build the
193
193
// application.
194
194
if !has_build_std ( "." ) {
195
- bail ! (
196
- "cargo currently requires the unstable 'build-std' feature, ensure \
197
- that .cargo/config{.toml} has the appropriate options.\n \
198
- See: https://doc.rust-lang.org/cargo/reference/unstable.html#build-std"
199
- ) ;
195
+ return Err ( Error :: NoBuildStd . into ( ) ) ;
200
196
} ;
201
197
202
198
// Build the list of arguments to pass to 'cargo build'.
@@ -239,8 +235,7 @@ fn build(release: bool, example: Option<&str>, features: Option<&str>) -> Result
239
235
Message :: CompilerArtifact ( artifact) => {
240
236
if artifact. executable . is_some ( ) {
241
237
if target_artifact. is_some ( ) {
242
- // We found multiple binary artifacts, so we don't know which one to use.
243
- bail ! ( "Multiple artifacts found, please specify one with --bin" ) ;
238
+ return Err ( Error :: MultipleArtifacts . into ( ) ) ;
244
239
} else {
245
240
target_artifact = Some ( artifact) ;
246
241
}
@@ -264,16 +259,9 @@ fn build(release: bool, example: Option<&str>, features: Option<&str>) -> Result
264
259
}
265
260
266
261
// If no target artifact was found, we don't have a path to return.
267
- if target_artifact. is_none ( ) {
268
- bail ! ( "Artifact not found" ) ;
269
- }
262
+ let target_artifact = target_artifact. ok_or ( Error :: NoArtifact ) ?;
270
263
271
- let artifact_path = PathBuf :: from (
272
- target_artifact
273
- . unwrap ( )
274
- . executable
275
- . ok_or_else ( || miette ! ( "artifact executable path is missing" ) ) ?,
276
- ) ;
264
+ let artifact_path = target_artifact. executable . unwrap ( ) . into ( ) ;
277
265
278
266
Ok ( artifact_path)
279
267
}
0 commit comments