• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kaidokert / picojson-rs / 16710046716

03 Aug 2025 10:21PM UTC coverage: 93.498%. First build
16710046716

Pull #78

github

web-flow
Merge 027f84e7a into 121371655
Pull Request #78: Push full

492 of 583 new or added lines in 9 files covered. (84.39%)

4990 of 5337 relevant lines covered (93.5%)

1312.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.4
/picojson/src/event_processor.rs
1
// SPDX-License-Identifier: Apache-2.0
2

3
//! Shared event processing logic between SliceParser and StreamParser.
4
//!
5
//! This module extracts the common event handling patterns to reduce code duplication
6
//! while preserving the performance characteristics of each parser type.
7

8
use crate::escape_processor::{EscapeProcessor, UnicodeEscapeCollector};
9
use crate::shared::{ContentRange, Event, ParserState, State, UnexpectedState};
10
use crate::ujson::{EventToken, Tokenizer};
11
use crate::{ujson, ParseError};
12

13
/// The core parser logic that handles the unified event processing loop.
14
///
15
/// This struct contains all the shared state and logic that was previously
16
/// duplicated between SliceParser and StreamParser. It uses trait abstractions
17
/// to handle the differences in content building and byte providing.
18
pub struct ParserCore<T: ujson::BitBucket, C: ujson::DepthCounter> {
19
    /// The tokenizer that processes JSON tokens
20
    pub tokenizer: Tokenizer<T, C>,
21
    /// Parser state and event storage
22
    pub parser_state: ParserState,
23
    /// Tracks if the parser is currently inside any escape sequence (\n, \uXXXX, etc.)
24
    in_escape_sequence: bool,
25
    /// Whether this parser handles chunked input (true for PushParser, false for Slice/Stream)
26
    /// When true, running out of input returns EndOfData. When false, calls tokenizer.finish().
27
    handles_chunked_input: bool,
28
}
29

30
impl<T: ujson::BitBucket, C: ujson::DepthCounter> ParserCore<T, C> {
31
    /// Create a new ParserCore for non-chunked parsers (SliceParser, StreamParser)
32
    pub fn new() -> Self {
1,134✔
33
        Self {
1,134✔
34
            tokenizer: Tokenizer::new(),
1,134✔
35
            parser_state: ParserState::new(),
1,134✔
36
            in_escape_sequence: false,
1,134✔
37
            handles_chunked_input: false,
1,134✔
38
        }
1,134✔
39
    }
1,134✔
40

41
    /// Create a new ParserCore for chunked parsers (PushParser)
42
    pub fn new_chunked() -> Self {
1,061✔
43
        Self {
1,061✔
44
            tokenizer: Tokenizer::new(),
1,061✔
45
            parser_state: ParserState::new(),
1,061✔
46
            in_escape_sequence: false,
1,061✔
47
            handles_chunked_input: true,
1,061✔
48
        }
1,061✔
49
    }
1,061✔
50

51
    /// Unified implementation with optional byte accumulation callback.
52
    /// This supports StreamParser-specific byte accumulation when no events are generated.
53
    /// SliceParser passes a no-op closure for byte_accumulator.
54
    pub fn next_event_impl<'a, P, F>(
5,272✔
55
        &mut self,
5,272✔
56
        provider: &'a mut P,
5,272✔
57
        escape_timing: EscapeTiming,
5,272✔
58
        byte_accumulator: F,
5,272✔
59
    ) -> Result<Event<'a, 'a>, ParseError>
5,272✔
60
    where
5,272✔
61
        P: ContentExtractor,
5,272✔
62
        F: FnMut(&mut P, u8) -> Result<(), ParseError>,
5,272✔
63
    {
64
        self.next_event_impl_with_flags(provider, escape_timing, byte_accumulator, false)
5,272✔
65
    }
5,272✔
66

67
    /// Extended version with flags for specialized behavior
68
    pub fn next_event_impl_with_flags<'a, P, F>(
18,685✔
69
        &mut self,
18,685✔
70
        provider: &'a mut P,
18,685✔
71
        escape_timing: EscapeTiming,
18,685✔
72
        mut byte_accumulator: F,
18,685✔
73
        always_accumulate_during_escapes: bool,
18,685✔
74
    ) -> Result<Event<'a, 'a>, ParseError>
18,685✔
75
    where
18,685✔
76
        P: ContentExtractor,
