rstm_core/rules/impls/
impl_rule_builder.rs1use crate::rules::{Rule, RuleBuilder};
6
7use crate::{Direction, Head, Tail};
8use rstm_state::{RawState, State};
9
10impl<Q1, Q2, A, B> RuleBuilder<Q1, A, Q2, B>
11where
12 Q1: RawState,
13 Q2: RawState,
14{
15 pub const fn new() -> Self {
17 Self {
18 direction: Direction::Stay,
19 state: None,
20 symbol: None,
21 next_state: None,
22 write_symbol: None,
23 }
24 }
25 pub fn direction(self, direction: Direction) -> Self {
27 Self { direction, ..self }
28 }
29 pub fn left(self) -> Self {
31 self.direction(Direction::Left)
32 }
33 pub fn right(self) -> Self {
35 self.direction(Direction::Right)
36 }
37 pub fn stay(self) -> Self {
39 self.direction(Direction::Stay)
40 }
41 pub fn state<O>(self, state: State<O>) -> RuleBuilder<O, A, Q2, B>
43 where
44 O: RawState,
45 {
46 RuleBuilder {
47 state: Some(state),
48 symbol: self.symbol,
49 direction: self.direction,
50 next_state: self.next_state,
51 write_symbol: self.write_symbol,
52 }
53 }
54 pub fn symbol<C>(self, symbol: C) -> RuleBuilder<Q1, C, Q2, B> {
56 RuleBuilder {
57 symbol: Some(symbol),
58 state: self.state,
59 direction: self.direction,
60 next_state: self.next_state,
61 write_symbol: self.write_symbol,
62 }
63 }
64 pub fn next_state<O>(self, state: State<O>) -> RuleBuilder<Q1, A, O, B>
66 where
67 O: RawState,
68 {
69 RuleBuilder {
70 next_state: Some(state),
71 state: self.state,
72 symbol: self.symbol,
73 direction: self.direction,
74 write_symbol: self.write_symbol,
75 }
76 }
77 pub fn write_symbol<C>(self, write_symbol: C) -> RuleBuilder<Q1, A, Q2, C> {
79 RuleBuilder {
80 write_symbol: Some(write_symbol),
81 state: self.state,
82 symbol: self.symbol,
83 direction: self.direction,
84 next_state: self.next_state,
85 }
86 }
87 #[inline]
89 pub fn build(self) -> Rule<Q1, A, Q2, B> {
90 Rule {
91 head: Head {
92 state: self.state.expect("state is required"),
93 symbol: self.symbol.expect("symbol is required"),
94 },
95 tail: Tail {
96 direction: self.direction,
97 next_state: self.next_state.expect("next_state is required"),
98 write_symbol: self.write_symbol.expect("write_symbol is required"),
99 },
100 }
101 }
102}
103
104impl<Q1, A, Q2, B> From<RuleBuilder<Q1, A, Q2, B>> for Rule<Q1, A, Q2, B>
105where
106 Q1: RawState,
107 Q2: RawState,
108{
109 fn from(builder: RuleBuilder<Q1, A, Q2, B>) -> Self {
110 builder.build()
111 }
112}