1
- use anyhow:: { anyhow, bail, Context } ;
2
1
use cargo_metadata:: Message ;
3
2
use clap:: { App , Arg , SubCommand } ;
3
+ use error:: Error ;
4
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,9 +13,11 @@ 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
- fn main ( ) -> anyhow :: Result < ( ) > {
20
+ fn main ( ) -> Result < ( ) > {
18
21
let mut app = App :: new ( env ! ( "CARGO_PKG_NAME" ) )
19
22
. bin_name ( "cargo" )
20
23
. subcommand (
@@ -84,7 +87,7 @@ fn main() -> anyhow::Result<()> {
84
87
let matches = match matches. subcommand_matches ( "espflash" ) {
85
88
Some ( matches) => matches,
86
89
None => {
87
- app. print_help ( ) ?;
90
+ app. print_help ( ) . into_diagnostic ( ) ?;
88
91
exit ( 0 ) ;
89
92
}
90
93
} ;
@@ -99,7 +102,7 @@ fn main() -> anyhow::Result<()> {
99
102
} else if let Some ( serial) = config. connection . serial {
100
103
serial
101
104
} else {
102
- app. print_help ( ) ?;
105
+ app. print_help ( ) . into_diagnostic ( ) ?;
103
106
exit ( 0 ) ;
104
107
} ;
105
108
@@ -120,15 +123,19 @@ fn main() -> anyhow::Result<()> {
120
123
// Attempt to open the serial port and set its initial baud rate.
121
124
println ! ( "Serial port: {}" , port) ;
122
125
println ! ( "Connecting...\n " ) ;
123
- let mut serial = serial:: open ( & port) . context ( format ! ( "Failed to open serial port {}" , port) ) ?;
124
- serial. reconfigure ( & |settings| {
125
- settings. set_baud_rate ( BaudRate :: Baud115200 ) ?;
126
- Ok ( ( ) )
127
- } ) ?;
126
+ let mut serial = serial:: open ( & port)
127
+ . map_err ( espflash:: Error :: from)
128
+ . wrap_err_with ( || format ! ( "Failed to open serial port {}" , port) ) ?;
129
+ serial
130
+ . reconfigure ( & |settings| {
131
+ settings. set_baud_rate ( BaudRate :: Baud115200 ) ?;
132
+ Ok ( ( ) )
133
+ } )
134
+ . into_diagnostic ( ) ?;
128
135
129
136
// Parse the baud rate if provided as as a command-line argument.
130
137
let speed = if let Some ( speed) = matches. value_of ( "speed" ) {
131
- let speed = speed. parse :: < usize > ( ) ?;
138
+ let speed = speed. parse :: < usize > ( ) . into_diagnostic ( ) ?;
132
139
Some ( BaudRate :: from_speed ( speed) )
133
140
} else {
134
141
None
@@ -145,8 +152,8 @@ fn main() -> anyhow::Result<()> {
145
152
// If the '--bootloader' option is provided, load the binary file at the
146
153
// specified path.
147
154
let bootloader = if let Some ( path) = matches. value_of ( "bootloader" ) {
148
- let path = fs:: canonicalize ( path) ?;
149
- let data = fs:: read ( path) ?;
155
+ let path = fs:: canonicalize ( path) . into_diagnostic ( ) ?;
156
+ let data = fs:: read ( path) . into_diagnostic ( ) ?;
150
157
Some ( data)
151
158
} else {
152
159
None
@@ -155,19 +162,16 @@ fn main() -> anyhow::Result<()> {
155
162
// If the '--partition-table' option is provided, load the partition table from
156
163
// the CSV at the specified path.
157
164
let partition_table = if let Some ( path) = matches. value_of ( "partition_table" ) {
158
- let path = fs:: canonicalize ( path) ?;
159
- let data = fs:: read_to_string ( path) ?;
160
-
161
- match PartitionTable :: try_from_str ( data) {
162
- Ok ( t) => Some ( t) ,
163
- Err ( e) => bail ! ( "{}" , e) ,
164
- }
165
+ let path = fs:: canonicalize ( path) . into_diagnostic ( ) ?;
166
+ let data = fs:: read_to_string ( path) . into_diagnostic ( ) ?;
167
+ let table = PartitionTable :: try_from_str ( data) ?;
168
+ Some ( table)
165
169
} else {
166
170
None
167
171
} ;
168
172
169
173
// Read the ELF data from the build path and load it to the target.
170
- let elf_data = fs:: read ( path. unwrap ( ) ) ?;
174
+ let elf_data = fs:: read ( path. unwrap ( ) ) . into_diagnostic ( ) ?;
171
175
if matches. is_present ( "ram" ) {
172
176
flasher. load_elf_to_ram ( & elf_data) ?;
173
177
} else {
@@ -183,16 +187,12 @@ fn board_info(flasher: &Flasher) {
183
187
println ! ( "Flash size: {}" , flasher. flash_size( ) ) ;
184
188
}
185
189
186
- fn build ( release : bool , example : Option < & str > , features : Option < & str > ) -> anyhow :: Result < PathBuf > {
190
+ fn build ( release : bool , example : Option < & str > , features : Option < & str > ) -> Result < PathBuf > {
187
191
// The 'build-std' unstable cargo feature is required to enable
188
192
// cross-compilation. If it has not been set then we cannot build the
189
193
// application.
190
194
if !has_build_std ( "." ) {
191
- bail ! (
192
- "cargo currently requires the unstable 'build-std' feature, ensure \
193
- that .cargo/config{.toml} has the appropriate options.\n \
194
- See: https://doc.rust-lang.org/cargo/reference/unstable.html#build-std"
195
- ) ;
195
+ return Err ( Error :: NoBuildStd . into ( ) ) ;
196
196
} ;
197
197
198
198
// Build the list of arguments to pass to 'cargo build'.
@@ -219,8 +219,10 @@ fn build(release: bool, example: Option<&str>, features: Option<&str>) -> anyhow
219
219
. args ( & [ "--message-format" , "json-diagnostic-rendered-ansi" ] )
220
220
. stdout ( Stdio :: piped ( ) )
221
221
. stderr ( Stdio :: inherit ( ) )
222
- . spawn ( ) ?
223
- . wait_with_output ( ) ?;
222
+ . spawn ( )
223
+ . into_diagnostic ( ) ?
224
+ . wait_with_output ( )
225
+ . into_diagnostic ( ) ?;
224
226
225
227
// Parse build output.
226
228
let messages = Message :: parse_stream ( & output. stdout [ ..] ) ;
@@ -229,12 +231,11 @@ fn build(release: bool, example: Option<&str>, features: Option<&str>) -> anyhow
229
231
let mut target_artifact = None ;
230
232
231
233
for message in messages {
232
- match message? {
234
+ match message. into_diagnostic ( ) ? {
233
235
Message :: CompilerArtifact ( artifact) => {
234
236
if artifact. executable . is_some ( ) {
235
237
if target_artifact. is_some ( ) {
236
- // We found multiple binary artifacts, so we don't know which one to use.
237
- bail ! ( "Multiple artifacts found, please specify one with --bin" ) ;
238
+ return Err ( Error :: MultipleArtifacts . into ( ) ) ;
238
239
} else {
239
240
target_artifact = Some ( artifact) ;
240
241
}
@@ -258,16 +259,9 @@ fn build(release: bool, example: Option<&str>, features: Option<&str>) -> anyhow
258
259
}
259
260
260
261
// If no target artifact was found, we don't have a path to return.
261
- if target_artifact. is_none ( ) {
262
- bail ! ( "Artifact not found" ) ;
263
- }
262
+ let target_artifact = target_artifact. ok_or ( Error :: NoArtifact ) ?;
264
263
265
- let artifact_path = PathBuf :: from (
266
- target_artifact
267
- . unwrap ( )
268
- . executable
269
- . ok_or_else ( || anyhow ! ( "artifact executable path is missing" ) ) ?,
270
- ) ;
264
+ let artifact_path = target_artifact. executable . unwrap ( ) . into ( ) ;
271
265
272
266
Ok ( artifact_path)
273
267
}
0 commit comments