ModeSwitcher

Struct ModeSwitcher 

Source
pub struct ModeSwitcher { /* private fields */ }
Expand description

Handles mode transitions with context preservation

The ModeSwitcher is responsible for:

  • Validating mode transitions
  • Preserving context across switches
  • Restoring context after switches
  • Managing mode-specific data

Implementations§

Source§

impl ModeSwitcher

Source

pub fn new(context: ModeContext) -> Self

Create a new mode switcher with the given context

Source

pub fn register_mode(&mut self, mode: Arc<dyn Mode>)

Register a mode

Source

pub fn get_mode(&self, id: &str) -> Result<Arc<dyn Mode>>

Get a registered mode by ID

Source

pub async fn current_mode(&self) -> Result<Option<Arc<dyn Mode>>>

Get the current active mode

Source

pub async fn current_mode_id(&self) -> Option<String>

Get the current mode ID

Source

pub async fn switch_mode(&self, mode_id: &str) -> Result<Arc<dyn Mode>>

Switch to a different mode with context preservation

This method:

  1. Validates the mode exists
  2. Saves the current context if switching from a mode
  3. Restores the saved context for the new mode if available
  4. Creates a fresh context if the mode hasn’t been visited before
  5. Updates the current mode
Source

pub async fn context(&self) -> ModeContext

Get the current context

Source

pub async fn update_context<F>(&self, f: F) -> Result<()>
where F: FnOnce(&mut ModeContext),

Update the context with a closure

Source

pub async fn save_context_for_mode(&self, mode_id: &str) -> Result<()>

Save the current context for a specific mode

Source

pub async fn restore_context_for_mode(&self, mode_id: &str) -> Result<()>

Restore context for a specific mode

Source

pub fn has_mode(&self, id: &str) -> bool

Check if a mode is registered

Source

pub fn mode_count(&self) -> usize

Get the number of registered modes

Source

pub fn list_mode_ids(&self) -> Vec<String>

Get all registered mode IDs

Source

pub async fn has_saved_context(&self, mode_id: &str) -> bool

Check if context is saved for a mode

Source

pub async fn saved_context_count(&self) -> usize

Get the number of saved contexts

Source

pub async fn clear_saved_contexts(&self)

Clear all saved contexts

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.