Fix lint issues and add comments

Lint issues:
- spelling problems
- iter instead of into_iter
This commit is contained in:
Robert Sammelson 2025-03-20 02:17:56 -04:00
parent db4b4d990d
commit 1171380877
No known key found for this signature in database
GPG key ID: BEA2C00D29709D51
6 changed files with 21 additions and 7 deletions

View file

@ -42,7 +42,7 @@ impl From<u16> for Dtc {
1 => Dtc::Chassis(n), 1 => Dtc::Chassis(n),
2 => Dtc::Body(n), 2 => Dtc::Body(n),
3 => Dtc::Network(n), 3 => Dtc::Network(n),
_ => unreachable!(), _ => unreachable!(), // can't happen, only two bits
} }
} }
} }

View file

@ -23,6 +23,10 @@ pub struct Elm327 {
} }
impl Default for Elm327 { impl Default for Elm327 {
/// Create a Elm327 device
///
/// # Panics
/// If the device cannot be initialized. Use [Self::new] for a panic-free API.
fn default() -> Self { fn default() -> Self {
Elm327::new().unwrap() Elm327::new().unwrap()
} }
@ -40,7 +44,7 @@ impl Obd2BaseDevice for Elm327 {
fn send_cmd(&mut self, data: &[u8]) -> Result<()> { fn send_cmd(&mut self, data: &[u8]) -> Result<()> {
trace!("send_cmd: sending {:?}", std::str::from_utf8(data)); trace!("send_cmd: sending {:?}", std::str::from_utf8(data));
self.send_serial_str( self.send_serial_str(
data.into_iter() data.iter()
.flat_map(|v| format!("{:02X}", v).chars().collect::<Vec<char>>()) .flat_map(|v| format!("{:02X}", v).chars().collect::<Vec<char>>())
.collect::<String>() .collect::<String>()
.as_str(), .as_str(),
@ -134,15 +138,22 @@ impl Elm327 {
fn reset_protocol(&mut self) -> Result<()> { fn reset_protocol(&mut self) -> Result<()> {
info!("Performing protocol reset"); info!("Performing protocol reset");
// set to use automatic protocol selection
debug!( debug!(
"reset_protocol: got response {:?}", "reset_protocol: got response {:?}",
self.serial_cmd("ATSP0")? self.serial_cmd("ATSP0")?
); );
// perform the search
debug!( debug!(
"reset_protocol: got OBD response {:?}", "reset_protocol: got OBD response {:?}",
self.cmd(&[0x01, 0x00])? self.cmd(&[0x01, 0x00])?
); );
// get rid of extra data hanging around in the buffer
self.flush_buffers()?; self.flush_buffers()?;
Ok(()) Ok(())
} }

View file

@ -27,12 +27,12 @@ impl From<super::device::Error> for Error {
impl From<std::num::ParseIntError> for Error { impl From<std::num::ParseIntError> for Error {
fn from(e: std::num::ParseIntError) -> Self { fn from(e: std::num::ParseIntError) -> Self {
Error::Other(format!("invalid data recieved: {:?}", e)) Error::Other(format!("invalid data received: {:?}", e))
} }
} }
impl From<std::string::FromUtf8Error> for Error { impl From<std::string::FromUtf8Error> for Error {
fn from(e: std::string::FromUtf8Error) -> Self { fn from(e: std::string::FromUtf8Error) -> Self {
Error::Other(format!("invalid string recieved: {:?}", e)) Error::Other(format!("invalid string received: {:?}", e))
} }
} }

View file

@ -17,9 +17,11 @@ impl<T: Obd2BaseDevice> Obd2Device for Obd2<T> {
for response in result.iter() { for response in result.iter() {
if response.first() != Some(&(0x40 | mode)) { if response.first() != Some(&(0x40 | mode)) {
// mismatch of mode in response
todo!() todo!()
} }
if response.get(1) != Some(&pid) { if response.get(1) != Some(&pid) {
// mismatch of PID in response
todo!() todo!()
} }
} }
@ -109,7 +111,8 @@ impl<T: Obd2BaseDevice> Obd2<T> {
.filter_map(|l| l.split_once(':')) .filter_map(|l| l.split_once(':'))
.flat_map(|(idx, data)| { .flat_map(|(idx, data)| {
if u8::from_str_radix(idx, 16) != Ok(n_idx) { if u8::from_str_radix(idx, 16) != Ok(n_idx) {
todo!() // got an invalid hex code or values were not already in the correct order
todo!("Line index: {}, should be {:X}", idx, n_idx)
} }
n_idx = (n_idx + 1) % 0x10; n_idx = (n_idx + 1) % 0x10;
data.split_whitespace().map(|s| s.to_owned()) data.split_whitespace().map(|s| s.to_owned())

View file

@ -16,7 +16,7 @@
//! ``` //! ```
#![forbid(unsafe_code)] #![forbid(unsafe_code)]
#![warn(missing_docs)] #![warn(missing_docs, clippy::panic)]
pub mod commands; pub mod commands;

View file

@ -13,7 +13,7 @@ pub trait Obd2Device {
/// ///
/// The responses are a list with one element for each ECU that responds. The data is decoded /// The responses are a list with one element for each ECU that responds. The data is decoded
/// into the ODB-II bytes from the vehicle and the first byte of the response---representing /// into the ODB-II bytes from the vehicle and the first byte of the response---representing
/// the mode the vehicle recieved---is validated and removed. /// the mode the vehicle received---is validated and removed.
fn obd_mode_command(&mut self, mode: u8) -> Result<Vec<Vec<u8>>>; fn obd_mode_command(&mut self, mode: u8) -> Result<Vec<Vec<u8>>>;
/// Send command and get list of OBD-II responses as an array /// Send command and get list of OBD-II responses as an array