@@ -7,19 +7,19 @@ use crate::{
7
7
error:: Details ,
8
8
state_machines:: reading:: {
9
9
CommandTape , ItemRead , StateMachine , StateMachineControlFlow , SubStateMachine ,
10
- bytes :: BytesStateMachine , decode_zigzag , object:: ObjectStateMachine , replace_drop,
10
+ decode_zigzag_buffer , object:: ObjectStateMachine , replace_drop,
11
11
} ,
12
12
} ;
13
13
14
- pub struct ArrayStateMachine {
14
+ pub struct BlockStateMachine {
15
15
command_tape : CommandTape ,
16
16
current_sub_machine : Box < SubStateMachine > ,
17
17
tape : Vec < ItemRead > ,
18
18
left_in_current_block : usize ,
19
19
need_to_read_block_byte_size : bool ,
20
20
}
21
21
22
- impl ArrayStateMachine {
22
+ impl BlockStateMachine {
23
23
pub fn new ( command_tape : CommandTape , tape : Vec < ItemRead > ) -> Self {
24
24
Self {
25
25
command_tape,
@@ -31,17 +31,16 @@ impl ArrayStateMachine {
31
31
}
32
32
}
33
33
34
- impl StateMachine for ArrayStateMachine {
34
+ impl StateMachine for BlockStateMachine {
35
35
type Output = Vec < ItemRead > ;
36
-
37
36
fn parse (
38
37
mut self ,
39
38
buffer : & mut Buffer ,
40
39
) -> Result < StateMachineControlFlow < Self , Self :: Output > , Error > {
41
40
loop {
42
41
// If we finished the last block (or are newly created) read the block info
43
42
if self . left_in_current_block == 0 {
44
- let Some ( block) = decode_zigzag ( buffer) ? else {
43
+ let Some ( block) = decode_zigzag_buffer ( buffer) ? else {
45
44
// Not enough data left in the buffer
46
45
return Ok ( StateMachineControlFlow :: NeedMore ( self ) ) ;
47
46
} ;
@@ -57,7 +56,7 @@ impl StateMachine for ArrayStateMachine {
57
56
}
58
57
// If the block length was negative we need to read the block size
59
58
if self . need_to_read_block_byte_size {
60
- let Some ( block) = decode_zigzag ( buffer) ? else {
59
+ let Some ( block) = decode_zigzag_buffer ( buffer) ? else {
61
60
// Not enough data left in the buffer
62
61
return Ok ( StateMachineControlFlow :: NeedMore ( self ) ) ;
63
62
} ;
@@ -107,167 +106,3 @@ impl StateMachine for ArrayStateMachine {
107
106
}
108
107
}
109
108
}
110
-
111
- pub struct MapStateMachine {
112
- command_tape : CommandTape ,
113
- current_sub_machine : Box < SubStateMachine > ,
114
- tape : Vec < ItemRead > ,
115
- left_in_current_block : usize ,
116
- need_to_read_block_byte_size : bool ,
117
- }
118
-
119
- impl MapStateMachine {
120
- pub fn new ( command_tape : CommandTape , tape : Vec < ItemRead > ) -> Self {
121
- Self {
122
- command_tape,
123
- current_sub_machine : Box :: new ( SubStateMachine :: None ) ,
124
- tape,
125
- left_in_current_block : 0 ,
126
- need_to_read_block_byte_size : false ,
127
- }
128
- }
129
- }
130
-
131
- impl StateMachine for MapStateMachine {
132
- type Output = Vec < ItemRead > ;
133
-
134
- fn parse (
135
- mut self ,
136
- buffer : & mut Buffer ,
137
- ) -> Result < StateMachineControlFlow < Self , Self :: Output > , Error > {
138
- loop {
139
- // If we finished the last block (or are newly created) read the block info
140
- if self . left_in_current_block == 0 {
141
- let Some ( block) = decode_zigzag ( buffer) ? else {
142
- // Not enough data left in the buffer
143
- return Ok ( StateMachineControlFlow :: NeedMore ( self ) ) ;
144
- } ;
145
- self . need_to_read_block_byte_size = block. is_negative ( ) ;
146
- let abs_block = block. unsigned_abs ( ) ;
147
- let abs_block = usize:: try_from ( abs_block)
148
- . map_err ( |e| Details :: ConvertU64ToUsize ( e, abs_block) ) ?;
149
- self . tape . push ( ItemRead :: Block ( abs_block) ) ;
150
- if abs_block == 0 {
151
- // Done parsing the map
152
- return Ok ( StateMachineControlFlow :: Done ( self . tape ) ) ;
153
- }
154
- }
155
- // If the block length was negative we need to read the block size
156
- if self . need_to_read_block_byte_size {
157
- let Some ( block) = decode_zigzag ( buffer) ? else {
158
- // Not enough data left in the buffer
159
- return Ok ( StateMachineControlFlow :: NeedMore ( self ) ) ;
160
- } ;
161
- // Make sure the value is sane
162
- let _ = usize:: try_from ( block) . map_err ( |e| Details :: ConvertI64ToUsize ( e, block) ) ?;
163
- self . need_to_read_block_byte_size = false ;
164
- }
165
-
166
- // Either run the existing state machine or create a new one and run that
167
- match std:: mem:: take ( self . current_sub_machine . deref_mut ( ) ) {
168
- SubStateMachine :: None => {
169
- let fsm = BytesStateMachine :: new ( ) ;
170
- // Optimistically run the state machine
171
- match fsm. parse ( buffer) ? {
172
- StateMachineControlFlow :: NeedMore ( fsm) => {
173
- replace_drop (
174
- self . current_sub_machine . deref_mut ( ) ,
175
- SubStateMachine :: String ( fsm) ,
176
- ) ;
177
- return Ok ( StateMachineControlFlow :: NeedMore ( self ) ) ;
178
- }
179
- StateMachineControlFlow :: Done ( bytes) => {
180
- let string =
181
- String :: from_utf8 ( bytes) . map_err ( Details :: ConvertToUtf8 ) ?;
182
- self . tape . push ( ItemRead :: String ( string. into_boxed_str ( ) ) ) ;
183
- replace_drop (
184
- self . current_sub_machine . deref_mut ( ) ,
185
- SubStateMachine :: None ,
186
- ) ;
187
- }
188
- }
189
- // Finished reading the key, start on the value
190
- let fsm = ObjectStateMachine :: new_with_tape (
191
- self . command_tape . clone ( ) ,
192
- std:: mem:: take ( & mut self . tape ) ,
193
- ) ;
194
- // Optimistically run the state machine
195
- match fsm. parse ( buffer) ? {
196
- StateMachineControlFlow :: NeedMore ( fsm) => {
197
- replace_drop (
198
- self . current_sub_machine . deref_mut ( ) ,
199
- SubStateMachine :: Object ( fsm) ,
200
- ) ;
201
- return Ok ( StateMachineControlFlow :: NeedMore ( self ) ) ;
202
- }
203
- StateMachineControlFlow :: Done ( tape) => {
204
- self . tape = tape;
205
- self . left_in_current_block -= 1 ;
206
- }
207
- }
208
- }
209
- SubStateMachine :: String ( fsm) => {
210
- // We didn't finish reading the key last loop
211
- match fsm. parse ( buffer) ? {
212
- StateMachineControlFlow :: NeedMore ( fsm) => {
213
- replace_drop (
214
- self . current_sub_machine . deref_mut ( ) ,
215
- SubStateMachine :: String ( fsm) ,
216
- ) ;
217
- return Ok ( StateMachineControlFlow :: NeedMore ( self ) ) ;
218
- }
219
- StateMachineControlFlow :: Done ( bytes) => {
220
- let string =
221
- String :: from_utf8 ( bytes) . map_err ( Details :: ConvertToUtf8 ) ?;
222
- self . tape . push ( ItemRead :: String ( string. into_boxed_str ( ) ) ) ;
223
- replace_drop (
224
- self . current_sub_machine . deref_mut ( ) ,
225
- SubStateMachine :: None ,
226
- ) ;
227
- }
228
- }
229
- // Finished reading the key, start on the value
230
- let fsm = ObjectStateMachine :: new_with_tape (
231
- self . command_tape . clone ( ) ,
232
- std:: mem:: take ( & mut self . tape ) ,
233
- ) ;
234
- // Optimistically run the state machine
235
- match fsm. parse ( buffer) ? {
236
- StateMachineControlFlow :: NeedMore ( fsm) => {
237
- replace_drop (
238
- self . current_sub_machine . deref_mut ( ) ,
239
- SubStateMachine :: Object ( fsm) ,
240
- ) ;
241
- return Ok ( StateMachineControlFlow :: NeedMore ( self ) ) ;
242
- }
243
- StateMachineControlFlow :: Done ( tape) => {
244
- self . tape = tape;
245
- self . left_in_current_block -= 1 ;
246
- }
247
- }
248
- }
249
- SubStateMachine :: Object ( fsm) => {
250
- // We didn't finish reading the value last loop
251
- match fsm. parse ( buffer) ? {
252
- StateMachineControlFlow :: NeedMore ( fsm) => {
253
- replace_drop (
254
- self . current_sub_machine . deref_mut ( ) ,
255
- SubStateMachine :: Object ( fsm) ,
256
- ) ;
257
- return Ok ( StateMachineControlFlow :: NeedMore ( self ) ) ;
258
- }
259
- StateMachineControlFlow :: Done ( tape) => {
260
- self . tape = tape;
261
- replace_drop (
262
- self . current_sub_machine . deref_mut ( ) ,
263
- SubStateMachine :: None ,
264
- ) ;
265
- self . left_in_current_block -= 1 ;
266
- }
267
- }
268
- }
269
- _ => unreachable ! ( ) ,
270
- }
271
- }
272
- }
273
- }
0 commit comments