use super::{Rule, RuleBuilder};
use rstm_core::{Direction, Head, Tail};
use rstm_state::{RawState, State};
impl<Q, S> RuleBuilder<Q, S>
where
Q: RawState,
{
pub const fn new() -> Self {
Self {
direction: Direction::Stay,
state: None,
symbol: None,
next_state: None,
write_symbol: None,
}
}
pub fn direction(self, direction: Direction) -> Self {
Self { direction, ..self }
}
pub fn left(self) -> Self {
self.direction(Direction::Left)
}
pub fn right(self) -> Self {
self.direction(Direction::Right)
}
pub fn stay(self) -> Self {
self.direction(Direction::Stay)
}
pub fn state(self, state: State<Q>) -> Self {
Self {
state: Some(state),
..self
}
}
pub fn symbol(self, symbol: S) -> Self {
Self {
symbol: Some(symbol),
..self
}
}
pub fn next_state(self, State(state): State<Q>) -> Self {
Self {
next_state: Some(State(state)),
..self
}
}
pub fn write_symbol(self, write_symbol: S) -> Self {
Self {
write_symbol: Some(write_symbol),
..self
}
}
#[inline]
pub fn build(self) -> Rule<Q, S> {
Rule {
head: Head {
state: self.state.expect("state is required"),
symbol: self.symbol.expect("symbol is required"),
},
tail: Tail {
direction: self.direction,
next_state: self.next_state.expect("next_state is required"),
write_symbol: self.write_symbol.expect("write_symbol is required"),
},
}
}
}
impl<Q, S> From<RuleBuilder<Q, S>> for Rule<Q, S>
where
Q: RawState,
{
fn from(builder: RuleBuilder<Q, S>) -> Self {
builder.build()
}
}