Struct Context

Source
pub struct Context {
    pub data: Value,
}
Expand description

Represents the extended state (context) for a state machine

Fields§

§data: Value

The context data

Implementations§

Source§

impl Context

Source

pub fn new() -> Self

Create a new empty context

Examples found in repository?
examples/hierarchical.rs (line 121)
36fn create_player() -> rustate::Result<Machine> {
37    // Create states
38    let power_off = State::new("powerOff");
39
40    let mut player = State::new_compound("player", "stopped");
41    player.parent = Some("root".to_string());
42
43    let mut stopped = State::new("stopped");
44    stopped.parent = Some("player".to_string());
45
46    let mut playing = State::new_compound("playing", "normal");
47    playing.parent = Some("player".to_string());
48
49    let mut normal = State::new("normal");
50    normal.parent = Some("playing".to_string());
51
52    let mut double_speed = State::new("doubleSpeed");
53    double_speed.parent = Some("playing".to_string());
54
55    let mut paused = State::new("paused");
56    paused.parent = Some("player".to_string());
57
58    // Create transitions
59    let power_toggle = Transition::new("powerOff", "POWER", "player");
60    let power_off_transition = Transition::new("player", "POWER", "powerOff");
61
62    let play = Transition::new("stopped", "PLAY", "playing");
63    let stop = Transition::new("playing", "STOP", "stopped");
64    let pause = Transition::new("playing", "PAUSE", "paused");
65    let resume = Transition::new("paused", "PLAY", "playing");
66
67    let speed_up = Transition::new("normal", "SPEED_UP", "doubleSpeed");
68    let speed_normal = Transition::new("doubleSpeed", "SPEED_NORMAL", "normal");
69
70    let next_track = Transition::internal_transition("playing", "NEXT");
71    let prev_track = Transition::internal_transition("playing", "PREV");
72
73    // Create guards and actions
74    let log_power_on = Action::new("logPowerOn", ActionType::Entry, |_ctx, _evt| {
75        println!("Power ON - Player ready")
76    });
77
78    let log_power_off = Action::new("logPowerOff", ActionType::Entry, |_ctx, _evt| {
79        println!("Power OFF")
80    });
81
82    let log_playing = Action::new("logPlaying", ActionType::Entry, |_ctx, _evt| {
83        println!("Playing track")
84    });
85
86    let log_stopped = Action::new("logStopped", ActionType::Entry, |_ctx, _evt| {
87        println!("Stopped")
88    });
89
90    let log_paused = Action::new("logPaused", ActionType::Entry, |_ctx, _evt| {
91        println!("Paused")
92    });
93
94    let log_double_speed = Action::new("logDoubleSpeed", ActionType::Entry, |_ctx, _evt| {
95        println!("Playing at double speed")
96    });
97
98    let log_normal_speed = Action::new("logNormalSpeed", ActionType::Entry, |_ctx, _evt| {
99        println!("Playing at normal speed")
100    });
101
102    let next_track_action = Action::new("nextTrack", ActionType::Transition, |ctx, _evt| {
103        let current_track = ctx.get::<usize>("track").unwrap_or(0);
104        let next_track = current_track + 1;
105        println!("Changing to track {}", next_track);
106        let _ = ctx.set("track", next_track);
107    });
108
109    let prev_track_action = Action::new("prevTrack", ActionType::Transition, |ctx, _evt| {
110        let current_track = ctx.get::<usize>("track").unwrap_or(0);
111        let prev_track = if current_track > 0 {
112            current_track - 1
113        } else {
114            0
115        };
116        println!("Changing to track {}", prev_track);
117        let _ = ctx.set("track", prev_track);
118    });
119
120    // Create context with initial track
121    let mut context = Context::new();
122    let _ = context.set("track", 0);
123
124    // Create and configure the state machine
125    let mut next_track = next_track;
126    next_track.with_action(next_track_action);
127
128    let mut prev_track = prev_track;
129    prev_track.with_action(prev_track_action);
130
131    let machine = MachineBuilder::new("musicPlayer")
132        .initial("powerOff")
133        .state(power_off)
134        .state(player)
135        .state(stopped)
136        .state(playing)
137        .state(normal)
138        .state(double_speed)
139        .state(paused)
140        .transition(power_toggle)
141        .transition(power_off_transition)
142        .transition(play)
143        .transition(stop)
144        .transition(pause)
145        .transition(resume)
146        .transition(speed_up)
147        .transition(speed_normal)
148        .transition(next_track)
149        .transition(prev_track)
150        .on_entry("player", log_power_on)
151        .on_entry("powerOff", log_power_off)
152        .on_entry("playing", log_playing)
153        .on_entry("stopped", log_stopped)
154        .on_entry("paused", log_paused)
155        .on_entry("doubleSpeed", log_double_speed)
156        .on_entry("normal", log_normal_speed)
157        .context(context)
158        .build()?;
159
160    Ok(machine)
161}
Source

pub fn with_data(data: impl Into<Value>) -> Self

