1- use anyhow:: { anyhow, bail, Context } ;
21use cargo_metadata:: Message ;
32use clap:: { App , Arg , SubCommand } ;
3+ use error:: Error ;
44use espflash:: { Config , Flasher , PartitionTable } ;
5+ use miette:: { IntoDiagnostic , Result , WrapErr } ;
56use serial:: { BaudRate , SerialPort } ;
67
78use std:: {
@@ -12,9 +13,11 @@ use std::{
1213} ;
1314
1415mod cargo_config;
16+ mod error;
17+
1518use cargo_config:: has_build_std;
1619
17- fn main ( ) -> anyhow :: Result < ( ) > {
20+ fn main ( ) -> Result < ( ) > {
1821 let mut app = App :: new ( env ! ( "CARGO_PKG_NAME" ) )
1922 . bin_name ( "cargo" )
2023 . subcommand (
@@ -84,7 +87,7 @@ fn main() -> anyhow::Result<()> {
8487 let matches = match matches. subcommand_matches ( "espflash" ) {
8588 Some ( matches) => matches,
8689 None => {
87- app. print_help ( ) ?;
90+ app. print_help ( ) . into_diagnostic ( ) ?;
8891 exit ( 0 ) ;
8992 }
9093 } ;
@@ -99,7 +102,7 @@ fn main() -> anyhow::Result<()> {
99102 } else if let Some ( serial) = config. connection . serial {
100103 serial
101104 } else {
102- app. print_help ( ) ?;
105+ app. print_help ( ) . into_diagnostic ( ) ?;
103106 exit ( 0 ) ;
104107 } ;
105108
@@ -120,15 +123,19 @@ fn main() -> anyhow::Result<()> {
120123 // Attempt to open the serial port and set its initial baud rate.
121124 println ! ( "Serial port: {}" , port) ;
122125 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 ( ) ?;
128135
129136 // Parse the baud rate if provided as as a command-line argument.
130137 let speed = if let Some ( speed) = matches. value_of ( "speed" ) {
131- let speed = speed. parse :: < usize > ( ) ?;
138+ let speed = speed. parse :: < usize > ( ) . into_diagnostic ( ) ?;
132139 Some ( BaudRate :: from_speed ( speed) )
133140 } else {
134141 None
@@ -145,8 +152,8 @@ fn main() -> anyhow::Result<()> {
145152 // If the '--bootloader' option is provided, load the binary file at the
146153 // specified path.
147154 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 ( ) ?;
150157 Some ( data)
151158 } else {
152159 None
@@ -155,19 +162,16 @@ fn main() -> anyhow::Result<()> {
155162 // If the '--partition-table' option is provided, load the partition table from
156163 // the CSV at the specified path.
157164 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)
165169 } else {
166170 None
167171 } ;
168172
169173 // 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 ( ) ?;
171175 if matches. is_present ( "ram" ) {
172176 flasher. load_elf_to_ram ( & elf_data) ?;
173177 } else {
@@ -183,16 +187,12 @@ fn board_info(flasher: &Flasher) {
183187 println ! ( "Flash size: {}" , flasher. flash_size( ) ) ;
184188}
185189
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 > {
187191 // The 'build-std' unstable cargo feature is required to enable
188192 // cross-compilation. If it has not been set then we cannot build the
189193 // application.
190194 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 ( ) ) ;
196196 } ;
197197
198198 // 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
219219 . args ( & [ "--message-format" , "json-diagnostic-rendered-ansi" ] )
220220 . stdout ( Stdio :: piped ( ) )
221221 . stderr ( Stdio :: inherit ( ) )
222- . spawn ( ) ?
223- . wait_with_output ( ) ?;
222+ . spawn ( )
223+ . into_diagnostic ( ) ?
224+ . wait_with_output ( )
225+ . into_diagnostic ( ) ?;
224226
225227 // Parse build output.
226228 let messages = Message :: parse_stream ( & output. stdout [ ..] ) ;
@@ -229,12 +231,11 @@ fn build(release: bool, example: Option<&str>, features: Option<&str>) -> anyhow
229231 let mut target_artifact = None ;
230232
231233 for message in messages {
232- match message? {
234+ match message. into_diagnostic ( ) ? {
233235 Message :: CompilerArtifact ( artifact) => {
234236 if artifact. executable . is_some ( ) {
235237 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 ( ) ) ;
238239 } else {
239240 target_artifact = Some ( artifact) ;
240241 }
@@ -258,16 +259,9 @@ fn build(release: bool, example: Option<&str>, features: Option<&str>) -> anyhow
258259 }
259260
260261 // 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 ) ?;
264263
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 ( ) ;
271265
272266 Ok ( artifact_path)
273267}
0 commit comments