rstm_core/rules/impls/
impl_rule.rs1use crate::rules::{Rule, RuleBuilder};
6
7use crate::{Direction, Head, Tail};
8use rstm_state::{RawState, State};
9
10impl<Q, A, R, B> Rule<Q, A, R, B>
11where
12 Q: RawState,
13 R: RawState,
14{
15 pub const fn new(head: Head<Q, A>, tail: Tail<R, B>) -> Self {
17 Self { head, tail }
18 }
19 pub const fn init() -> RuleBuilder<Q, A, R, B> {
21 RuleBuilder::new()
22 }
23 pub const fn from_parts(
25 state: Q,
26 symbol: A,
27 direction: Direction,
28 next_state: R,
29 write_symbol: B,
30 ) -> Self {
31 let head = Head::new(state, symbol);
32 let tail = Tail::new(direction, next_state, write_symbol);
33 Self { head, tail }
34 }
35 pub fn with_head(self, head: Head<Q, A>) -> Self {
37 Self { head, ..self }
38 }
39 pub fn with_tail(self, tail: Tail<R, B>) -> Self {
41 Self { tail, ..self }
42 }
43 pub const fn head(&self) -> &Head<Q, A> {
45 &self.head
46 }
47 pub const fn head_mut(&mut self) -> &mut Head<Q, A> {
49 &mut self.head
50 }
51 pub const fn tail(&self) -> &Tail<R, B> {
53 &self.tail
54 }
55 pub const fn tail_mut(&mut self) -> &mut Tail<R, B> {
57 &mut self.tail
58 }
59 pub const fn head_view(&self) -> Head<&'_ Q, &'_ A> {
61 self.head().view()
62 }
63 pub const fn tail_view(&self) -> Tail<&'_ R, &'_ B> {
65 self.tail().view()
66 }
67 pub const fn direction(&self) -> Direction {
69 self.tail().direction()
70 }
71 pub const fn state(&self) -> &State<Q> {
73 self.head().state()
74 }
75 pub const fn state_mut(&mut self) -> &mut State<Q> {
77 self.head_mut().state_mut()
78 }
79 pub const fn symbol(&self) -> &A {
81 self.head().symbol()
82 }
83 pub const fn symbol_mut(&mut self) -> &mut A {
85 self.head_mut().symbol_mut()
86 }
87 pub const fn next_state(&self) -> &State<R> {
89 self.tail().state()
90 }
91 pub const fn next_state_mut(&mut self) -> &mut State<R> {
93 self.tail_mut().state_mut()
94 }
95 pub const fn next_symbol(&self) -> &B {
97 self.tail().symbol()
98 }
99 pub const fn next_symbol_mut(&mut self) -> &mut B {
101 self.tail_mut().symbol_mut()
102 }
103 pub const fn write_symbol(&self) -> &B {
105 self.tail().symbol()
106 }
107 pub const fn write_symbol_mut(&mut self) -> &mut B {
109 self.tail_mut().symbol_mut()
110 }
111 pub fn set_direction(&mut self, direction: Direction) {
113 self.tail_mut().set_direction(direction);
114 }
115 pub fn set_symbol(&mut self, symbol: A) {
117 self.head_mut().set_symbol(symbol);
118 }
119 pub fn set_state(&mut self, state: Q) {
121 self.head_mut().set_state(state);
122 }
123 pub fn set_next_state(&mut self, state: R) {
125 self.tail_mut().set_state(state);
126 }
127 pub fn set_next_symbol(&mut self, symbol: B) {
129 self.tail_mut().set_symbol(symbol);
130 }
131 pub fn set_head(&mut self, state: Q, symbol: A) {
133 self.head_mut().set_state(state);
134 self.head_mut().set_symbol(symbol);
135 }
136 pub fn set_tail(&mut self, state: R, symbol: B) {
138 self.tail_mut().set_state(state);
139 self.tail_mut().set_symbol(symbol);
140 }
141 pub fn into_tuple(self) -> (Head<Q, A>, Tail<R, B>) {
143 (self.head, self.tail)
144 }
145 pub const fn next_head(&self) -> Head<&'_ R, &'_ B> {
147 self.tail().get_head()
148 }
149 pub fn into_next_head(self) -> Head<R, B> {
151 self.tail.into_head()
152 }
153}