Skip to main content

rstm_core/rules/impls/
impl_rule_builder.rs

1/*
2    appellation: impl_rule_builder <module>
3    authors: @FL03
4*/
5use 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    /// initialize a new instance of the [`RuleBuilder`]
16    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    /// configure the direction
26    pub fn direction(self, direction: Direction) -> Self {
27        Self { direction, ..self }
28    }
29    /// toggle the direction to the [`Left`](Direction::Left)
30    pub fn left(self) -> Self {
31        self.direction(Direction::Left)
32    }
33    /// toggle the direction to the [`Right`](Direction::Right)
34    pub fn right(self) -> Self {
35        self.direction(Direction::Right)
36    }
37    /// toggle the direction to the [`Stay`](Direction::Stay)
38    pub fn stay(self) -> Self {
39        self.direction(Direction::Stay)
40    }
41    /// configure the current state
42    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    /// configure the current symbol
55    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    /// configure the next state
65    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    /// configure the write symbol for the rule
78    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    /// consume the current instance to create a formal [`Rule`]
88    #[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}