Create a new context with data

Source

pub fn set<T: Serialize>(&mut self, key: &str, value: T) -> Result<(), Error>

Set a value in the context

Examples found in repository?
examples/hierarchical.rs (line 106)
36fn create_player() -> rustate::Result<Machine> {
37    // Create states
38    let power_off = State::new("powerOff");
39
40    let mut player = State::new_compound("player", "stopped");
41    player.parent = Some("root".to_string());
42
43    let mut stopped = State::new("stopped");
44    stopped.parent = Some("player".to_string());
45
46    let mut playing = State::new_compound("playing", "normal");
47    playing.parent = Some("player".to_string());
48
49    let mut normal = State::new("normal");
50    normal.parent = Some("playing".to_string());
51
52    let mut double_speed = State::new("doubleSpeed");
53    double_speed.parent = Some("playing".to_string());
54
55    let mut paused = State::new("paused");
56    paused.parent = Some("player".to_string());
57
58    // Create transitions
59    let power_toggle = Transition::new("powerOff", "POWER", "player");
60    let power_off_transition = Transition::new("player", "POWER", "powerOff");
61
62    let play = Transition::new("stopped", "PLAY", "playing");
63    let stop = Transition::new("playing", "STOP", "stopped");
64    let pause = Transition::new("playing", "PAUSE", "paused");
65    let resume = Transition::new("paused", "PLAY", "playing");
66
67    let speed_up = Transition::new("normal", "SPEED_UP", "doubleSpeed");
68    let speed_normal = Transition::new("doubleSpeed", "SPEED_NORMAL", "normal");
69
70    let next_track = Transition::internal_transition("playing", "NEXT");
71    let prev_track = Transition::internal_transition("playing", "PREV");
72
73    // Create guards and actions
74    let log_power_on = Action::new("logPowerOn", ActionType::Entry, |_ctx, _evt| {
75        println!("Power ON - Player ready")
76    });
77
78    let log_power_off = Action::new("logPowerOff", ActionType::Entry, |_ctx, _evt| {
79        println!("Power OFF")
80    });
81
82    let log_playing = Action::new("logPlaying", ActionType::Entry, |_ctx, _evt| {
83        println!("Playing track")
84    });
85
86    let log_stopped = Action::new("logStopped", ActionType::Entry, |_ctx, _evt| {
87        println!("Stopped")
88    });
89
90    let log_paused = Action::new("logPaused", ActionType::Entry, |_ctx, _evt| {
91        println!("Paused")
92    });
93
94    let log_double_speed = Action::new("logDoubleSpeed", ActionType::Entry, |_ctx, _evt| {
95        println!("Playing at double speed")
96    });
97
98    let log_normal_speed = Action::new("logNormalSpeed", ActionType::Entry, |_ctx, _evt| {
99        println!("Playing at normal speed")
100    });
101
102    let next_track_action = Action::new("nextTrack", ActionType::Transition, |ctx, _evt| {
103        let current_track = ctx.get::<usize>("track").unwrap_or(0);
104        let next_track = current_track + 1;
105        println!("Changing to track {}", next_track);
106        let _ = ctx.set("track", next_track);
107    });
108
109    let prev_track_action = Action::new("prevTrack", ActionType::Transition, |ctx, _evt| {
110        let current_track = ctx.get::<usize>("track").unwrap_or(0);
111        let prev_track = if current_track > 0 {
112            current_track - 1
113        } else {
114            0
115        };
116        println!("Changing to track {}", prev_track);
117        let _ = ctx.set("track", prev_track);
118    });
119
120    // Create context with initial track
121    let mut context = Context::new();
122    let _ = context.set("track", 0);
123
124    // Create and configure the state machine
125    let mut next_track = next_track;
126    next_track.with_action(next_track_action);
127
128    let mut prev_track = prev_track;
129    prev_track.with_action(prev_track_action);
130
131    let machine = MachineBuilder::new("musicPlayer")
132        .initial("powerOff")
133        .state(power_off)
134        .state(player)
135        .state(stopped)
136        .state(playing)
137        .state(normal)
138        .state(double_speed)
139        .state(paused)
140        .transition(power_toggle)
141        .transition(power_off_transition)
142        .transition(play)
143        .transition(stop)
144        .transition(pause)
145        .transition(resume)
146        .transition(speed_up)
147        .transition(speed_normal)
148        .transition(next_track)
149        .transition(prev_track)
150        .on_entry("player", log_power_on)
151        .on_entry("powerOff", log_power_off)
152        .on_entry("playing", log_playing)
153        .on_entry("stopped", log_stopped)
154        .on_entry("paused", log_paused)
155        .on_entry("doubleSpeed", log_double_speed)
156        .on_entry("normal", log_normal_speed)
157        .context(context)
158        .build()?;
159
160    Ok(machine)
161}
Source

pub fn get<T: for<'de> Deserialize<'de>>(&self, key: &str) -> Option<T>

Get a value from the context