18,685✔
77
        F: FnMut(&mut P, u8) -> Result<(), ParseError>,
18,685✔
78
    {
79
        loop {
80
            while !have_events(&self.parser_state.evts) {
77,601✔
81
                if let Some(byte) = provider.next_byte()? {
56,527✔
82
                    {
83
                        clear_events(&mut self.parser_state.evts);
49,056✔
84
                        let mut callback = create_tokenizer_callback(&mut self.parser_state.evts);
49,056✔
85
                        self.tokenizer
49,056✔
86
                            .parse_chunk(&[byte], &mut callback)
49,056✔
87
                            .map_err(ParseError::TokenizerError)?;
49,056✔
88
                    }
89

90
                    // Call byte accumulator if no events were generated AND we're not in an escape sequence
91
                    // OR if we're configured to always accumulate during escape sequences (for PushParser)
92
                    // OR if we always accumulate during escapes AND we're processing Unicode escape hex digits
93
                    let should_accumulate = if always_accumulate_during_escapes {
49,041✔
94
                        // For PushParser: accumulate during escapes even when events are generated
95
                        // This ensures hex digits reach the accumulator even when End UnicodeEscape events consume them
96
                        // BUT still respect the normal logic when not in escape sequences
97
                        if self.in_escape_sequence {
28,509✔
98
                            true // Always accumulate during escape sequences
2,964✔
99
                        } else {
100
                            !have_events(&self.parser_state.evts) // Normal behavior outside escapes
25,545✔
101
                        }
102
                    } else {
103
                        // For other parsers: only accumulate when no events generated and not in escape
104
                        !have_events(&self.parser_state.evts) && !self.in_escape_sequence
20,532✔
105
                    };
106

107
                    if should_accumulate {
49,041✔
108
                        byte_accumulator(provider, byte)?;
30,880✔
109
                    }
18,161✔
110
                } else {
111
                    // Handle end of input - behavior depends on parser type
112
                    if self.handles_chunked_input {
6,940✔
113
                        // For chunked parsers (PushParser), return EndOfData so they can handle chunk boundaries
114
                        return Err(ParseError::EndOfData);
6,418✔
115
                    } else {
116
                        // For non-chunked parsers (SliceParser, StreamParser), finish the document
117
                        {
118
                            let mut finish_callback =
522✔
119
                                create_tokenizer_callback(&mut self.parser_state.evts);
522✔
120
                            let _bytes_processed = self.tokenizer.finish(&mut finish_callback)?;
522✔
121
                        } // Drop the callback to release the borrow
122

123
                        // If finish() generated events, process them. Otherwise, return EndDocument.
124
                        if !have_events(&self.parser_state.evts) {
518✔
125
                            return Ok(Event::EndDocument);
514✔
126
                        }
4✔
127
                        // Continue to process any events generated by finish()
128
                    }
129
                }
130
            }
131

132
            let taken_event = take_first_event(&mut self.parser_state.evts);
21,074✔
133
            let Some(taken) = taken_event else {
21,074✔
134
                return Err(UnexpectedState::StateMismatch.into());
×
135
            };
136

137
            // Try shared event processors first
138
            if let Some(result) =
15,110✔
139
                process_simple_events(&taken).or_else(|| provider.process_begin_events(&taken))
21,074✔
140
            {
141
                match result {
15,110✔
142
                    EventResult::Complete(event) => return Ok(event),
7,754✔
143
                    EventResult::ExtractString => return provider.validate_and_extract_string(),
1,289✔
144
                    EventResult::ExtractKey => return provider.validate_and_extract_key(),
1,229✔
145
                    EventResult::ExtractNumber(from_container_end) => {
815✔
146
                        return provider.validate_and_extract_number(from_container_end)
815✔
147
                    }
148
                    EventResult::Continue => continue,
4,023✔
149
                }
150
            }
5,964✔
151

152
            // Handle parser-specific events based on escape timing
153
            match taken {
252✔
154
                ujson::Event::Begin(EventToken::EscapeSequence) => {
155
                    self.in_escape_sequence = true;
2,044✔
156
                    provider.process_begin_escape_sequence_event()?;
2,044✔
157
                }
158
                ujson::Event::Begin(EventToken::UnicodeEscape) => {
159
                    self.in_escape_sequence = true;
685✔
160
                    provider.process_unicode_escape_events(&taken)?;
685✔
161
                }
162
                ujson::Event::End(EventToken::UnicodeEscape) => {
163
                    self.in_escape_sequence = false;
655✔
164
                    provider.process_unicode_escape_events(&taken)?;
655✔
165
                }
166
                ujson::Event::Begin(
167
                    escape_token @ (EventToken::EscapeQuote
1✔
168
                    | EventToken::EscapeBackslash
169
                    | EventToken::EscapeSlash
170
                    | EventToken::EscapeBackspace
171
                    | EventToken::EscapeFormFeed
172
                    | EventToken::EscapeNewline
173
                    | EventToken::EscapeCarriageReturn
174
                    | EventToken::EscapeTab),
175
                ) if escape_timing == EscapeTiming::OnBegin => {
252✔
176
                    // For SliceParser, the escape is handled in a single event.
177
                    // It begins and ends within this block.
178
                    self.in_escape_sequence = true;
20✔
179
                    provider.process_simple_escape_event(&escape_token)?;
20✔
180
                    self.in_escape_sequence = false;
18✔
181
                }
182
                ujson::Event::End(
183
                    escape_token @ (EventToken::EscapeQuote
251✔
184
                    | EventToken::EscapeBackslash
185
                    | EventToken::EscapeSlash
186
                    | EventToken::EscapeBackspace
187
                    | EventToken::EscapeFormFeed
188
                    | EventToken::EscapeNewline
189
                    | EventToken::EscapeCarriageReturn
190
                    | EventToken::EscapeTab),
191
                ) if escape_timing == EscapeTiming::OnEnd => {
252✔
192
                    // For StreamParser, the escape ends here.
193
                    provider.process_simple_escape_event(&escape_token)?;
1,271✔
194
                    self.in_escape_sequence = false;
1,259✔
195
                }
196
                _ => {
1,289✔
197
                    // All other events continue to next iteration
1,289✔
198
                }
1,289✔
199
            }
200
        }
201
    }
