rsonpath/automaton/
state.rs1use std::{fmt::Display, ops::BitOr};
4
5#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
7#[repr(u8)]
8pub(crate) enum StateAttribute {
9 Accepting = 0x01,
11 Rejecting = 0x02,
14 Unitary = 0x04,
18 TransitionsToAccepting = 0x08,
21 HasArrayTransition = 0x10,
23 HasArrayTransitionToAccepting = 0x20,
26}
27
28pub(crate) struct StateAttributesBuilder {
29 attrs: StateAttributes,
30}
31
32#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
34#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default)]
35pub struct StateAttributes(u8);
36
37impl StateAttributesBuilder {
38 pub(crate) fn new() -> Self {
39 Self {
40 attrs: StateAttributes(0),
41 }
42 }
43
44 pub(crate) fn accepting(self) -> Self {
45 self.set(StateAttribute::Accepting)
46 }
47
48 pub(crate) fn rejecting(self) -> Self {
49 self.set(StateAttribute::Rejecting)
50 }
51
52 pub(crate) fn unitary(self) -> Self {
53 self.set(StateAttribute::Unitary)
54 }
55
56 pub(crate) fn transitions_to_accepting(self) -> Self {
57 self.set(StateAttribute::TransitionsToAccepting)
58 }
59
60 pub(crate) fn has_array_transition(self) -> Self {
61 self.set(StateAttribute::HasArrayTransition)
62 }
63
64 pub(crate) fn has_array_transition_to_accepting(self) -> Self {
65 self.set(StateAttribute::HasArrayTransitionToAccepting)
66 }
67
68 pub(crate) fn build(self) -> StateAttributes {
69 self.attrs
70 }
71
72 fn set(self, attr: StateAttribute) -> Self {
73 Self {
74 attrs: StateAttributes(self.attrs.0 | attr as u8),
75 }
76 }
77}
78
79impl From<StateAttributesBuilder> for StateAttributes {
80 #[inline(always)]
81 fn from(value: StateAttributesBuilder) -> Self {
82 value.build()
83 }
84}
85
86impl BitOr for StateAttributes {
87 type Output = Self;
88
89 #[inline(always)]
90 fn bitor(self, rhs: Self) -> Self::Output {
91 Self(self.0 | rhs.0)
92 }
93}
94
95impl StateAttributes {
96 pub const ACCEPTING: Self = Self(StateAttribute::Accepting as u8);
98 pub const EMPTY: Self = Self(0);
100 pub const REJECTING: Self = Self(StateAttribute::Rejecting as u8);
103 pub const TRANSITIONS_TO_ACCEPTING: Self = Self(StateAttribute::TransitionsToAccepting as u8);
106 pub const UNITARY: Self = Self(StateAttribute::Unitary as u8);
110 pub const HAS_ARRAY_TRANSITION: Self = Self(StateAttribute::HasArrayTransition as u8);
112 pub const HAS_ARRAY_TRANSITION_TO_ACCEPTING: Self = Self(StateAttribute::HasArrayTransitionToAccepting as u8);
115
116 #[inline(always)]
118 #[must_use]
119 pub fn is_accepting(&self) -> bool {
120 self.is_set(StateAttribute::Accepting)
121 }
122
123 #[inline(always)]
126 #[must_use]
127 pub fn is_rejecting(&self) -> bool {
128 self.is_set(StateAttribute::Rejecting)
129 }
130
131 #[inline(always)]
134 #[must_use]
135 pub fn has_transition_to_accepting(&self) -> bool {
136 self.is_set(StateAttribute::TransitionsToAccepting)
137 }
138
139 #[inline(always)]
143 #[must_use]
144 pub fn is_unitary(&self) -> bool {
145 self.is_set(StateAttribute::Unitary)
146 }
147
148 #[inline(always)]
150 #[must_use]
151 pub fn has_array_transition(&self) -> bool {
152 self.is_set(StateAttribute::HasArrayTransition)
153 }
154
155 #[inline(always)]
158 #[must_use]
159 pub fn has_array_transition_to_accepting(&self) -> bool {
160 self.is_set(StateAttribute::HasArrayTransitionToAccepting)
161 }
162
163 #[inline(always)]
164 #[must_use]
165 fn is_set(&self, attr: StateAttribute) -> bool {
166 (self.0 & attr as u8) != 0
167 }
168}
169
170#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
172#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
173pub struct State(
174 pub(super) u8,
178);
179
180impl Display for State {
181 #[inline]
182 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
183 write!(f, "DFA({})", self.0)
184 }
185}