rstm_core/rule/
impl_rule_builder.rs

1/*
2    appellation: impl_rule_builder <module>
3    authors: @FL03
4*/
5use 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    /// initialize a new instance of the [`RuleBuilder`]
15    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    /// configure the direction
25    pub fn direction(self, direction: Direction) -> Self {
26        Self { direction, ..self }
27    }
28    /// toggle the direction to the [`Left`](Direction::Left)
29    pub fn left(self) -> Self {
30        self.direction(Direction::Left)
31    }
32    /// toggle the direction to the [`Right`](Direction::Right)
33    pub fn right(self) -> Self {
34        self.direction(Direction::Right)
35    }
36    /// toggle the direction to the [`Stay`](Direction::Stay)
37    pub fn stay(self) -> Self {
38        self.direction(Direction::Stay)
39    }
40    /// configure the current state
41    pub fn state(self, state: State<Q>) -> Self {
42        Self {
43            state: Some(state),
44            ..self
45        }
46    }
47    /// configure the current symbol
48    pub fn symbol(self, symbol: S) -> Self {
49        Self {
50            symbol: Some(symbol),
51            ..self
52        }
53    }
54    /// configure the next state
55    pub fn next_state(self, State(state): State<Q>) -> Self {
56        Self {
57            next_state: Some(State(state)),
58            ..self
59        }
60    }
61    /// configure the symbol to write
62    pub fn write_symbol(self, write_symbol: S) -> Self {
63        Self {
64            write_symbol: Some(write_symbol),
65            ..self
66        }
67    }
68    /// consume the current instance to create a formal [`Rule`]
69    #[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}