18,685✔
202
}
203

204
impl<T: ujson::BitBucket, C: ujson::DepthCounter> Default for ParserCore<T, C> {
205
    fn default() -> Self {
×
206
        Self::new()
×
207
    }
×
208
}
209

210
/// Enum to specify when escape sequences should be processed
211
#[derive(Debug, Clone, Copy, PartialEq)]
212
pub enum EscapeTiming {
213
    /// Process simple escape sequences on Begin events (SliceParser)
214
    OnBegin,
215
    /// Process simple escape sequences on End events (StreamParser)
216
    OnEnd,
217
}
218

219
/// Result of processing a tokenizer event
220
#[derive(Debug)]
221
pub enum EventResult<'a, 'b> {
222
    /// Event processing is complete, return this event to the user
223
    Complete(Event<'a, 'b>),
224
    /// Continue processing more tokenizer events
225
    Continue,
226
    /// Extract string content (delegate to parser-specific logic)
227
    ExtractString,
228
    /// Extract key content (delegate to parser-specific logic)
229
    ExtractKey,
230
    /// Extract number content (delegate to parser-specific logic)
231
    /// bool indicates if number was terminated by container delimiter
232
    ExtractNumber(bool),
233
}
234

235
/// Trait for content extraction operations that differ between parsers
236
/// Consolidates ParserContext and ContentExtractor functionality
237
pub trait ContentExtractor {
238
    /// Get the next byte from the input source
239
    /// Returns None when end of input is reached
240
    fn next_byte(&mut self) -> Result<Option<u8>, ParseError>;
241

242
    /// Get current position in the input
243
    fn current_position(&self) -> usize;
244

245
    /// Begin string/key content processing at current position
246
    fn begin_string_content(&mut self, pos: usize);
247

248
    /// Get mutable access to parser state
249
    fn parser_state_mut(&mut self) -> &mut State;
250

251
    /// Get mutable access to the Unicode escape collector
252
    /// This eliminates the need for wrapper methods that just forward calls
253
    fn unicode_escape_collector_mut(&mut self) -> &mut UnicodeEscapeCollector;
254

255
    /// Extract string content using parser-specific logic
256
    fn extract_string_content(&mut self, start_pos: usize) -> Result<Event<'_, '_>, ParseError>;
257

258
    /// Extract key content using parser-specific logic
259
    fn extract_key_content(&mut self, start_pos: usize) -> Result<Event<'_, '_>, ParseError>;
260

261
    /// Extract a completed number using shared number parsing logic
262
    ///
263
    /// # Arguments
264
    /// * `start_pos` - Position where the number started
265
    /// * `from_container_end` - True if number was terminated by container delimiter
266
    /// * `finished` - True if the parser has finished processing input (StreamParser-specific)
267
    fn extract_number(
268
        &mut self,
269
        start_pos: usize,
270
        from_container_end: bool,
271
        finished: bool,
272
    ) -> Result<Event<'_, '_>, ParseError>;
273

274
    /// Shared validation and extraction for string content
275
    fn validate_and_extract_string(&mut self) -> Result<Event<'_, '_>, ParseError> {
1,289✔
276
        let start_pos = match *self.parser_state() {
1,289✔
277
            State::String(pos) => pos,
1,289✔
278
            _ => return Err(crate::shared::UnexpectedState::StateMismatch.into()),
×
279
        };
280

281
        // Check for incomplete surrogate pairs before ending the string
282
        if self
1,289✔
283
            .unicode_escape_collector_mut()
1,289✔
284
            .has_pending_high_surrogate()
1,289✔
285
        {
286
            return Err(ParseError::InvalidUnicodeCodepoint);
3✔
287
        }
1,286✔
288

289
        *self.parser_state_mut() = State::None;
1,286✔
290
        self.extract_string_content(start_pos)
1,286✔
291
    }
1,289✔
292

293
    /// Shared validation and extraction for key content
294
    fn validate_and_extract_key(&mut self) -> Result<Event<'_, '_>, ParseError> {
1,229✔
295
        let start_pos = match *self.parser_state() {
1,229✔
296
            State::Key(pos) => pos,
1,229✔
297
            _ => return Err(crate::shared::UnexpectedState::StateMismatch.into()),
×
298
        };
299

300
        // Check for incomplete surrogate pairs before ending the key
301
        if self
1,229✔
302
            .unicode_escape_collector_mut()
1,229✔
303
            .has_pending_high_surrogate()
1,229✔
304
        {
305
            return Err(ParseError::InvalidUnicodeCodepoint);
×
306
        }
1,229✔
307

308
        *self.parser_state_mut() = State::None;
1,229✔
309
        self.extract_key_content(start_pos)
1,229✔
310
    }
1,229✔
311

312
    /// Shared validation and extraction for number content
313
    fn validate_and_extract_number(
565✔
314
        &mut self,
565✔
315
        from_container_end: bool,
565✔
316
    ) -> Result<Event<'_, '_>, ParseError> {
565✔
317
        let start_pos = match *self.parser_state() {
565✔
318
            State::Number(pos) => pos,
565✔
319
            _ => return Err(crate::shared::UnexpectedState::StateMismatch.into()),
×
320
        };
321

322
        *self.parser_state_mut() = State::None;
565✔
323
        self.extract_number(start_pos, from_container_end, true)
565✔
324
    }
565✔
325

326
    /// Get the current parser state for escape context checking
327
    fn parser_state(&self) -> &State;
328

329
    /// Process Unicode escape sequence using shared collector logic
330
    fn process_unicode_escape_with_collector(&mut self) -> Result<(), ParseError>;
331

332
    /// Handle a simple escape character (after EscapeProcessor conversion)
333
    fn handle_simple_escape_char(&mut self, escape_char: u8) -> Result<(), ParseError>;
334

335
    /// Begin escape sequence processing (lifecycle method with default no-op implementation)
336
    /// Called when escape sequence processing begins (e.g., on Begin(EscapeSequence))
337
    fn begin_escape_sequence(&mut self) -> Result<(), ParseError>;
338

339
    /// Begin unicode escape sequence processing
340
    fn begin_unicode_escape(&mut self) -> Result<(), ParseError>;
341

342
    /// Process Begin events that have similar patterns between parsers
343
    fn process_begin_events(
9,994✔
344
        &mut self,
9,994✔
345
        event: &ujson::Event,
9,994✔
346
    ) -> Option<EventResult<'static, 'static>> {
9,994✔
347
        match event {
8,050✔
348
            // String/Key Begin events - nearly identical patterns
349
            ujson::Event::Begin(EventToken::Key) => {
350
                let pos = self.current_position();
1,273✔
351
                *self.parser_state_mut() = State::Key(pos);
1,273✔
352
                self.begin_string_content(pos);
1,273✔
353
                Some(EventResult::Continue)
1,273✔
354
            }
355
            ujson::Event::Begin(EventToken::String) => {
356
                let pos = self.current_position();
1,700✔
357
                *self.parser_state_mut() = State::String(pos);
1,700✔
358
                self.begin_string_content(pos);
1,700✔
359
                Some(EventResult::Continue)
1,700✔
360
            }
361

362
            // Number Begin events - identical logic
363
            ujson::Event::Begin(
364
                EventToken::Number | EventToken::NumberAndArray | EventToken::NumberAndObject,
365
            ) => {
366
                let pos = self.current_position();
1,027✔
367
                let number_start = ContentRange::number_start_from_current(pos);
1,027✔
368
                *self.parser_state_mut() = State::Number(number_start);
1,027✔
369
                Some(EventResult::Continue)
1,027✔
370
            }
371

372
            // Primitive Begin events - identical logic
373
            ujson::Event::Begin(EventToken::True | EventToken::False | EventToken::Null) => {
374
                Some(EventResult::Continue)
29✔
375
            }
376

377
            _ => None,
5,965✔
378
        }
379
    }
9,994✔
380

381
    /// Process Begin(EscapeSequence) events using the enhanced lifecycle interface
382
    fn process_begin_escape_sequence_event(&mut self) -> Result<(), ParseError> {
2,044✔
383
        // Only process if we're inside a string or key
384
        match self.parser_state() {
2,044✔
385
            State::String(_) | State::Key(_) => {
386
                self.begin_escape_sequence()?;
2,044✔
387
            }
388
            _ => {} // Ignore if not in string/key context
×
389
        }
390
        Ok(())
2,038✔
391
    }
2,044✔
392

393
    /// Process simple escape sequence events that have similar patterns between parsers
394
    fn process_simple_escape_event(&mut self, escape_token: &EventToken) -> Result<(), ParseError> {
1,291✔
395
        // Clear any pending high surrogate state when we encounter a simple escape
396
        // This ensures that interrupted surrogate pairs (like \uD801\n\uDC37) are properly rejected
397
        self.unicode_escape_collector_mut().reset_all();
1,291✔
398

399
        // Use unified escape token processing from EscapeProcessor
400
        let unescaped_char = EscapeProcessor::process_escape_token(escape_token)?;
1,291✔
401

402
        // Only process if we're inside a string or key
403
        match self.parser_state() {
1,291✔
404
            State::String(_) | State::Key(_) => {
405
                self.handle_simple_escape_char(unescaped_char)?;
1,291✔
406
            }
407
            _ => {} // Ignore if not in string/key context
×
408
        }
409

410
        Ok(())
1,277✔
411
    }
1,291✔
412

413
    /// Process Unicode escape begin/end events that have similar patterns between parsers
414
    fn process_unicode_escape_events(&mut self, event: &ujson::Event) -> Result<bool, ParseError> {
1,340✔
415
        match event {
685✔
416
            ujson::Event::Begin(EventToken::UnicodeEscape) => {
417
                // Start Unicode escape collection - reset collector for new sequence
418
                // Only handle if we're inside a string or key
419
                match self.parser_state() {
685✔
420
                    State::String(_) | State::Key(_) => {
421
                        self.unicode_escape_collector_mut().reset();
685✔
422
                        self.begin_unicode_escape()?;
685✔
423
                    }
NEW
424
                    _ => {}
×
425
                }
426
                Ok(true) // Event was handled
685✔
427
            }
428
            ujson::Event::End(EventToken::UnicodeEscape) => {
429
                // Handle end of Unicode escape sequence (\uXXXX)
430
                match self.parser_state() {
655✔
431
                    State::String(_) | State::Key(_) => {
432
                        self.process_unicode_escape_with_collector()?;
655✔
433
                    }
NEW
434
                    _ => {}
×
435
                }
436
                Ok(true) // Event was handled
631✔
437
            }
438
            _ => Ok(false), // Event was not handled
×
439
        }
440
    }
1,340✔
441
}
442

