Skip to content

Commit 0e7196c

Browse files
author
George Cosma
committed
wip
Signed-off-by: George Cosma <george.cosma@wyliodrin.com>
1 parent d25d23e commit 0e7196c

3 files changed

Lines changed: 62 additions & 40 deletions

File tree

tockloader-lib/src/connection.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,12 @@ impl Connection for ProbeRSConnection {
7878
let probe = self
7979
.debug_probe
8080
.open()
81-
.map_err(TockloaderError::ProbeRsInitializationError)?;
81+
.map_err(|e| TockloaderError::Probe(e.into()))?;
8282

8383
self.session = Some(
8484
probe
8585
.attach(&self.target_info.chip, Permissions::default())
86-
.map_err(TockloaderError::ProbeRsCommunicationError)?,
86+
.map_err(|e| TockloaderError::Probe(e.into()))?,
8787
);
8888

8989
Ok(())
@@ -129,7 +129,7 @@ impl Connection for SerialConnection {
129129
.timeout(self.target_info.timeout);
130130

131131
let mut stream =
132-
SerialStream::open(&builder).map_err(TockloaderError::SerialInitializationError)?;
132+
SerialStream::open(&builder).map_err(|e| TockloaderError::Serial(e.into()))?;
133133

134134
stream
135135
.write_request_to_send(self.target_info.request_to_send)

tockloader-lib/src/errors.rs

Lines changed: 57 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,55 +5,77 @@
55
use std::io;
66
use thiserror::Error;
77

8-
// TODO(george-cosma): Split this. Possibly each meta-function (install, list,
9-
// ...) should have its own error type + error type for connection. We can have
10-
// more detailed errors as "sources" for more generic error types, if we TRULY
11-
// need to know why probe-rs failed. Or serial.
12-
138
#[derive(Debug, Error)]
149
pub enum TockloaderError {
15-
#[error("Failed due to the connection not being open.")]
16-
ConnectionNotOpen,
17-
18-
#[error("Error occurred while trying to access core: {0}")]
19-
CoreAccessError(usize, probe_rs::Error),
20-
21-
#[error("Failed to initialize probe_rs connection due to a communication error. Inner: {0}")]
22-
ProbeRsInitializationError(#[from] probe_rs::probe::DebugProbeError),
10+
#[error("Serial connection error: {0}")]
11+
Serial(#[from] SerialError),
2312

24-
#[error("Failed to establish communication with board. Inner: {0}")]
25-
ProbeRsCommunicationError(probe_rs::Error),
13+
#[error("Probe connection error: {0}")]
14+
Probe(#[from] ProbeError),
2615

27-
#[error("Failed to read from debug probe. Inner: {0}")]
28-
ProbeRsReadError(probe_rs::Error),
16+
#[error("TAB file error: {0}")]
17+
Tab(#[from] TabError),
2918

30-
#[error("Failed to write binary. Inner: {0}")]
31-
ProbeRsWriteError(#[from] probe_rs::flashing::FlashError),
19+
#[error("Tock OS error: {0}")]
20+
Tock(#[from] TockError),
3221

33-
#[error("Failed to initialize serial connection due to a communication error. Inner: {0}")]
34-
SerialInitializationError(#[from] tokio_serial::Error),
22+
#[error("Internal tockloader error: {0}")]
23+
Internal(#[from] InternalError),
24+
}
3525

36-
#[error("Bootloader did not respond properly: {0}")]
37-
BootloaderError(u8),
26+
#[derive(Debug, Error)]
27+
pub enum SerialError {
28+
#[error("Failed to interface in serial using tokio_serial: {0}")]
29+
TokioSerial(#[from] tokio_serial::Error),
3830

39-
#[error("No binary found for {0} architecture.")]
40-
NoBinaryError(String),
31+
#[error("Failed to perform read/write operations on serial port: {0}")]
32+
IO(#[from] io::Error),
33+
}
4134

42-
#[error("App data could not be parsed.")]
43-
ParsingError(tbf_parser::types::TbfParseError),
35+
#[derive(Debug, Error)]
36+
pub enum ProbeError {
37+
#[error("Failed to interact with probe: {0}")]
38+
Probe(#[from] probe_rs::probe::DebugProbeError),
4439

45-
#[error("Failed to perform read/write operations on serial port. Inner: {0}")]
46-
IOError(#[from] io::Error),
40+
#[error("Communication with board failed: {0}")]
41+
Communication(#[from] probe_rs::Error),
4742

48-
#[error("Expected board attribute to be present")]
49-
MisconfiguredBoard(String),
43+
#[error("Failed to flash data: {0}")]
44+
Flashing(#[from] probe_rs::flashing::FlashError),
45+
}
5046

51-
#[error("Failed to use tab from provided path. Inner: {0}")]
52-
UnusableTab(io::Error),
47+
#[derive(Debug, Error)]
48+
pub enum TabError {
49+
#[error("Failed to use tab from provided path: {0}")]
50+
Unusable(io::Error),
5351

54-
#[error("Failed to parse metadata. Inner: {0}")]
52+
#[error("Failed to parse metadata: {0}")]
5553
InvalidMetadata(toml::de::Error),
5654

57-
#[error("No metadata.toml found.")]
55+
#[error("No metadata.toml found")]
5856
NoMetadata,
57+
58+
#[error("App data could not be parsed: {0:?}")]
59+
Parsing(tbf_parser::types::TbfParseError),
60+
61+
#[error("No binary found for {0} architecture")]
62+
NoBinary(String),
63+
}
64+
65+
#[derive(Debug, Error)]
66+
pub enum TockError {
67+
#[error("Bootloader did not respond properly: {0}")]
68+
Bootloader(u8),
69+
70+
#[error("Expected board attribute to be present: {0}")]
71+
MisconfiguredBoard(String),
72+
}
73+
74+
#[derive(Debug, Error)]
75+
pub enum InternalError {
76+
#[error("Operation failed due to board not being open.")]
77+
ConnectionNotOpen,
78+
79+
#[error("Operation failed due to board not being in bootloader mode or not having a bootloader present.")]
80+
BootloaderNotPresent,
5981
}

tockloader-lib/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ use tokio_serial::SerialPortInfo;
1818
use crate::attributes::app_attributes::AppAttributes;
1919
use crate::attributes::general_attributes::GeneralAttributes;
2020
use crate::board_settings::BoardSettings;
21-
use crate::errors::TockloaderError;
21+
use crate::errors::*;
2222
use crate::tabs::tab::Tab;
2323

2424
pub fn list_debug_probes() -> Vec<DebugProbeInfo> {
2525
probe_rs::probe::list::Lister::new().list_all()
2626
}
2727

2828
pub fn list_serial_ports() -> Result<Vec<SerialPortInfo>, TockloaderError> {
29-
tokio_serial::available_ports().map_err(TockloaderError::SerialInitializationError)
29+
tokio_serial::available_ports().map_err(|e| TockloaderError::Serial(e.into()))
3030
}
3131

3232
// TODO(george-cosma): Examine if we need to split these functions into smaller

0 commit comments

Comments
 (0)