This commit is contained in:
2024-12-03 21:08:26 +01:00
parent 0afcc35513
commit b07b8d6347
3 changed files with 123 additions and 29 deletions

View File

@ -180,10 +180,30 @@ impl SequenceTableEntry {
}
}
#[derive(Debug, thiserror::Error)]
#[error("target {0} not in mode store")]
pub struct TargetNotInModeStoreError(pub ComponentId);
pub trait ModeStoreProvider {
fn add_component(&mut self, target_id: ComponentId, mode: ModeAndSubmode);
fn has_component(&self, target_id: ComponentId) -> bool;
fn get_mode(&self, target_id: ComponentId) -> Option<ModeAndSubmode>;
fn set_mode(&mut self, target_id: ComponentId, mode: ModeAndSubmode);
fn set_mode_for_contained_component(&mut self, target_id: ComponentId, mode: ModeAndSubmode);
fn set_mode(
&mut self,
target_id: ComponentId,
mode: ModeAndSubmode,
) -> Result<(), TargetNotInModeStoreError> {
if !self.has_component(target_id) {
return Err(TargetNotInModeStoreError(target_id));
}
self.set_mode_for_contained_component(target_id, mode);
Ok(())
}
}
#[cfg(feature = "alloc")]
@ -268,6 +288,10 @@ pub mod alloc_mod {
self.0.push((target_id, mode));
}
fn has_component(&self, target_id: ComponentId) -> bool {
self.0.iter().any(|(id, _)| *id == target_id)
}
fn get_mode(&self, target_id: ComponentId) -> Option<ModeAndSubmode> {
self.0.iter().find_map(|(id, mode)| {
if *id == target_id {
@ -277,7 +301,11 @@ pub mod alloc_mod {
})
}
fn set_mode(&mut self, target_id: ComponentId, mode_to_set: ModeAndSubmode) {
fn set_mode_for_contained_component(
&mut self,
target_id: ComponentId,
mode_to_set: ModeAndSubmode,
) {
self.0.iter_mut().for_each(|(id, mode)| {
if *id == target_id {
*mode = mode_to_set;
@ -290,10 +318,19 @@ pub mod alloc_mod {
fn add_component(&mut self, target_id: ComponentId, mode: ModeAndSubmode) {
self.0.insert(target_id, mode);
}
fn has_component(&self, target_id: ComponentId) -> bool {
self.0.contains_key(&target_id)
}
fn get_mode(&self, target_id: ComponentId) -> Option<ModeAndSubmode> {
self.0.get(&target_id).copied()
}
fn set_mode(&mut self, target_id: ComponentId, mode_to_set: ModeAndSubmode) {
fn set_mode_for_contained_component(
&mut self,
target_id: ComponentId,
mode_to_set: ModeAndSubmode,
) {
self.0.insert(target_id, mode_to_set);
}
}

View File

@ -6,31 +6,13 @@ use crate::{
ComponentId,
};
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum SequenceExecutionHelperStates {
Idle,
AwaitingCheckSuccess,
Done,
}
#[derive(Debug)]
pub struct SequenceExecutionHelper {
target_mode: Mode,
state: SequenceExecutionHelperStates,
request_id: RequestId,
current_sequence_index: Option<usize>,
}
impl Default for SequenceExecutionHelper {
fn default() -> Self {
Self {
target_mode: 0,
state: SequenceExecutionHelperStates::Idle,
request_id: 0,
current_sequence_index: todo!(),
}
}
}
pub trait CheckSuccessProvider {
fn mode_request_requires_success_check(
&mut self,
@ -46,15 +28,38 @@ pub enum SequenceHandlerResult {
AwaitingSuccessCheck,
}
#[derive(Debug, thiserror::Error)]
#[error("Mode {0} does not exist")]
pub struct ModeDoesNotExistError(Mode);
#[derive(Debug)]
pub struct SequenceExecutionHelper {
target_mode: Mode,
state: SequenceExecutionHelperStates,
request_id: RequestId,
current_sequence_index: Option<usize>,
}
impl Default for SequenceExecutionHelper {
fn default() -> Self {
Self {
target_mode: 0,
state: SequenceExecutionHelperStates::Idle,
request_id: 0,
current_sequence_index: None,
}
}
}
impl SequenceExecutionHelper {
pub fn load(
&mut self,
mode: Mode,
request_id: RequestId,
sequence_tables: &SequenceModeTables,
) -> Result<(), ()> {
) -> Result<(), ModeDoesNotExistError> {
if !sequence_tables.0.contains_key(&mode) {
return Err(());
return Err(ModeDoesNotExistError(mode));
}
self.target_mode = mode;
self.request_id = request_id;
@ -68,6 +73,21 @@ impl SequenceExecutionHelper {
}
}
pub fn state(&self) -> SequenceExecutionHelperStates {
self.state
}
pub fn awaiting_check_success(&self) -> bool {
matches!(
self.state,
SequenceExecutionHelperStates::AwaitingCheckSuccess
)
}
pub fn current_sequence_index(&self) -> Option<usize> {
self.current_sequence_index
}
pub fn run(
&mut self,
table: &SequenceModeTables,