443
/// Clear event storage array - utility function
444
pub fn clear_events(event_storage: &mut [Option<ujson::Event>; 2]) {
49,056✔
445
    event_storage[0] = None;
49,056✔
446
    event_storage[1] = None;
49,056✔
447
}
49,056✔
448

449
/// Creates a standard tokenizer callback for event storage
450
///
451
/// This callback stores tokenizer events in the parser's event array, filling the first
452
/// available slot. This pattern is identical across both SliceParser and StreamParser.
453
pub fn create_tokenizer_callback(
49,581✔
454
    event_storage: &mut [Option<ujson::Event>; 2],
49,581✔
455
) -> impl FnMut(ujson::Event, usize) + '_ {
49,581✔
456
    |event, _len| {
21,113✔
457
        for evt in event_storage.iter_mut() {
23,157✔
458
            if evt.is_none() {
23,157✔
459
                *evt = Some(event);
21,112✔
460
                return;
21,112✔
461
            }
2,045✔
462
        }
463
    }
21,113✔
464
}
49,581✔
465

466
/// Shared utility to check if any events are waiting to be processed
467
pub fn have_events(event_storage: &[Option<ujson::Event>; 2]) -> bool {
124,199✔
468
    event_storage.iter().any(|evt| evt.is_some())
211,836✔
469
}
124,199✔
470

