From 8de6c8db61f781ca2a715b047374d4728da6848d Mon Sep 17 00:00:00 2001 From: Alex Janka Date: Fri, 12 Jan 2024 09:32:43 +1100 Subject: [PATCH] better pli error propagation --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/errors.rs | 2 ++ src/pl_interface.rs | 8 ++++---- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 763ec42..c8a2db8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2256,7 +2256,7 @@ dependencies = [ [[package]] name = "tesla-charge-controller" -version = "0.1.18" +version = "0.1.19" dependencies = [ "async-channel", "chrono", diff --git a/Cargo.toml b/Cargo.toml index fa3d80a..a93eb7e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tesla-charge-controller" -version = "0.1.18" +version = "0.1.19" edition = "2021" license = "MITNFA" description = "Controls Tesla charge rate based on solar charge data" diff --git a/src/errors.rs b/src/errors.rs index dbd3dc4..f2eb99a 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -60,6 +60,8 @@ pub enum RequestError { pub enum PliError { #[error("read error")] ReadError(u8), + #[error("io error")] + StdioErr(#[from] std::io::Error), } #[derive(Error, Debug)] diff --git a/src/pl_interface.rs b/src/pl_interface.rs index 3d5143c..63b0aab 100644 --- a/src/pl_interface.rs +++ b/src/pl_interface.rs @@ -206,10 +206,10 @@ impl Pli { .expect("failed to write to serial port"); } - fn receive(&mut self) -> [u8; LENGTH] { + fn receive(&mut self) -> Result<[u8; LENGTH], PliError> { let mut buf = [0; LENGTH]; - let _ = self.port.read_exact(&mut buf).some_or_print(); - buf + self.port.read_exact(&mut buf)?; + Ok(buf) } fn read_ram(&mut self, address: T) -> Result @@ -217,7 +217,7 @@ impl Pli { T: Into, { self.send_command(command(20, address.into(), 0)); - let buf: [u8; 2] = self.receive(); + let buf: [u8; 2] = self.receive()?; if buf[0] == 200 { Ok(buf[1]) } else {