From 25f695d20a8ee3462aa5be6012fbe75fba083507 Mon Sep 17 00:00:00 2001 From: Paul Bender Date: Sat, 11 Oct 2025 08:35:24 -0700 Subject: [PATCH] Replace some_or_invalid_error with idiomatic Rust. --- src/ds323x/datetime.rs | 23 ++++++++++++----------- src/ds323x/mod.rs | 24 ------------------------ 2 files changed, 12 insertions(+), 35 deletions(-) diff --git a/src/ds323x/datetime.rs b/src/ds323x/datetime.rs index 3491ea7..d2cbb90 100644 --- a/src/ds323x/datetime.rs +++ b/src/ds323x/datetime.rs @@ -1,7 +1,7 @@ //! Common implementation use super::{ - decimal_to_packed_bcd, hours_to_register, packed_bcd_to_decimal, some_or_invalid_error, + decimal_to_packed_bcd, hours_to_register, packed_bcd_to_decimal, }; use crate::{ interface::{ReadData, WriteData}, @@ -27,14 +27,15 @@ where let year = 2000 + (packed_bcd_to_decimal(data[Register::YEAR as usize + 1]) as u16); let month = packed_bcd_to_decimal(data[Register::MONTH as usize + 1]); let day = packed_bcd_to_decimal(data[Register::DOM as usize + 1]); - let hour = hours_from_register(data[Register::HOURS as usize + 1]); + let hour = get_h24(hours_from_register(data[Register::HOURS as usize + 1])); let minute = packed_bcd_to_decimal(data[Register::MINUTES as usize + 1]); let second = packed_bcd_to_decimal(data[Register::SECONDS as usize + 1]); - let date = NaiveDate::from_ymd_opt(year.into(), month.into(), day.into()); - let date = some_or_invalid_error(date)?; - let datetime = date.and_hms_opt(get_h24(hour).into(), minute.into(), second.into()); - some_or_invalid_error(datetime) + let date = NaiveDate::from_ymd_opt(year.into(), month.into(), day.into()).ok_or(Error::InvalidDeviceState)?; + let time = NaiveTime::from_hms_opt(hour.into(), minute.into(), second.into()).ok_or(Error::InvalidDeviceState)?; + let datetime = NaiveDateTime::new(date, time); + + Ok(datetime) } fn set_datetime(&mut self, datetime: &NaiveDateTime) -> Result<(), Self::Error> { @@ -75,12 +76,12 @@ where fn time(&mut self) -> Result { let mut data = [0; 4]; self.iface.read_data(&mut data)?; - let hour = hours_from_register(data[Register::HOURS as usize + 1]); + + let hour = get_h24(hours_from_register(data[Register::HOURS as usize + 1])); let minute = packed_bcd_to_decimal(data[Register::MINUTES as usize + 1]); let second = packed_bcd_to_decimal(data[Register::SECONDS as usize + 1]); - let time = NaiveTime::from_hms_opt(get_h24(hour).into(), minute.into(), second.into()); - some_or_invalid_error(time) + NaiveTime::from_hms_opt(hour.into(), minute.into(), second.into()).ok_or(Error::InvalidDeviceState) } fn weekday(&mut self) -> Result { @@ -133,8 +134,8 @@ where 2000 + (packed_bcd_to_decimal(data[Register::YEAR as usize + 1 - offset]) as u16); let month = packed_bcd_to_decimal(data[Register::MONTH as usize + 1 - offset]); let day = packed_bcd_to_decimal(data[Register::DOM as usize + 1 - offset]); - let date = NaiveDate::from_ymd_opt(year.into(), month.into(), day.into()); - some_or_invalid_error(date) + + NaiveDate::from_ymd_opt(year.into(), month.into(), day.into()).ok_or(Error::InvalidDeviceState) } fn set_seconds(&mut self, seconds: u8) -> Result<(), Self::Error> { diff --git a/src/ds323x/mod.rs b/src/ds323x/mod.rs index 63c59ba..3543a9c 100644 --- a/src/ds323x/mod.rs +++ b/src/ds323x/mod.rs @@ -28,34 +28,10 @@ fn hours_to_register(hours: Hours) -> Result> { } } -fn some_or_invalid_error(data: Option) -> Result> { - if let Some(data) = data { - Ok(data) - } else { - Err(Error::InvalidDeviceState) - } -} - #[cfg(test)] mod tests { use super::*; - #[test] - fn if_some_then_get_inner() { - match some_or_invalid_error::(Some(1)) { - Ok(1) => (), - _ => panic!(), - } - } - - #[test] - fn if_none_then_error() { - match some_or_invalid_error::(None) { - Err(Error::InvalidDeviceState) => (), - _ => panic!(), - } - } - #[test] fn can_convert_packed_bcd_to_decimal() { assert_eq!(0, packed_bcd_to_decimal(0b0000_0000));