Skip to content

Commit

Permalink
implemented from <err> to TockloaderError to remove the use of .map_err
Browse files Browse the repository at this point in the history
  • Loading branch information
george-cosma committed Sep 30, 2023
1 parent 0ed13ca commit 26a37a1
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 20 deletions.
16 changes: 13 additions & 3 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use core::fmt;

use tokio::task::JoinError;

#[derive(Debug)]
pub enum TockloaderError {
TokioSeriallError(tokio_serial::Error),
NoPortAvailable,
CLIError(CLIError),
IOError(std::io::Error),
JoinError(JoinError),
JoinError(tokio::task::JoinError),
}

#[derive(Debug)]
Expand Down Expand Up @@ -54,5 +52,17 @@ impl From<std::io::Error> for TockloaderError {
}
}

impl From<tokio_serial::Error> for TockloaderError {
fn from(value: tokio_serial::Error) -> Self {
Self::TokioSeriallError(value)
}
}

impl From<tokio::task::JoinError> for TockloaderError {
fn from(value: tokio::task::JoinError) -> Self {
Self::JoinError(value)
}
}

impl std::error::Error for TockloaderError {}
impl std::error::Error for CLIError {}
3 changes: 1 addition & 2 deletions src/interfaces/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ impl SerialInterface {
let port = if let Some(user_port) = args.get_one::<String>("port") {
user_port.clone()
} else {
let available_ports =
tokio_serial::available_ports().map_err(TockloaderError::TokioSeriallError)?;
let available_ports = tokio_serial::available_ports()?;

if available_ports.is_empty() {
return Err(TockloaderError::NoPortAvailable);
Expand Down
4 changes: 1 addition & 3 deletions src/interfaces/serial/board_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ use crate::interfaces::SerialInterface;
impl BoardInterface for SerialInterface {
fn open(&mut self) -> Result<(), TockloaderError> {
// Is it async? It can't be awaited...
let stream = tokio_serial::new(self.port.clone(), self.baud_rate)
.open_native_async()
.map_err(TockloaderError::TokioSeriallError)?;
let stream = tokio_serial::new(self.port.clone(), self.baud_rate).open_native_async()?;

self.stream = Some(stream);

Expand Down
15 changes: 3 additions & 12 deletions src/interfaces/serial/virtual_terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,27 +53,18 @@ impl VirtualTerminal for SerialInterface {

tokio::select! {
join_result = read_handle => {
match join_result {
Ok(read_handle_result) => read_handle_result,
Err(join_err) => Err(TockloaderError::JoinError(join_err)),
}
join_result?
}
join_result = write_handle => {
match join_result {
Ok(write_handle_result) => write_handle_result,
Err(join_err) => Err(TockloaderError::JoinError(join_err)),
}
join_result?
}
}
}
}

async fn get_key() -> Result<Option<String>, TockloaderError> {
let console_result = tokio::task::spawn_blocking(move || Term::stdout().read_key())
.await
.map_err(TockloaderError::JoinError)?;
let console_result = tokio::task::spawn_blocking(move || Term::stdout().read_key()).await?;

// Tockloader implements From<std::io::Error>, so we don't need to use 'map_err'
let key = console_result?;

Ok(match key {
Expand Down

0 comments on commit 26a37a1

Please sign in to comment.