rstm_core/rule/
impl_rule_builder.rs1use super::{Rule, RuleBuilder};
6
7use crate::{Direction, Head, Tail};
8use rstm_state::{RawState, State};
9
10impl<Q, S> RuleBuilder<Q, S>
11where
12 Q: RawState,
13{
14 pub const fn new() -> Self {
16 Self {
17 direction: Direction::Stay,
18 state: None,
19 symbol: None,
20 next_state: None,
21 write_symbol: None,
22 }
23 }
24 pub fn direction(self, direction: Direction) -> Self {
26 Self { direction, ..self }
27 }
28 pub fn left(self) -> Self {
30 self.direction(Direction::Left)
31 }
32 pub fn right(self) -> Self {
34 self.direction(Direction::Right)
35 }
36 pub fn stay(self) -> Self {
38 self.direction(Direction::Stay)
39 }
40 pub fn state(self, state: State<Q>) -> Self {
42 Self {
43 state: Some(state),
44 ..self
45 }
46 }
47 pub fn symbol(self, symbol: S) -> Self {
49 Self {
50 symbol: Some(symbol),
51 ..self
52 }
53 }
54 pub fn next_state(self, State(state): State<Q>) -> Self {
56 Self {
57 next_state: Some(State(state)),
58 ..self
59 }
60 }
61 pub fn write_symbol(self, write_symbol: S) -> Self {
63 Self {
64 write_symbol: Some(write_symbol),
65 ..self
66 }
67 }
68 #[inline]
70 pub fn build(self) -> Rule<Q, S> {
71 Rule {
72 head: Head {
73 state: self.state.expect("state is required"),
74 symbol: self.symbol.expect("symbol is required"),
75 },
76 tail: Tail {
77 direction: self.direction,
78 next_state: self.next_state.expect("next_state is required"),
79 write_symbol: self.write_symbol.expect("write_symbol is required"),
80 },
81 }
82 }
83}
84
85impl<Q, S> From<RuleBuilder<Q, S>> for Rule<Q, S>
86where
87 Q: RawState,
88{
89 fn from(builder: RuleBuilder<Q, S>) -> Self {
90 builder.build()
91 }
92}