Skip to content

Commit

Permalink
remove "expect" from lib code
Browse files Browse the repository at this point in the history
clean up abstractions a bit more
  • Loading branch information
cujomalainey committed May 19, 2024
1 parent 9f6cff1 commit 9ca5077
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 37 deletions.
8 changes: 4 additions & 4 deletions ant/examples/mac_usb_hr_display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ struct RxReceiver<T> {
}

impl<T: Default + Clone> TxHandler<T> for TxSender<T> {
fn try_send(&self, msg: T) -> Result<(), TxError<T>> {
fn try_send(&self, msg: T) -> Result<(), TxError> {
match self.sender.try_send(msg) {
Ok(_) => Ok(()),
Err(TrySendError::Full(m)) => Err(TxError::Full(m)),
Err(TrySendError::Closed(m)) => Err(TxError::Closed(m)),
Err(TrySendError::Full(_)) => Err(TxError::Full),
Err(TrySendError::Closed(_)) => Err(TxError::Closed),
Err(_) => Err(TxError::UnknownError),
}
}
Expand Down Expand Up @@ -111,6 +111,6 @@ fn main() -> std::io::Result<()> {
hr.open();
loop {
router.process().unwrap();
hr.process();
hr.process().unwrap();
}
}
26 changes: 22 additions & 4 deletions ant/src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,33 @@ pub enum RxError {
}

#[derive(Clone, Debug)]
pub enum TxError<T> {
Full(T),
Closed(T),
pub enum TxError {
Full,
Closed,
UnknownError,
}

#[derive(Clone, Debug)]
pub enum ChanError {
Rx(RxError),
Tx(TxError),
}

impl From<RxError> for ChanError {
fn from(err: RxError) -> ChanError {
ChanError::Rx(err)
}
}

impl From<TxError> for ChanError {
fn from(err: TxError) -> ChanError {
ChanError::Tx(err)
}
}

pub trait TxHandler<T> {
// TODO async versions
fn try_send(&self, msg: T) -> Result<(), TxError<T>>;
fn try_send(&self, msg: T) -> Result<(), TxError>;
}

pub trait RxHandler<T> {
Expand Down
13 changes: 6 additions & 7 deletions ant/src/plus/profiles/heart_rate/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// except according to those terms.

use crate::channel::duration_to_search_timeout;
use crate::channel::{RxError, RxHandler, TxError, TxHandler};
use crate::channel::{ChanError, RxHandler, TxHandler};
use crate::messages::config::{
ChannelType, TransmissionChannelType, TransmissionGlobalDataPages, TransmissionType,
};
Expand Down Expand Up @@ -173,7 +173,7 @@ impl<T: TxHandler<TxMessage>, R: RxHandler<AntMessage>> Display<T, R> {
Err(Error::UnsupportedDataPage(dp_num))
}

pub fn process(&mut self) {
pub fn process(&mut self) -> Result<(), ChanError> {
while let Ok(msg) = self.rx.try_recv() {
if let Some(f) = self.rx_message_callback {
f(&msg);
Expand All @@ -195,24 +195,23 @@ impl<T: TxHandler<TxMessage>, R: RxHandler<AntMessage>> Display<T, R> {

// TODO handle errors
if let Some(msg) = self.msg_handler.send_message() {
self.tx.try_send(msg).expect("TODO");
return;
self.tx.try_send(msg)?;
}
if let Some(callback) = self.tx_message_callback {
if let Some(mut msg) = callback() {
msg.set_channel(self.msg_handler.get_channel());
self.tx.try_send(msg.into()).expect("TODO");
return;
self.tx.try_send(msg.into())?;
}
}
if self.msg_handler.is_tx_ready() {
if let Some(callback) = self.tx_datapage_callback {
if let Some(mut msg) = callback() {
msg.set_channel(self.msg_handler.get_channel());
self.msg_handler.tx_sent();
self.tx.try_send(msg.into()).expect("TODO");
self.tx.try_send(msg.into())?;
}
}
}
Ok(())
}
}
39 changes: 17 additions & 22 deletions ant/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use crate::channel::{RxError, RxHandler, TxError, TxHandler};
use crate::channel::{ChanError, RxHandler, TxError, TxHandler};
use crate::drivers::{Driver, DriverError};
use crate::messages::config::UnAssignChannel;
use crate::messages::control::{CloseChannel, RequestMessage, RequestableMessageId, ResetSystem};
Expand All @@ -24,6 +24,13 @@ pub enum RouterError {
ChannelOutOfBounds(),
ChannelNotAssociated(),
FailedToGetCapabilities(),
ChannelBufferError(ChanError),
}

impl From<TxError> for RouterError {
fn from(err: TxError) -> RouterError {
RouterError::ChannelBufferError(ChanError::Tx(err))
}
}

// This in theory is infinite, but its what the current hardware limit is.
Expand Down Expand Up @@ -152,17 +159,18 @@ impl<E, D: Driver<E>, T: TxHandler<AntMessage>, R: RxHandler<TxMessage>> Router<
return Err(RouterError::ChannelOutOfBounds());
}
match &self.channels[channel as usize] {
Some(handler) => handler.try_send(msg).expect("TODO"),
Some(handler) => handler.try_send(msg)?,
None => return Err(RouterError::ChannelNotAssociated()),
};
Ok(())
}

fn broadcast_message(&self, msg: AntMessage) {
fn broadcast_message(&self, msg: AntMessage) -> Result<(), RouterError> {
self.channels
.iter()
.flatten()
.for_each(|x| x.try_send(msg.clone()).expect("TODO"));
.try_for_each(|x| x.try_send(msg.clone()))?;
Ok(())
}

fn parse_capabilities(&self, msg: &Capabilities) {
Expand Down Expand Up @@ -192,27 +200,14 @@ impl<E, D: Driver<E>, T: TxHandler<AntMessage>, R: RxHandler<TxMessage>> Router<
RxMessage::ChannelId(data) => self.route_message(data.channel_number, msg),
// These messages can all provide actionable information to the profile but are not
// channel specific
RxMessage::StartUpMessage(_) => {
self.broadcast_message(msg);
Ok(())
}
RxMessage::StartUpMessage(_) => self.broadcast_message(msg),
RxMessage::Capabilities(data) => {
self.broadcast_message(msg.clone());
self.parse_capabilities(data);
Ok(())
}
RxMessage::AdvancedBurstCapabilities(_) => {
self.broadcast_message(msg);
Ok(())
}
RxMessage::AdvancedBurstCurrentConfiguration(_) => {
self.broadcast_message(msg);
Ok(())
}
RxMessage::EncryptionModeParameters(_) => {
self.broadcast_message(msg);
Ok(())
self.broadcast_message(msg.clone())
}
RxMessage::AdvancedBurstCapabilities(_) => self.broadcast_message(msg),
RxMessage::AdvancedBurstCurrentConfiguration(_) => self.broadcast_message(msg),
RxMessage::EncryptionModeParameters(_) => self.broadcast_message(msg),
// These message are not channel specific and operate at the router scope, should be
// consumed directly at router callback
RxMessage::EventFilter(_) => Ok(()),
Expand Down

0 comments on commit 9ca5077

Please sign in to comment.