move generic components to library

This commit is contained in:
2024-11-28 12:36:43 +01:00
parent c973339ee5
commit 04b96579bd
3 changed files with 398 additions and 160 deletions

View File

@ -2,13 +2,57 @@ use alloc::vec::Vec;
use hashbrown::HashMap;
use crate::{
mode::{Mode, ModeAndSubmode, Submode},
mode::{Mode, ModeAndSubmode, ModeReply, ModeRequest, Submode},
request::MessageSender,
ComponentId,
};
#[cfg(feature = "alloc")]
pub use alloc_mod::*;
/// Common trait for node modes which can have mode parents or mode children.
pub trait ModeNode {
fn id(&self) -> ComponentId;
}
/// Trait which denotes that an object is a parent in a mode tree.
///
/// A mode parent is capable of sending mode requests to child objects and has a unique component
/// ID.
pub trait ModeParent: ModeNode {
type Sender: MessageSender<ModeRequest>;
fn add_mode_child(&mut self, id: ComponentId, request_sender: Self::Sender);
}
/// Trait which denotes that an object is a child in a mode tree.
///
/// A child is capable of sending mode replies to parent objects and has a unique component ID.
pub trait ModeChild: ModeNode {
type Sender: MessageSender<ModeReply>;
fn add_mode_parent(&mut self, id: ComponentId, reply_sender: Self::Sender);
}
/// Utility method which connects a mode tree parent object to a child object by calling
/// [ModeParent::add_mode_child] on the [parent][ModeParent] and calling
/// [ModeChild::add_mode_parent] on the [child][ModeChild].
///
/// # Arguments
///
/// * `parent` - The parent object which implements [ModeParent].
/// * `request_sender` - Sender object to send mode requests to the child.
/// * `child` - The child object which implements [ModeChild].
/// * `reply_sender` - Sender object to send mode replies to the parent.
pub fn connect_mode_nodes<ReqSender, ReplySender>(
parent: &mut impl ModeParent<Sender = ReqSender>,
request_sender: ReqSender,
child: &mut impl ModeChild<Sender = ReplySender>,
reply_sender: ReplySender,
) {
parent.add_mode_child(child.id(), request_sender);
child.add_mode_parent(parent.id(), reply_sender);
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum TableEntryType {
/// Target table containing information of the expected children modes for given mode.
@ -62,6 +106,24 @@ impl TargetTableEntry {
target_id: ComponentId,
mode_submode: ModeAndSubmode,
monitor_state: bool,
allowed_submode_mask: Option<Submode>,
) -> Self {
Self {
common: ModeTableEntryCommon {
name,
target_id,
mode_submode,
allowed_submode_mask,
},
monitor_state,
}
}
pub fn new_with_precise_submode(
name: &'static str,
target_id: ComponentId,
mode_submode: ModeAndSubmode,
monitor_state: bool,
) -> Self {
Self {
common: ModeTableEntryCommon {
@ -129,14 +191,14 @@ pub mod alloc_mod {
use super::*;
#[derive(Debug)]
pub struct TargetTableMapValue {
pub struct TargetTablesMapValue {
/// Name for a given mode table entry.
pub name: &'static str,
/// These are the rows of the a target table.
pub entries: Vec<TargetTableEntry>,
}
impl TargetTableMapValue {
impl TargetTablesMapValue {
pub fn new(name: &'static str) -> Self {
Self {
name,
@ -171,14 +233,14 @@ pub mod alloc_mod {
}
#[derive(Debug)]
pub struct SequenceTableMapValue {
pub struct SequenceTablesMapValue {
/// Name for a given mode sequence.
pub name: &'static str,
/// Each sequence can consists of multiple sequences that are executed consecutively.
pub entries: Vec<SequenceTableMapTable>,
}
impl SequenceTableMapValue {
impl SequenceTablesMapValue {
pub fn new(name: &'static str) -> Self {
Self {
name,
@ -192,9 +254,9 @@ pub mod alloc_mod {
}
#[derive(Debug, Default)]
pub struct TargetModeTables(pub HashMap<Mode, TargetTableMapValue>);
pub struct TargetModeTables(pub HashMap<Mode, TargetTablesMapValue>);
#[derive(Debug, Default)]
pub struct SequenceModeTables(pub HashMap<Mode, SequenceTableMapValue>);
pub struct SequenceModeTables(pub HashMap<Mode, SequenceTablesMapValue>);
#[derive(Debug, Default)]
pub struct ModeStoreVec(pub alloc::vec::Vec<(ComponentId, ModeAndSubmode)>);

View File

@ -1,6 +1,6 @@
use crate::{
mode::{Mode, ModeAndSubmode, ModeRequest, ModeRequestSender},
mode_tree::{SequenceModeTables, SequenceTableMapTable, SequenceTableMapValue},
mode_tree::{SequenceModeTables, SequenceTableMapTable, SequenceTablesMapValue},
queue::GenericTargetedMessagingError,
request::RequestId,
ComponentId,
@ -88,7 +88,7 @@ impl SequenceExecutionHelper {
pub fn execute_sequence_and_map_to_result(
&mut self,
seq_table_value: &SequenceTableMapValue,
seq_table_value: &SequenceTablesMapValue,
sequence_idx: usize,
sender: &impl ModeRequestSender,
) -> Result<SequenceHandlerResult, GenericTargetedMessagingError> {