471
/// Shared utility to extract the first available event from storage
472
pub fn take_first_event(event_storage: &mut [Option<ujson::Event>; 2]) -> Option<ujson::Event> {
21,077✔
473
    event_storage.iter_mut().find_map(|e| e.take())
23,111✔
474
}
21,077✔
475

476
/// Process simple container and primitive events that are identical between parsers
477
pub fn process_simple_events(event: &ujson::Event) -> Option<EventResult<'static, 'static>> {
21,083✔
478
    match event {
5,308✔
479
        // Container events - identical processing
480
        ujson::Event::ObjectStart => Some(EventResult::Complete(Event::StartObject)),
826✔
481
        ujson::Event::ObjectEnd => Some(EventResult::Complete(Event::EndObject)),
757✔
482
        ujson::Event::ArrayStart => Some(EventResult::Complete(Event::StartArray)),
3,478✔
483
        ujson::Event::ArrayEnd => Some(EventResult::Complete(Event::EndArray)),
2,669✔
484

485
        // Primitive values - identical processing
486
        ujson::Event::End(EventToken::True) => Some(EventResult::Complete(Event::Bool(true))),
13✔
487
        ujson::Event::End(EventToken::False) => Some(EventResult::Complete(Event::Bool(false))),
7✔
488
        ujson::Event::End(EventToken::Null) => Some(EventResult::Complete(Event::Null)),
8✔
489

490
        // Content extraction triggers - identical logic
491
        ujson::Event::End(EventToken::String) => Some(EventResult::ExtractString),
1,290✔
492
        ujson::Event::End(EventToken::Key) => Some(EventResult::ExtractKey),
1,229✔
493
        ujson::Event::End(EventToken::Number) => Some(EventResult::ExtractNumber(false)),
65✔
494
        ujson::Event::End(EventToken::NumberAndArray) => Some(EventResult::ExtractNumber(true)),
348✔
495
        ujson::Event::End(EventToken::NumberAndObject) => Some(EventResult::ExtractNumber(true)),
404✔
496

497
        // All other events need parser-specific handling
498
        _ => None,
9,989✔
499
    }
500
}
21,083✔
501

