Skip to content

Commit

Permalink
refactor: Error Handling (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
george-cosma authored Sep 19, 2024
1 parent 82eee80 commit 534d99b
Show file tree
Hide file tree
Showing 11 changed files with 480 additions and 504 deletions.
1 change: 1 addition & 0 deletions tockloader-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ tbf-parser = { path = "../tbf-parser"}
glob = "0.3.1"
inquire = "0.7.5"
tockloader-lib = { path = "../tockloader-lib/" }
anyhow = "1.0.89"
79 changes: 0 additions & 79 deletions tockloader-cli/src/errors.rs

This file was deleted.

155 changes: 73 additions & 82 deletions tockloader-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@

mod cli;
mod display;
mod errors;

use anyhow::{Context, Result};
use cli::make_cli;
use display::{print_info, print_list};
use errors::TockloaderError;
use inquire::Select;
use tockloader_lib::{
connection::{Connection, ConnectionInfo},
Expand All @@ -17,140 +16,132 @@ use tockloader_lib::{
};

#[tokio::main]
async fn main() -> Result<(), TockloaderError> {
let result = run().await;
if let Err(e) = &result {
eprintln!("\n{}", e);
}
result
}

async fn run() -> Result<(), TockloaderError> {
async fn main() -> Result<()> {
let matches = make_cli().get_matches();

if matches.get_flag("debug") {
println!("Debug mode enabled");
println!("Debug mode enabled.");
}

match matches.subcommand() {
Some(("listen", _sub_matches)) => {
match tock_process_console::run().await {
Ok(()) => {}
Err(_) => {
print!("cli bricked!")
}
};
tock_process_console::run()
.await
.context("Failed to run console.")?;
}
Some(("list", sub_matches)) => {
if *sub_matches.get_one::<bool>("serial").unwrap() {
let serial_ports = list_serial_ports();
// Let the user choose the port that will be used
let mut port_names = Vec::new();
for port in serial_ports {
port_names.push(port.port_name);
}
// TODO(Micu Ana): Add error handling
if sub_matches.get_one::<bool>("serial").is_some() {
let serial_ports = list_serial_ports().context("Failed to list serial ports.")?;
let port_names: Vec<_> = serial_ports.iter().map(|p| p.port_name.clone()).collect();
let ans = Select::new("Which serial port do you want to use?", port_names)
.prompt()
.unwrap();
// Open connection
let conn = Connection::open(ConnectionInfo::from(ans), None);
// Install app
let mut apps_details = list(conn.unwrap(), None).await.unwrap();
.context("No device is connected.")?;
let conn = Connection::open(ConnectionInfo::from(ans), None)
.context("Failed to open serial connection.")?;
let mut apps_details = list(conn, None).await.context("Failed to list apps.")?;
print_list(&mut apps_details).await;
} else {
// TODO(george-cosma):inspect-err
// TODO(Micu Ana): Add error handling
let ans = Select::new("Which debug probe do you want to use?", list_debug_probes())
.prompt();
// Open connection
.prompt()
.context("No debug probe is connected.")?;
let conn = Connection::open(
tockloader_lib::connection::ConnectionInfo::ProbeInfo(ans.unwrap()),
Some(sub_matches.get_one::<String>("chip").unwrap().to_string()),
);

let mut apps_details = list(conn.unwrap(), sub_matches.get_one::<usize>("core"))
ConnectionInfo::ProbeInfo(ans),
Some(
sub_matches
.get_one::<String>("chip")
.context("No chip has been provided.")?
.to_string(),
),
)
.context("Failed to open probe connection.")?;
let mut apps_details = list(conn, sub_matches.get_one::<usize>("core"))
.await
.unwrap();
.context("Failed to list apps.")?;
print_list(&mut apps_details).await;
}
}
Some(("info", sub_matches)) => {
if *sub_matches.get_one::<bool>("serial").unwrap() {
let serial_ports = list_serial_ports();
if sub_matches.get_one::<bool>("serial").is_some() {
let serial_ports = list_serial_ports().context("Failed to list serial ports.")?;
// Let the user choose the port that will be used
let mut port_names = Vec::new();
for port in serial_ports {
port_names.push(port.port_name);
}
// TODO(Micu Ana): Add error handling
let port_names: Vec<_> = serial_ports.iter().map(|p| p.port_name.clone()).collect();
let ans = Select::new("Which serial port do you want to use?", port_names)
.prompt()
.unwrap();
.context("No device is connected.")?;
// Open connection
let conn = Connection::open(ConnectionInfo::from(ans), None);
let mut attributes = info(conn.unwrap(), None).await.unwrap();
let conn = Connection::open(ConnectionInfo::from(ans), None)
.context("Failed to open serial connection.")?;
let mut attributes = info(conn, None)
.await
.context("Failed to get data from the board.")?;
print_info(&mut attributes.apps, &mut attributes.system).await;
} else {
// TODO(Micu Ana): Add error handling
let ans = Select::new("Which debug probe do you want to use?", list_debug_probes())
.prompt();
.prompt()
.context("No debug probe is connected.")?;
// Open connection
let conn = Connection::open(
tockloader_lib::connection::ConnectionInfo::ProbeInfo(ans.unwrap()),
Some(sub_matches.get_one::<String>("chip").unwrap().to_string()),
);
ConnectionInfo::ProbeInfo(ans),
Some(
sub_matches
.get_one::<String>("chip")
.context("No chip has been provided.")?
.to_string(),
),
)
.context("Failed to open probe connection.")?;

let mut attributes = info(conn.unwrap(), sub_matches.get_one::<usize>("core"))
let mut attributes = info(conn, sub_matches.get_one::<usize>("core"))
.await
.unwrap();
.context("Failed to get data from the board.")?;

print_info(&mut attributes.apps, &mut attributes.system).await;
}
}
Some(("install", sub_matches)) => {
let tab_file =
Tab::open(sub_matches.get_one::<String>("tab").unwrap().to_string()).unwrap();
let tab_file = Tab::open(sub_matches.get_one::<String>("tab").unwrap().to_string())
.context("Failed to use provided tab file.")?;
// If "--serial" flag is used, we choose the serial connection
if *sub_matches.get_one::<bool>("serial").unwrap() {
let serial_ports = list_serial_ports();
if sub_matches.get_one::<bool>("serial").is_some() {
let serial_ports = list_serial_ports().context("Failed to list serial ports.")?;
// Let the user choose the port that will be used
let mut port_names = Vec::new();
for port in serial_ports {
port_names.push(port.port_name);
}
// TODO(Micu Ana): Add error handling
let port_names: Vec<_> = serial_ports.iter().map(|p| p.port_name.clone()).collect();
let ans = Select::new("Which serial port do you want to use?", port_names)
.prompt()
.unwrap();
.context("No device is connected.")?;
// Open connection
let conn = Connection::open(ConnectionInfo::from(ans), None);
let conn = Connection::open(ConnectionInfo::from(ans), None)
.context("Failed to open serial connection.")?;
// Install app
install_app(conn.unwrap(), None, tab_file).await.unwrap();
install_app(conn, None, tab_file)
.await
.context("Failed to install app.")?;
} else {
// TODO(Micu Ana): Add error handling
let ans = Select::new("Which debug probe do you want to use?", list_debug_probes())
.prompt();
// Open connection
.prompt()
.context("No debug probe is connected.")?;
let conn = Connection::open(
tockloader_lib::connection::ConnectionInfo::ProbeInfo(ans.unwrap()),
Some(sub_matches.get_one::<String>("chip").unwrap().to_string()),
);
// Install app
install_app(
conn.unwrap(),
sub_matches.get_one::<usize>("core"),
tab_file,
ConnectionInfo::ProbeInfo(ans),
Some(
sub_matches
.get_one::<String>("chip")
.context("No chip has been provided.")?
.to_string(),
),
)
.await
.unwrap();
.context("Failed to open probe connection.")?;
// Install app
install_app(conn, sub_matches.get_one::<usize>("core"), tab_file)
.await
.context("Failed to install app.")?;
}
}
_ => {
println!("Could not run the provided subcommand.");
_ = make_cli().print_help();
}
}
// TODO(Micu Ana): Add error handling
Ok(())
}
1 change: 1 addition & 0 deletions tockloader-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ tar = "0.4.41"
bytes = "1.7.1"
toml = "0.8.19"
serde = { version = "1.0.210", features = ["derive"] }
thiserror = "1.0.63"
Loading

0 comments on commit 534d99b

Please sign in to comment.