rstm_core/head/
impl_head_ext.rs

1/*
2    Appellation: impl_head_ext <module>
3    Created At: 2025.09.05:17:57:25
4    Contrib: @FL03
5*/
6use crate::head::Head;
7
8use crate::Rule;
9use crate::tail::Tail;
10
11use rstm_state::{RawState, State};
12
13impl<Q, S> core::fmt::Debug for Head<Q, S>
14where
15    Q: core::fmt::Debug,
16    S: core::fmt::Debug,
17{
18    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
19        f.debug_struct("Head")
20            .field("state", &self.state)
21            .field("symbol", &self.symbol)
22            .finish()
23    }
24}
25
26impl<Q, S> core::fmt::Display for Head<Q, S>
27where
28    Q: core::fmt::Display,
29    S: core::fmt::Display,
30{
31    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
32        write!(f, "{{ state: {}, symbol: {} }}", self.state, self.symbol)
33    }
34}
35
36/*
37 ************* Operations *************
38*/
39
40impl<Q, A> core::ops::Add<Tail<Q, A>> for Head<Q, A>
41where
42    Q: RawState,
43{
44    type Output = Rule<Q, A>;
45
46    fn add(self, rhs: Tail<Q, A>) -> Self::Output {
47        Rule {
48            head: self,
49            tail: rhs,
50        }
51    }
52}
53
54/*
55 ************* Equivalence *************
56*/
57
58impl<Q, S> PartialEq<State<Q>> for Head<Q, S>
59where
60    Q: RawState + PartialEq,
61{
62    fn eq(&self, state: &State<Q>) -> bool {
63        self.state() == state
64    }
65}
66
67impl<Q, S> PartialEq<Head<Q, S>> for State<Q>
68where
69    Q: RawState + PartialEq,
70{
71    fn eq(&self, head: &Head<Q, S>) -> bool {
72        self == head.state()
73    }
74}
75
76impl<Q, S> PartialEq<Head<Q, S>> for State<&Q>
77where
78    Q: RawState + PartialEq,
79{
80    fn eq(&self, head: &Head<Q, S>) -> bool {
81        *self == head.state().view()
82    }
83}
84
85impl<'a, Q, S> PartialEq<State<&'a Q>> for Head<Q, S>
86where
87    Q: RawState + PartialEq,
88{
89    fn eq(&self, state: &State<&'a Q>) -> bool {
90        self.state().view() == *state
91    }
92}
93
94impl<Q, S> PartialEq<(State<Q>, S)> for Head<Q, S>
95where
96    Q: RawState + PartialEq,
97    S: PartialEq,
98{
99    fn eq(&self, (state, symbol): &(State<Q>, S)) -> bool {
100        &self.state == state && &self.symbol == symbol
101    }
102}
103
104impl<Q, S> PartialEq<(Q, S)> for Head<Q, S>
105where
106    State<Q>: PartialEq,
107    Q: RawState + PartialEq,
108    S: PartialEq,
109{
110    fn eq(&self, (state, symbol): &(Q, S)) -> bool {
111        self.state() == state && self.symbol() == symbol
112    }
113}
114
115impl<Q, S> PartialEq<Head<Q, S>> for (State<Q>, S)
116where
117    Q: RawState + PartialEq,
118    S: PartialEq,
119{
120    fn eq(&self, head: &Head<Q, S>) -> bool {
121        head.state() == &self.0 && head.symbol() == &self.1
122    }
123}