Skip to content

Commit cf3dda2

Browse files
authored
datatypes: fix fsm.set_state() and cleanup fsm module (#16539)
1 parent 8543d5e commit cf3dda2

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

vlib/datatypes/fsm/fsm.v

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ pub fn new() StateMachine {
2828
return StateMachine{}
2929
}
3030

31-
pub fn (mut s StateMachine) set_state(name string) ? {
31+
pub fn (mut s StateMachine) set_state(name string) ! {
3232
if name in s.states {
3333
s.current_state = name
34+
} else {
35+
return error('unknown state: ${name}')
3436
}
35-
return error('unknown state: ${name}')
3637
}
3738

3839
pub fn (mut s StateMachine) get_state() string {
@@ -62,7 +63,7 @@ pub fn (mut s StateMachine) add_transition(from string, to string, condition_han
6263
s.transitions[from] = [t]
6364
}
6465

65-
pub fn (mut s StateMachine) run(receiver voidptr) ? {
66+
pub fn (mut s StateMachine) run(receiver voidptr) ! {
6667
from_state := s.current_state
6768
mut to_state := s.current_state
6869
if transitions := s.transitions[s.current_state] {

vlib/datatypes/fsm/fsm_test.v

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,29 @@ fn default_setup() (MyReceiver, fsm.StateMachine) {
1414
return receiver, s
1515
}
1616

17-
fn test_statemachine_number_of_callbacks_correct_when_single_transition() ? {
17+
fn test_statemachine_number_of_callbacks_correct_when_single_transition() {
1818
mut receiver, mut s := default_setup()
1919

20-
s.run(receiver)?
20+
s.run(receiver)!
2121

2222
assert receiver.data.len == 3
2323
}
2424

25-
fn test_statemachine_sequence_works_when_typical() ? {
25+
fn test_statemachine_sequence_works_when_typical() {
2626
mut receiver, mut s := default_setup()
2727

28-
s.run(receiver)?
28+
s.run(receiver)!
2929

3030
assert receiver.data[0] == 'on_state_exit: A -> B'
3131
assert receiver.data[1] == 'on_state_entry: A -> B'
3232
assert receiver.data[2] == 'on_state_run: A -> B'
3333
}
3434

35-
fn test_statemachine_works_when_final_state() ? {
35+
fn test_statemachine_works_when_final_state() {
3636
mut receiver, mut s := default_setup()
3737

3838
// current state `A`, with a possible transition to `B`:
39-
s.run(receiver)? // run should not error here
39+
s.run(receiver)! // run should not error here
4040

4141
// Note: run will now return error, because for state `B`,
4242
// there are no more transitions:
@@ -49,7 +49,7 @@ fn test_statemachine_works_when_final_state() ? {
4949
assert receiver.data[4] == 'on_state_run: B -> B'
5050
}
5151

52-
fn test_simple_loop() ? {
52+
fn test_simple_loop() {
5353
mut receiver, mut s := default_setup()
5454

5555
// Add a transition back to `A` too:

vlib/datatypes/fsm/tools/fsm_graph.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22
import flag
33

4-
pub fn read_file(file string) ?[]string {
4+
pub fn read_file(file string) ![]string {
55
if os.is_file(file) {
66
text := os.read_lines(file) or {
77
return error(@MOD + '.' + @STRUCT + '.' + @FN +
@@ -33,7 +33,7 @@ pub fn get_transitions(line string) ?string {
3333
pub fn main() {
3434
mut fp := flag.new_flag_parser(os.args)
3535
file := fp.string('file', `f`, '', 'input V file with transitions to generate graph from.')
36-
lines := read_file(file)?
36+
lines := read_file(file)!
3737
println('digraph fsm {')
3838
for line in lines {
3939
if line.contains('add_transition') {

0 commit comments

Comments
 (0)