From 286160be8d8351f5cdcc85bb29f93a7c70d6e30b Mon Sep 17 00:00:00 2001 From: Robert Zieba Date: Fri, 7 Feb 2025 14:33:51 -0700 Subject: [PATCH] pdo: add common PDO trait --- src/pdo/mod.rs | 32 ++++++++++++++++++++++++++++++++ src/pdo/sink.rs | 22 ++++++++++++++++++++++ src/pdo/source.rs | 22 ++++++++++++++++++++++ 3 files changed, 76 insertions(+) diff --git a/src/pdo/mod.rs b/src/pdo/mod.rs index 0c6fab2..7692a77 100644 --- a/src/pdo/mod.rs +++ b/src/pdo/mod.rs @@ -110,3 +110,35 @@ impl TryFrom for ApdoKind { } } } + +/// Common PDO trait +pub trait Common { + /// Get the PDO kind + fn kind(&self) -> PdoKind; + /// Get the APDO kind + fn apdo_kind(&self) -> Option; +} + +/// Top-level PDO type +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] +pub enum Pdo { + Source(source::Pdo), + Sink(sink::Pdo), +} + +impl Common for Pdo { + fn kind(&self) -> PdoKind { + match self { + Pdo::Source(pdo) => pdo.kind(), + Pdo::Sink(pdo) => pdo.kind(), + } + } + + fn apdo_kind(&self) -> Option { + match self { + Pdo::Source(pdo) => pdo.apdo_kind(), + Pdo::Sink(pdo) => pdo.apdo_kind(), + } + } +} diff --git a/src/pdo/sink.rs b/src/pdo/sink.rs index ce93478..371f0c6 100644 --- a/src/pdo/sink.rs +++ b/src/pdo/sink.rs @@ -18,6 +18,28 @@ pub enum Pdo { Augmented(Apdo), } +impl Common for Pdo { + fn kind(&self) -> PdoKind { + match self { + Pdo::Fixed(_) => PdoKind::Fixed, + Pdo::Battery(_) => PdoKind::Battery, + Pdo::Variable(_) => PdoKind::Variable, + Pdo::Augmented(_) => PdoKind::Augmented, + } + } + + fn apdo_kind(&self) -> Option { + match self { + Pdo::Augmented(apdo) => Some(match apdo { + Apdo::SprPps(_) => ApdoKind::SprPps, + Apdo::EprAvs(_) => ApdoKind::EprAvs, + Apdo::SprAvs(_) => ApdoKind::SprAvs, + }), + _ => None, + } + } +} + impl TryFrom for Pdo { type Error = PdError; diff --git a/src/pdo/source.rs b/src/pdo/source.rs index 7d16703..9e5b53c 100644 --- a/src/pdo/source.rs +++ b/src/pdo/source.rs @@ -18,6 +18,28 @@ pub enum Pdo { Augmented(Apdo), } +impl Common for Pdo { + fn kind(&self) -> PdoKind { + match self { + Pdo::Fixed(_) => PdoKind::Fixed, + Pdo::Battery(_) => PdoKind::Battery, + Pdo::Variable(_) => PdoKind::Variable, + Pdo::Augmented(_) => PdoKind::Augmented, + } + } + + fn apdo_kind(&self) -> Option { + match self { + Pdo::Augmented(apdo) => Some(match apdo { + Apdo::SprPps(_) => ApdoKind::SprPps, + Apdo::EprAvs(_) => ApdoKind::EprAvs, + Apdo::SprAvs(_) => ApdoKind::SprAvs, + }), + _ => None, + } + } +} + impl TryFrom for Pdo { type Error = PdError;