rstm_core/rule/
impl_rule.rs1use super::{Rule, RuleBuilder};
6
7use crate::{Direction, Head, Tail};
8use rstm_state::{RawState, State};
9
10impl<Q, A> Rule<Q, A>
11where
12 Q: RawState,
13{
14 pub const fn new(head: Head<Q, A>, tail: Tail<Q, A>) -> Self {
16 Self { head, tail }
17 }
18 pub const fn create() -> RuleBuilder<Q, A> {
20 RuleBuilder::new()
21 }
22 pub const fn from_parts(
24 state: Q,
25 symbol: A,
26 direction: Direction,
27 next_state: Q,
28 write_symbol: A,
29 ) -> Self {
30 let head = Head::new(state, symbol);
31 let tail = Tail::new(direction, next_state, write_symbol);
32 Self::new(head, tail)
33 }
34 pub const fn head(&self) -> &Head<Q, A> {
36 &self.head
37 }
38 pub const fn head_mut(&mut self) -> &mut Head<Q, A> {
40 &mut self.head
41 }
42 pub const fn tail(&self) -> &Tail<Q, A> {
44 &self.tail
45 }
46 pub const fn tail_mut(&mut self) -> &mut Tail<Q, A> {
48 &mut self.tail
49 }
50 pub const fn head_view(&self) -> Head<&'_ Q, &'_ A> {
52 self.head().view()
53 }
54 pub const fn tail_view(&self) -> Tail<&'_ Q, &'_ A> {
56 self.tail().view()
57 }
58 pub const fn direction(&self) -> Direction {
60 self.tail().direction()
61 }
62 pub const fn state(&self) -> &State<Q> {
64 self.head().state()
65 }
66 pub const fn state_mut(&mut self) -> &mut State<Q> {
68 self.head_mut().state_mut()
69 }
70 pub const fn symbol(&self) -> &A {
72 self.head().symbol()
73 }
74 pub const fn symbol_mut(&mut self) -> &mut A {
76 self.head_mut().symbol_mut()
77 }
78 pub const fn next_state(&self) -> &State<Q> {
80 self.tail().state()
81 }
82 pub const fn next_state_mut(&mut self) -> &mut State<Q> {
84 self.tail_mut().state_mut()
85 }
86 pub const fn next_symbol(&self) -> &A {
88 self.tail().symbol()
89 }
90 pub const fn next_symbol_mut(&mut self) -> &mut A {
92 self.tail_mut().symbol_mut()
93 }
94 pub fn set_direction(&mut self, direction: Direction) -> &mut Self {
96 self.tail_mut().set_direction(direction);
97 self
98 }
99 pub fn set_symbol(&mut self, symbol: A) -> &mut Self {
101 self.head_mut().set_symbol(symbol);
102 self
103 }
104 pub fn set_state(&mut self, state: Q) -> &mut Self {
106 self.head_mut().set_state(state);
107 self
108 }
109 pub fn set_next_state(&mut self, state: Q) -> &mut Self {
111 self.tail_mut().set_state(state);
112 self
113 }
114 pub fn set_next_symbol(&mut self, symbol: A) -> &mut Self {
116 self.tail_mut().set_symbol(symbol);
117 self
118 }
119 pub fn set_head(&mut self, state: Q, symbol: A) -> &mut Self {
121 self.head_mut().set_state(state);
122 self.head_mut().set_symbol(symbol);
123 self
124 }
125 pub fn set_tail(&mut self, state: Q, symbol: A) -> &mut Self {
127 self.tail_mut().set_state(state);
128 self.tail_mut().set_symbol(symbol);
129 self
130 }
131 pub const fn next_head(&self) -> Head<&'_ Q, &'_ A> {
133 self.tail().as_head()
134 }
135 pub fn into_next_head(self) -> Head<Q, A> {
137 self.tail.into_head()
138 }
139 pub const fn write_symbol(&self) -> &A {
141 self.tail().symbol()
142 }
143 pub fn into_tuple(self) -> (Head<Q, A>, Tail<Q, A>) {
145 (self.head, self.tail)
146 }
147 pub fn cloned(&self) -> Rule<Q, A>
149 where
150 Q: Clone,
151 A: Clone,
152 {
153 Rule {
154 head: self.head.clone(),
155 tail: self.tail.clone(),
156 }
157 }
158 pub fn copied(&self) -> Rule<Q, A>
160 where
161 Q: Clone,
162 A: Clone,
163 {
164 Rule {
165 head: self.head.clone(),
166 tail: self.tail.clone(),
167 }
168 }
169}