1use crate::rules::Tail;
7use crate::{Direction, Head};
8use rstm_state::{RawState, State};
9
10impl<Q, A> core::fmt::Debug for Tail<Q, A>
11where
12 Q: core::fmt::Debug,
13 A: core::fmt::Debug,
14{
15 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
16 f.debug_tuple("Tail")
17 .field(&self.direction)
18 .field(&self.next_state)
19 .field(&self.write_symbol)
20 .finish()
21 }
22}
23
24impl<Q, S> core::fmt::Display for Tail<Q, S>
25where
26 Q: core::fmt::Display,
27 S: core::fmt::Display,
28{
29 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
30 write!(
31 f,
32 "{{ direction: {}, next_state: {}, write_symbol: {} }}",
33 self.direction, self.next_state, self.write_symbol
34 )
35 }
36}
37
38impl<Q, A, R, B> core::ops::Add<Head<Q, A>> for Tail<R, B>
39where
40 Q: RawState,
41 R: RawState,
42{
43 type Output = crate::Rule<Q, A, R, B>;
44
45 fn add(self, rhs: Head<Q, A>) -> Self::Output {
46 crate::Rule::new(rhs, self)
47 }
48}
49
50impl<Q, A> core::borrow::Borrow<Direction> for Tail<Q, A>
51where
52 Q: RawState,
53{
54 fn borrow(&self) -> &Direction {
55 &self.direction
56 }
57}
58
59impl<Q, A> core::borrow::Borrow<State<Q>> for Tail<Q, A>
60where
61 Q: RawState,
62{
63 fn borrow(&self) -> &State<Q> {
64 &self.next_state
65 }
66}
67
68impl<Q, A> core::borrow::BorrowMut<State<Q>> for Tail<Q, A>
69where
70 Q: RawState,
71{
72 fn borrow_mut(&mut self) -> &mut State<Q> {
73 &mut self.next_state
74 }
75}
76
77impl<Q, A> From<(Direction, Head<Q, A>)> for Tail<Q, A> {
78 fn from((direction, head): (Direction, Head<Q, A>)) -> Self {
79 Tail {
80 direction,
81 next_state: head.state,
82 write_symbol: head.symbol,
83 }
84 }
85}
86
87impl<Q, A> From<Tail<Q, A>> for (Direction, Head<Q, A>) {
88 fn from(tail: Tail<Q, A>) -> Self {
89 (
90 tail.direction,
91 Head {
92 state: tail.next_state,
93 symbol: tail.write_symbol,
94 },
95 )
96 }
97}
98
99impl<Q, A> From<(Direction, State<Q>, A)> for Tail<Q, A> {
100 fn from((direction, next_state, write_symbol): (Direction, State<Q>, A)) -> Self {
101 Tail {
102 direction,
103 next_state,
104 write_symbol,
105 }
106 }
107}
108
109impl<Q, A> From<(Direction, Q, A)> for Tail<Q, A> {
110 fn from((direction, next_state, write_symbol): (Direction, Q, A)) -> Self {
111 Tail {
112 direction,
113 next_state: State(next_state),
114 write_symbol,
115 }
116 }
117}
118
119impl<Q, A> From<Tail<Q, A>> for (Direction, State<Q>, A) {
120 fn from(tail: Tail<Q, A>) -> Self {
121 (tail.direction, tail.next_state, tail.write_symbol)
122 }
123}
124
125impl<Q, A> PartialEq<(Direction, Head<Q, A>)> for Tail<Q, A>
126where
127 Q: PartialEq,
128 A: PartialEq,
129{
130 fn eq(&self, (other_direction, other_head): &(Direction, Head<Q, A>)) -> bool {
131 &self.direction == other_direction
132 && self.next_state == other_head.state
133 && self.write_symbol == other_head.symbol
134 }
135}
136
137impl<Q, A> PartialEq<(Direction, State<Q>, A)> for Tail<Q, A>
138where
139 Q: PartialEq,
140 A: PartialEq,
141{
142 fn eq(&self, (other_direction, other_state, other_symbol): &(Direction, State<Q>, A)) -> bool {
143 &self.direction == other_direction
144 && &self.next_state == other_state
145 && &self.write_symbol == other_symbol
146 }
147}
148
149impl<Q, A> PartialEq<Tail<Q, A>> for (Direction, State<Q>, A)
150where
151 Q: PartialEq,
152 A: PartialEq,
153{
154 fn eq(&self, other: &Tail<Q, A>) -> bool {
155 self.0 == other.direction && self.1 == other.next_state && self.2 == other.write_symbol
156 }
157}
158
159impl<Q, A> PartialEq<(State<Q>, A)> for Tail<Q, A>
160where
161 Q: PartialEq,
162 A: PartialEq,
163{
164 fn eq(&self, (other_state, other_symbol): &(State<Q>, A)) -> bool {
165 &self.next_state == other_state && &self.write_symbol == other_symbol
166 }
167}
168
169impl<Q, A> PartialEq<Tail<Q, A>> for (State<Q>, A)
170where
171 Q: PartialEq,
172 A: PartialEq,
173{
174 fn eq(&self, other: &Tail<Q, A>) -> bool {
175 self.0 == other.next_state && self.1 == other.write_symbol
176 }
177}