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

kaidokert / picojson-rs / 16709017230

03 Aug 2025 08:15PM UTC coverage: 93.498% (-0.5%) from 94.008%
16709017230

Pull #77

github

web-flow
Merge 027f84e7a into 377ce19f7
Pull Request #77: Datasource intro

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

22 existing lines in 3 files now uncovered.

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

67.24
/picojson/src/parse_error.rs
1
// SPDX-License-Identifier: Apache-2.0
2

3
use crate::shared::UnexpectedState;
4
use crate::slice_input_buffer;
5
use crate::stream_buffer;
6

7
use crate::ujson;
8

9
/// Errors that can occur during JSON parsing
10
#[derive(Debug, PartialEq)]
11
pub enum ParseError {
12
    /// An error bubbled up from the underlying tokenizer.
13
    TokenizerError(ujson::Error),
14
    /// The scratch buffer is full.
15
    ScratchBufferFull,
16
    /// A UTF-8 error occurred.
17
    InvalidUtf8(core::str::Utf8Error),
18
    /// The input buffer is full.
19
    InputBufferFull,
20
    /// A number string could not be parsed.
21
    InvalidNumber,
22
    /// The parser entered an unexpected internal state.
23
    Unexpected(UnexpectedState),
24
    /// End of input data.
25
    EndOfData,
26
    /// Invalid hex digits in Unicode escape sequence.
27
    InvalidUnicodeHex,
28
    /// Valid hex but invalid Unicode codepoint.
29
    InvalidUnicodeCodepoint,
30
    /// Invalid escape sequence character.
31
    InvalidEscapeSequence,
32
    /// Float encountered but float support is disabled and float-error is configured
33
    FloatNotAllowed,
34
    /// Error from the underlying reader (I/O error, not end-of-stream)
35
    ReaderError,
36
    /// Numeric overflow
37
    NumericOverflow,
38
}
39

40
impl From<slice_input_buffer::Error> for ParseError {
UNCOV
41
    fn from(err: slice_input_buffer::Error) -> Self {
×
UNCOV
42
        match err {
×
43
            slice_input_buffer::Error::ReachedEnd => ParseError::EndOfData,
×
44
            slice_input_buffer::Error::InvalidSliceBounds => {
45
                UnexpectedState::InvalidSliceBounds.into()
×
46
            }
47
        }
UNCOV
48
    }
×
49
}
50

51
impl From<stream_buffer::StreamBufferError> for ParseError {
52
    fn from(err: stream_buffer::StreamBufferError) -> Self {
99✔
53
        match err {
99✔
54
            stream_buffer::StreamBufferError::BufferFull => ParseError::ScratchBufferFull,
99✔
UNCOV
55
            stream_buffer::StreamBufferError::EndOfData => ParseError::EndOfData,
×
56
            stream_buffer::StreamBufferError::Unexpected => {
57
                ParseError::Unexpected(UnexpectedState::BufferCapacityExceeded)
×
58
            }
59
            stream_buffer::StreamBufferError::InvalidSliceBounds => {
UNCOV
60
                ParseError::Unexpected(UnexpectedState::InvalidSliceBounds)
×
61
            }
62
        }
63
    }
99✔
64
}
65

66
impl From<core::str::Utf8Error> for ParseError {
67
    fn from(err: core::str::Utf8Error) -> Self {
2✔
68
        ParseError::InvalidUtf8(err)
2✔
69
    }
2✔
70
}
71

72
impl From<UnexpectedState> for ParseError {
73
    fn from(info: UnexpectedState) -> Self {
5✔
74
        ParseError::Unexpected(info)
5✔
75
    }
5✔
76
}
77

78
impl From<ujson::Error> for ParseError {
79
    fn from(err: ujson::Error) -> Self {
10✔
80
        ParseError::TokenizerError(err)
10✔
81
    }
10✔
82
}
83

84
impl core::fmt::Display for ParseError {
UNCOV
85
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
×
UNCOV
86
        match self {
×
UNCOV
87
            ParseError::TokenizerError(e) => write!(f, "{e}"),
×
UNCOV
88
            ParseError::InvalidUtf8(e) => write!(f, "Invalid UTF-8: {e}"),
×
UNCOV
89
            _ => write!(f, "{self:?}"),
×
90
        }
UNCOV
91
    }
×
92
}
93

94
#[cfg(test)]
95
mod tests {
96
    use super::*;
97

98
    #[test]
99
    fn test_error_constructors() {
1✔
100
        // Test state_mismatch error constructor
101
        let error: ParseError = UnexpectedState::StateMismatch.into();
1✔
102
        match error {
1✔
103
            ParseError::Unexpected(info) => {
1✔
104
                assert_eq!(info, UnexpectedState::StateMismatch);
1✔
105
            }
UNCOV
106
            _ => panic!("Expected UnexpectedState error"),
×
107
        }
108

109
        // Test invalid_unicode_length error constructor
110
        let error: ParseError = UnexpectedState::InvalidUnicodeEscape.into();
1✔
111
        match error {
1✔
112
            ParseError::Unexpected(info) => {
1✔
113
                assert_eq!(info, UnexpectedState::InvalidUnicodeEscape);
1✔
114
            }
UNCOV
115
            _ => panic!("Expected UnexpectedState error"),
×
116
        }
117

118
        // Test incomplete_unicode_escape error constructor
119
        let error: ParseError = UnexpectedState::InvalidUnicodeEscape.into();
1✔
120
        match error {
1✔
121
            ParseError::Unexpected(info) => {
1✔
122
                assert_eq!(info, UnexpectedState::InvalidUnicodeEscape);
1✔
123
            }
UNCOV
124
            _ => panic!("Expected UnexpectedState error"),
×
125
        }
126
    }
1✔
127

128
    #[test]
129
    fn test_utf8_error_conversion() {
1✔
130
        // Test From<Utf8Error> trait implementation
131
        use core::str;
132
        // Create a proper invalid UTF-8 sequence (lone continuation byte) dynamically
133
        // to avoid compile-time warning about static invalid UTF-8 literals
134
        let mut invalid_utf8_array = [0u8; 1];
1✔
135
        invalid_utf8_array[0] = 0b10000000u8; // Invalid UTF-8 - continuation byte without start
1✔
136
        let invalid_utf8 = &invalid_utf8_array;
1✔
137

138
        match str::from_utf8(invalid_utf8) {
1✔
139
            Err(utf8_error) => {
1✔
140
                let parse_error: ParseError = utf8_error.into();
1✔
141
                match parse_error {
1✔
142
                    ParseError::InvalidUtf8(_) => {
1✔
143
                        // Expected - conversion works correctly
1✔
144
                    }
1✔
UNCOV
145
                    _ => panic!("Expected InvalidUtf8 error"),
×
146
                }
147
            }
UNCOV
148
            Ok(_) => panic!("Expected UTF-8 validation to fail"),
×
149
        }
150
    }
1✔
151
}
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