502
#[cfg(test)]
503
mod tests {
504
    use super::*;
505

506
    #[test]
507
    fn test_container_events() {
1✔
508
        assert!(matches!(
1✔
509
            process_simple_events(&ujson::Event::ObjectStart),
1✔
510
            Some(EventResult::Complete(Event::StartObject))
511
        ));
512

513
        assert!(matches!(
1✔
514
            process_simple_events(&ujson::Event::ArrayEnd),
1✔
515
            Some(EventResult::Complete(Event::EndArray))
516
        ));
517
    }
1✔
518

519
    #[test]
520
    fn test_primitive_events() {
1✔
521
        assert!(matches!(
1✔
522
            process_simple_events(&ujson::Event::End(EventToken::True)),
1✔
523
            Some(EventResult::Complete(Event::Bool(true)))
524
        ));
525

526
        assert!(matches!(
1✔
527
            process_simple_events(&ujson::Event::End(EventToken::Null)),
1✔
528
            Some(EventResult::Complete(Event::Null))
529
        ));
530
    }
1✔
531

532
    #[test]
533
    fn test_extraction_triggers() {
1✔
534
        assert!(matches!(
1✔
535
            process_simple_events(&ujson::Event::End(EventToken::String)),
1✔
536
            Some(EventResult::ExtractString)
537
        ));
538

539
        assert!(matches!(
1✔
540
            process_simple_events(&ujson::Event::End(EventToken::Number)),
1✔
541
            Some(EventResult::ExtractNumber(false))
542
        ));
543

544
        assert!(matches!(
1✔
545
            process_simple_events(&ujson::Event::End(EventToken::NumberAndArray)),
1✔
546
            Some(EventResult::ExtractNumber(true))
547
        ));
548
    }
1✔
549

550
    #[test]
551
    fn test_complex_events_not_handled() {
1✔
552
        assert!(process_simple_events(&ujson::Event::Begin(EventToken::String)).is_none());
1✔
553
        assert!(process_simple_events(&ujson::Event::Begin(EventToken::EscapeQuote)).is_none());
1✔
554
    }
1✔
555

556
    // Mock ContentExtractor for testing
557
    struct MockContentExtractor {
558
        position: usize,
559
        state: State,
560
        string_begin_calls: Vec<usize>,
561
    }
562

563
    impl MockContentExtractor {
564
        fn new() -> Self {
5✔
565
            Self {
5✔
566
                position: 42,
5✔
567
                state: State::None,
5✔
568
                string_begin_calls: Vec::new(),
5✔
569
            }
5✔
570
        }
5✔
571
    }
572

573
    impl ContentExtractor for MockContentExtractor {
574
        fn next_byte(&mut self) -> Result<Option<u8>, ParseError> {
×
575
            Ok(None)
×
576
        }
×
577

578
        fn current_position(&self) -> usize {
3✔
579
            self.position
3✔
580
        }
3✔
581

582
        fn begin_string_content(&mut self, pos: usize) {
2✔
583
            self.string_begin_calls.push(pos);
2✔
584
        }
2✔
585

586
        fn parser_state_mut(&mut self) -> &mut State {
3✔
587
            &mut self.state
3✔
588
        }
3✔
589

590
        fn unicode_escape_collector_mut(&mut self) -> &mut UnicodeEscapeCollector {
×
591
            unimplemented!("Mock doesn't need unicode collector")
×
592
        }
593

594
        fn extract_string_content(
×
595
            &mut self,
×
596
            _start_pos: usize,
×
597
        ) -> Result<Event<'_, '_>, ParseError> {
×
598
            unimplemented!("Mock doesn't need extraction")
×
599
        }
600

601
        fn extract_key_content(&mut self, _start_pos: usize) -> Result<Event<'_, '_>, ParseError> {
×
602
            unimplemented!("Mock doesn't need extraction")
×
603
        }
604

605
        fn extract_number(
×
606
            &mut self,
×
607
            _start_pos: usize,
×
608
            _from_container_end: bool,
×
609
            _finished: bool,
×
610
        ) -> Result<Event<'_, '_>, ParseError> {
×
611
            unimplemented!("Mock doesn't need extraction")
×
612
        }
613

614
        fn parser_state(&self) -> &State {
×
615
            &self.state
×
616
        }
×
617

618
        fn process_unicode_escape_with_collector(&mut self) -> Result<(), ParseError> {
×
619
            Ok(())
×
620
        }
×
621

622
        fn handle_simple_escape_char(&mut self, _escape_char: u8) -> Result<(), ParseError> {
×
623
            Ok(())
×
624
        }
×
625

626
        fn begin_unicode_escape(&mut self) -> Result<(), ParseError> {
×
627
            Ok(())
×
628
        }
×
629

630
        fn begin_escape_sequence(&mut self) -> Result<(), ParseError> {
×
631
            Ok(())
×
632
        }
×
633
    }
634

635
    #[test]
636
    fn test_begin_events_key() {
1✔
637
        let mut context = MockContentExtractor::new();
1✔
638
        let event = ujson::Event::Begin(EventToken::Key);
1✔
639

640
        let result = context.process_begin_events(&event);
1✔
641

642
        assert!(matches!(result, Some(EventResult::Continue)));
1✔
643
        assert!(matches!(context.state, State::Key(42)));
1✔
644
        assert_eq!(context.string_begin_calls, vec![42]);
1✔
645
    }
1✔
646

647
    #[test]
648
    fn test_begin_events_string() {
1✔
649
        let mut context = MockContentExtractor::new();
1✔
650
        let event = ujson::Event::Begin(EventToken::String);
1✔
651

652
        let result = context.process_begin_events(&event);
1✔
653

654
        assert!(matches!(result, Some(EventResult::Continue)));
1✔
655
        assert!(matches!(context.state, State::String(42)));
1✔
656
        assert_eq!(context.string_begin_calls, vec![42]);
1✔
657
    }
1✔
658

659
    #[test]
660
    fn test_begin_events_number() {
1✔
661
        let mut context = MockContentExtractor::new();
1✔
662
        let event = ujson::Event::Begin(EventToken::Number);
1✔
663

664
        let result = context.process_begin_events(&event);
1✔
665

666
        assert!(matches!(result, Some(EventResult::Continue)));
1✔
667
        // Number should get position adjusted by ContentRange::number_start_from_current
668
        assert!(matches!(context.state, State::Number(_)));
1✔
669
        assert_eq!(context.string_begin_calls, Vec::<usize>::new()); // No string calls for numbers
1✔
670
    }
1✔
671

672
    #[test]
673
    fn test_begin_events_primitives() {
1✔
674
        let mut context = MockContentExtractor::new();
1✔
675

676
        for token in [EventToken::True, EventToken::False, EventToken::Null] {
3✔
677
            let event = ujson::Event::Begin(token);
3✔
678
            let result = context.process_begin_events(&event);
3✔
679
            assert!(matches!(result, Some(EventResult::Continue)));
3✔
680
        }
681

682
        // Should not affect state or string processing
683
        assert!(matches!(context.state, State::None));
1✔
684
        assert!(context.string_begin_calls.is_empty());
1✔
685
    }
1✔
686

687
    #[test]
688
    fn test_begin_events_not_handled() {
1✔
689
        let mut context = MockContentExtractor::new();
1✔
690
        let event = ujson::Event::Begin(EventToken::EscapeQuote);
1✔
691

692
        let result = context.process_begin_events(&event);
1✔
693

694
        assert!(result.is_none());
1✔
695
        assert!(matches!(context.state, State::None));
1✔
696
        assert!(context.string_begin_calls.is_empty());
1✔
697
    }
1✔
698

699
    #[test]
700
    fn test_tokenizer_callback() {
1✔
701
        let mut event_storage = [None, None];
1✔
702

703
        // Initially no events
704
        assert!(!have_events(&event_storage));
1✔
705

706
        {
1✔
707
            let mut callback = create_tokenizer_callback(&mut event_storage);
1✔
708

1✔
709
            // Add first event
1✔
710
            callback(ujson::Event::ObjectStart, 1);
1✔
711
        }
1✔
712
        assert!(have_events(&event_storage));
1✔
713
        assert!(event_storage[0].is_some());
1✔
714
        assert!(event_storage[1].is_none());
1✔
715

716
        {
1✔
717
            let mut callback = create_tokenizer_callback(&mut event_storage);
1✔
718
            // Add second event
1✔
719
            callback(ujson::Event::ArrayStart, 1);
1✔
720
        }
1✔
721
        assert!(event_storage[0].is_some());
1✔
722
        assert!(event_storage[1].is_some());
1✔
723

724
        {
1✔
725
            let mut callback = create_tokenizer_callback(&mut event_storage);
1✔
726
            // Storage is full, third event should be ignored (no panic)
1✔
727
            callback(ujson::Event::ObjectEnd, 1);
1✔
728
        }
1✔
729
        assert!(event_storage[0].is_some());
1✔
730
        assert!(event_storage[1].is_some());
1✔
731
    }
1✔
732

733
    #[test]
734
    fn test_event_extraction() {
1✔
735
        let mut event_storage = [
1✔
736
            Some(ujson::Event::ObjectStart),
1✔
737
            Some(ujson::Event::ArrayStart),
1✔
738
        ];
1✔
739

740
        // Extract first event
741
        let first = take_first_event(&mut event_storage);
1✔
742
        assert!(matches!(first, Some(ujson::Event::ObjectStart)));
1✔
743
        assert!(event_storage[0].is_none());
1✔
744
        assert!(event_storage[1].is_some());
1✔
745

746
        // Extract second event
747
        let second = take_first_event(&mut event_storage);
1✔
748
        assert!(matches!(second, Some(ujson::Event::ArrayStart)));
1✔
749
        assert!(event_storage[0].is_none());
1✔
750
        assert!(event_storage[1].is_none());
1✔
751

752
        // No more events
753
        let none = take_first_event(&mut event_storage);
1✔
754
        assert!(none.is_none());
1✔
755
        assert!(!have_events(&event_storage));
1✔
756
    }
1✔
757
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc