Skip to content

Commit

Permalink
pdo: add common PDO trait
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertZ2011 committed Feb 10, 2025
1 parent d7ea0bd commit 286160b
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/pdo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,35 @@ impl TryFrom<u32> 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<ApdoKind>;
}

/// 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<ApdoKind> {
match self {
Pdo::Source(pdo) => pdo.apdo_kind(),
Pdo::Sink(pdo) => pdo.apdo_kind(),
}
}
}
22 changes: 22 additions & 0 deletions src/pdo/sink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ApdoKind> {
match self {
Pdo::Augmented(apdo) => Some(match apdo {
Apdo::SprPps(_) => ApdoKind::SprPps,
Apdo::EprAvs(_) => ApdoKind::EprAvs,
Apdo::SprAvs(_) => ApdoKind::SprAvs,
}),
_ => None,
}
}
}

impl TryFrom<u32> for Pdo {
type Error = PdError;

Expand Down
22 changes: 22 additions & 0 deletions src/pdo/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ApdoKind> {
match self {
Pdo::Augmented(apdo) => Some(match apdo {
Apdo::SprPps(_) => ApdoKind::SprPps,
Apdo::EprAvs(_) => ApdoKind::EprAvs,
Apdo::SprAvs(_) => ApdoKind::SprAvs,
}),
_ => None,
}
}
}

impl TryFrom<u32> for Pdo {
type Error = PdError;

Expand Down

0 comments on commit 286160b

Please sign in to comment.