rstm_core/rules/
rule.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
    Appellation: instruction <module>
    Contrib: FL03 <jo3mccain@icloud.com>
*/
use super::RuleBuilder;

use crate::{Head, State, Tail};

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Ord, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Rule<Q = String, A = char> {
    pub head: Head<Q, A>,
    pub tail: Tail<Q, A>,
}

impl<Q, A> Rule<Q, A> {
    pub fn new() -> RuleBuilder<Q, A> {
        RuleBuilder::new()
    }
    /// Returns an immutable reference to the [Head]
    pub const fn head(&self) -> &Head<Q, A> {
        &self.head
    }
    /// Returns a mutable reference to the [Head]
    pub fn head_mut(&mut self) -> &mut Head<Q, A> {
        &mut self.head
    }
    /// Returns an instance of the [Head] whose elements are immutable references
    pub fn head_ref(&self) -> Head<&'_ Q, &'_ A> {
        self.head().to_ref()
    }
    /// Returns an immutable reference to the [Tail] of the [Instruction]
    pub const fn tail(&self) -> &Tail<Q, A> {
        &self.tail
    }
    /// Returns a mutable reference to the [Tail] of the [Instruction]
    pub fn tail_mut(&mut self) -> &mut Tail<Q, A> {
        &mut self.tail
    }
    /// Returns an instance of the [Tail] whose elements are immutable references
    pub fn tail_ref(&self) -> Tail<&'_ Q, &'_ A> {
        self.tail().to_ref()
    }
    /// Returns the direction of the shift
    pub fn direction(&self) -> crate::Direction {
        self.tail().direction()
    }
    /// Returns the current [State] of the system
    pub fn state(&self) -> State<&'_ Q> {
        self.head().state()
    }
    /// Returns the symbol of the [Head]
    pub const fn symbol(&self) -> &A {
        self.head().symbol()
    }
    /// Returns the next [Head] of the system
    pub fn next_head(&self) -> Head<&'_ Q, &'_ A> {
        self.tail().as_head()
    }
    /// Consumes the current object and returns the next [Head] of the system
    pub fn into_next_head(self) -> Head<Q, A> {
        self.tail.into_head()
    }
    /// Returns the next [State] of the system
    pub fn next_state(&self) -> State<&'_ Q> {
        self.tail().state()
    }
    /// Returns the value which for which the current object will be replaced with
    pub const fn write_symbol(&self) -> &A {
        self.tail().symbol()
    }
    /// Consumes the current object and returns a 2-tuple consisting of the [Head] and [Tail]
    pub fn into_tuple(self) -> (Head<Q, A>, Tail<Q, A>) {
        (self.head, self.tail)
    }
}

impl<'a, Q, A> Rule<&'a Q, &'a A> {
    pub fn cloned(&self) -> Rule<Q, A>
    where
        Q: Clone,
        A: Clone,
    {
        Rule {
            head: self.head.cloned(),
            tail: self.tail.cloned(),
        }
    }

    pub fn copied(&self) -> Rule<Q, A>
    where
        Q: Copy,
        A: Copy,
    {
        Rule {
            head: self.head.copied(),
            tail: self.tail.copied(),
        }
    }
}

mod impls {
    use super::Rule;
    use crate::{Head, Tail};

    impl<Q, S> core::convert::AsRef<Head<Q, S>> for Rule<Q, S> {
        fn as_ref(&self) -> &Head<Q, S> {
            self.head()
        }
    }

    impl<Q, S> core::convert::AsRef<Tail<Q, S>> for Rule<Q, S> {
        fn as_ref(&self) -> &Tail<Q, S> {
            self.tail()
        }
    }

    impl<Q, S> core::convert::AsMut<Head<Q, S>> for Rule<Q, S> {
        fn as_mut(&mut self) -> &mut Head<Q, S> {
            self.head_mut()
        }
    }

    impl<Q, S> core::convert::AsMut<Tail<Q, S>> for Rule<Q, S> {
        fn as_mut(&mut self) -> &mut Tail<Q, S> {
            self.tail_mut()
        }
    }

    impl<Q, S> core::borrow::Borrow<Head<Q, S>> for Rule<Q, S> {
        fn borrow(&self) -> &Head<Q, S> {
            self.head()
        }
    }

    impl<Q, S> core::borrow::Borrow<Tail<Q, S>> for Rule<Q, S> {
        fn borrow(&self) -> &Tail<Q, S> {
            self.tail()
        }
    }

    impl<Q, S> core::borrow::BorrowMut<Head<Q, S>> for Rule<Q, S> {
        fn borrow_mut(&mut self) -> &mut Head<Q, S> {
            self.head_mut()
        }
    }

    impl<Q, S> core::borrow::BorrowMut<Tail<Q, S>> for Rule<Q, S> {
        fn borrow_mut(&mut self) -> &mut Tail<Q, S> {
            self.tail_mut()
        }
    }

    impl<Q, S> PartialEq<(Head<Q, S>, Tail<Q, S>)> for Rule<Q, S>
    where
        Q: PartialEq,
        S: PartialEq,
    {
        fn eq(&self, other: &(Head<Q, S>, Tail<Q, S>)) -> bool {
            self.head == other.0 && self.tail == other.1
        }
    }

    impl<Q, S> PartialEq<Head<Q, S>> for Rule<Q, S>
    where
        Q: PartialEq,
        S: PartialEq,
    {
        fn eq(&self, other: &Head<Q, S>) -> bool {
            &self.head == other
        }
    }

    impl<Q, S> PartialEq<Tail<Q, S>> for Rule<Q, S>
    where
        Q: PartialEq,
        S: PartialEq,
    {
        fn eq(&self, other: &Tail<Q, S>) -> bool {
            &self.tail == other
        }
    }

    impl<Q, S> From<(Head<Q, S>, Tail<Q, S>)> for Rule<Q, S> {
        fn from((head, tail): (Head<Q, S>, Tail<Q, S>)) -> Self {
            Self { head, tail }
        }
    }

    impl<Q, S> From<Rule<Q, S>> for (Head<Q, S>, Tail<Q, S>) {
        fn from(rule: Rule<Q, S>) -> Self {
            (rule.head, rule.tail)
        }
    }
}