Examples found in repository?
examples/hierarchical.rs (line 103)
36fn create_player() -> rustate::Result<Machine> {
37    // Create states
38    let power_off = State::new("powerOff");
39
40    let mut player = State::new_compound("player", "stopped");
41    player.parent = Some("root".to_string());
42
43    let mut stopped = State::new("stopped");
44    stopped.parent = Some("player".to_string());
45
46    let mut playing = State::new_compound("playing", "normal");
47    playing.parent = Some("player".to_string());
48
49    let mut normal = State::new("normal");
50    normal.parent = Some("playing".to_string());
51
52    let mut double_speed = State::new("doubleSpeed");
53    double_speed.parent = Some("playing".to_string());
54
55    let mut paused = State::new("paused");
56    paused.parent = Some("player".to_string());
57
58    // Create transitions
59    let power_toggle = Transition::new("powerOff", "POWER", "player");
60    let power_off_transition = Transition::new("player", "POWER", "powerOff");
61
62    let play = Transition::new("stopped", "PLAY", "playing");
63    let stop = Transition::new("playing", "STOP", "stopped");
64    let pause = Transition::new("playing", "PAUSE", "paused");
65    let resume = Transition::new("paused", "PLAY", "playing");
66
67    let speed_up = Transition::new("normal", "SPEED_UP", "doubleSpeed");
68    let speed_normal = Transition::new("doubleSpeed", "SPEED_NORMAL", "normal");
69
70    let next_track = Transition::internal_transition("playing", "NEXT");
71    let prev_track = Transition::internal_transition("playing", "PREV");
72
73    // Create guards and actions
74    let log_power_on = Action::new("logPowerOn", ActionType::Entry, |_ctx, _evt| {
75        println!("Power ON - Player ready")
76    });
77
78    let log_power_off = Action::new("logPowerOff", ActionType::Entry, |_ctx, _evt| {
79        println!("Power OFF")
80    });
81
82    let log_playing = Action::new("logPlaying", ActionType::Entry, |_ctx, _evt| {
83        println!("Playing track")
84    });
85
86    let log_stopped = Action::new("logStopped", ActionType::Entry, |_ctx, _evt| {
87        println!("Stopped")
88    });
89
90    let log_paused = Action::new("logPaused", ActionType::Entry, |_ctx, _evt| {
91        println!("Paused")
92    });
93
94    let log_double_speed = Action::new("logDoubleSpeed", ActionType::Entry, |_ctx, _evt| {
95        println!("Playing at double speed")
96    });
97
98    let log_normal_speed = Action::new("logNormalSpeed", ActionType::Entry, |_ctx, _evt| {
99        println!("Playing at normal speed")
100    });
101
102    let next_track_action = Action::new("nextTrack", ActionType::Transition, |ctx, _evt| {
103        let current_track = ctx.get::<usize>("track").unwrap_or(0);
104        let next_track = current_track + 1;
105        println!("Changing to track {}", next_track);
106        let _ = ctx.set("track", next_track);
107    });
108
109    let prev_track_action = Action::new("prevTrack", ActionType::Transition, |ctx, _evt| {
110        let current_track = ctx.get::<usize>("track").unwrap_or(0);
111        let prev_track = if current_track > 0 {
112            current_track - 1
113        } else {
114            0
115        };
116        println!("Changing to track {}", prev_track);
117        let _ = ctx.set("track", prev_track);
118    });
119
120    // Create context with initial track
121    let mut context = Context::new();
122    let _ = context.set("track", 0);
123
124    // Create and configure the state machine
125    let mut next_track = next_track;
126    next_track.with_action(next_track_action);
127
128    let mut prev_track = prev_track;
129    prev_track.with_action(prev_track_action);
130
131    let machine = MachineBuilder::new("musicPlayer")
132        .initial("powerOff")
133        .state(power_off)
134        .state(player)
135        .state(stopped)
136        .state(playing)
137        .state(normal)
138        .state(double_speed)
139        .state(paused)
140        .transition(power_toggle)
141        .transition(power_off_transition)
142        .transition(play)
143        .transition(stop)
144        .transition(pause)
145        .transition(resume)
146        .transition(speed_up)
147        .transition(speed_normal)
148        .transition(next_track)
149        .transition(prev_track)
150        .on_entry("player", log_power_on)
151        .on_entry("powerOff", log_power_off)
152        .on_entry("playing", log_playing)
153        .on_entry("stopped", log_stopped)
154        .on_entry("paused", log_paused)
155        .on_entry("doubleSpeed", log_double_speed)
156        .on_entry("normal", log_normal_speed)
157        .context(context)
158        .build()?;
159
160    Ok(machine)
161}
Source

pub fn contains_key(&self, key: &str) -> bool

Check if a key exists in the context

Source

pub fn remove(&mut self, key: &str) -> Option<Value>

Remove a key from the context

Trait Implementations§

Source§

impl Clone for Context

Source§

fn clone(&self) -> Context

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Context

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Context

Source§

fn default() -> Context

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Context

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Context

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Serialize for Context

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,