proa_const_json 0.1.2

Const-evaluable JSON serialization and parsing with derive support.
Documentation
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
// Copyright Proa Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

//! Const-compatible JSON parsing utilities.
//!
//! This module provides `const fn` JSON parsing for simple use cases at compile time.

/// A JSON value that can be parsed at compile time.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum JsonValue<'a> {
    Null,
    Bool(bool),
    Number(i64),
    String(&'a str),
    Object(JsonObject<'a>),
    Array(JsonArray<'a>),
}

/// A JSON object representation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct JsonObject<'a> {
    json: &'a str,
}

/// A JSON array representation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct JsonArray<'a> {
    json: &'a str,
}

impl<'a> JsonObject<'a> {
    /// Get a field from the JSON object by key.
    pub const fn get(&self, key: &str) -> Option<JsonValue<'a>> {
        get_field(self.json, key)
    }
}

impl<'a> JsonArray<'a> {
    /// Get an element from the JSON array by index.
    pub const fn get(&self, _index: usize) -> Option<JsonValue<'a>> {
        // TODO: Implement array indexing
        None
    }
}

impl<'a> JsonValue<'a> {
    /// Parse a JSON value from a string at compile time.
    pub const fn parse(json: &'a str) -> Result<Self, ParseError> {
        let json = skip_whitespace(json);
        parse_value(json)
    }

    /// If this value is a string, return it.
    pub const fn as_str(&self) -> Option<&'a str> {
        match self {
            JsonValue::String(s) => Some(s),
            _ => None,
        }
    }

    /// If this value is an object, return it.
    pub const fn as_object(&self) -> Option<JsonObject<'a>> {
        match self {
            JsonValue::Object(obj) => Some(*obj),
            _ => None,
        }
    }

    /// If this value is a number, return it.
    pub const fn as_number(&self) -> Option<i64> {
        match self {
            JsonValue::Number(n) => Some(*n),
            _ => None,
        }
    }

    /// If this value is a bool, return it.
    pub const fn as_bool(&self) -> Option<bool> {
        match self {
            JsonValue::Bool(b) => Some(*b),
            _ => None,
        }
    }
}

/// Error type for const JSON parsing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ParseError {
    UnexpectedEndOfInput,
    InvalidSyntax,
    UnsupportedValue,
}

const fn skip_whitespace(json: &str) -> &str {
    let bytes = json.as_bytes();
    let mut i = 0;
    while i < bytes.len() {
        match bytes[i] {
            b' ' | b'\t' | b'\n' | b'\r' => i += 1,
            _ => break,
        }
    }
    unsafe_slice_str(bytes, i, bytes.len())
}

const fn parse_value(json: &str) -> Result<JsonValue<'_>, ParseError> {
    let bytes = json.as_bytes();
    if bytes.is_empty() {
        return Err(ParseError::UnexpectedEndOfInput);
    }

    match bytes[0] {
        b'{' => parse_object(json),
        b'[' => parse_array(json),
        b'"' => parse_string(json),
        b't' | b'f' => parse_bool(json),
        b'n' => parse_null(json),
        b'-' | b'0'..=b'9' => parse_number(json),
        _ => Err(ParseError::InvalidSyntax),
    }
}

const fn parse_object(json: &str) -> Result<JsonValue<'_>, ParseError> {
    // For now, just store the raw JSON string
    // We'll parse fields on-demand when get() is called
    let bytes = json.as_bytes();
    if bytes.is_empty() || bytes[0] != b'{' {
        return Err(ParseError::InvalidSyntax);
    }

    // Find matching closing brace
    let mut depth = 0;
    let mut i = 0;
    let mut in_string = false;
    let mut escape_next = false;

    while i < bytes.len() {
        if escape_next {
            escape_next = false;
            i += 1;
            continue;
        }

        match bytes[i] {
            b'\\' if in_string => escape_next = true,
            b'"' => in_string = !in_string,
            b'{' if !in_string => depth += 1,
            b'}' if !in_string => {
                depth -= 1;
                if depth == 0 {
                    let obj_str = unsafe_slice_str(bytes, 0, i + 1);
                    return Ok(JsonValue::Object(JsonObject { json: obj_str }));
                }
            }
            _ => {}
        }
        i += 1;
    }

    Err(ParseError::UnexpectedEndOfInput)
}

const fn parse_array(json: &str) -> Result<JsonValue<'_>, ParseError> {
    let bytes = json.as_bytes();
    if bytes.is_empty() || bytes[0] != b'[' {
        return Err(ParseError::InvalidSyntax);
    }

    // Find matching closing bracket
    let mut depth = 0;
    let mut i = 0;
    let mut in_string = false;
    let mut escape_next = false;

    while i < bytes.len() {
        if escape_next {
            escape_next = false;
            i += 1;
            continue;
        }

        match bytes[i] {
            b'\\' if in_string => escape_next = true,
            b'"' => in_string = !in_string,
            b'[' if !in_string => depth += 1,
            b']' if !in_string => {
                depth -= 1;
                if depth == 0 {
                    let arr_str = unsafe_slice_str(bytes, 0, i + 1);
                    return Ok(JsonValue::Array(JsonArray { json: arr_str }));
                }
            }
            _ => {}
        }
        i += 1;
    }

    Err(ParseError::UnexpectedEndOfInput)
}

const fn parse_string(json: &str) -> Result<JsonValue<'_>, ParseError> {
    let bytes = json.as_bytes();
    if bytes.is_empty() || bytes[0] != b'"' {
        return Err(ParseError::InvalidSyntax);
    }

    let mut i = 1;
    let mut escape_next = false;

    while i < bytes.len() {
        if escape_next {
            escape_next = false;
            i += 1;
            continue;
        }

        match bytes[i] {
            b'\\' => escape_next = true,
            b'"' => {
                // Found closing quote
                let str_content = unsafe_slice_str(bytes, 1, i);
                return Ok(JsonValue::String(str_content));
            }
            _ => {}
        }
        i += 1;
    }

    Err(ParseError::UnexpectedEndOfInput)
}

const fn parse_bool(json: &str) -> Result<JsonValue<'_>, ParseError> {
    let bytes = json.as_bytes();
    if bytes.len() >= 4
        && bytes[0] == b't'
        && bytes[1] == b'r'
        && bytes[2] == b'u'
        && bytes[3] == b'e'
    {
        Ok(JsonValue::Bool(true))
    } else if bytes.len() >= 5
        && bytes[0] == b'f'
        && bytes[1] == b'a'
        && bytes[2] == b'l'
        && bytes[3] == b's'
        && bytes[4] == b'e'
    {
        Ok(JsonValue::Bool(false))
    } else {
        Err(ParseError::InvalidSyntax)
    }
}

const fn parse_null(json: &str) -> Result<JsonValue<'_>, ParseError> {
    let bytes = json.as_bytes();
    if bytes.len() >= 4
        && bytes[0] == b'n'
        && bytes[1] == b'u'
        && bytes[2] == b'l'
        && bytes[3] == b'l'
    {
        Ok(JsonValue::Null)
    } else {
        Err(ParseError::InvalidSyntax)
    }
}

const fn parse_number(json: &str) -> Result<JsonValue<'_>, ParseError> {
    let bytes = json.as_bytes();
    if bytes.is_empty() {
        return Err(ParseError::UnexpectedEndOfInput);
    }

    let mut i = 0;
    let negative = if bytes[0] == b'-' {
        i += 1;
        true
    } else {
        false
    };

    let mut value: i64 = 0;
    let mut found_digit = false;

    while i < bytes.len() {
        match bytes[i] {
            b'0'..=b'9' => {
                let digit = (bytes[i] - b'0') as i64;
                // Use checked arithmetic to avoid silent overflow in release mode.
                value = match value.checked_mul(10) {
                    Some(v) => v,
                    None => return Err(ParseError::InvalidSyntax),
                };
                value = match value.checked_add(digit) {
                    Some(v) => v,
                    None => return Err(ParseError::InvalidSyntax),
                };
                found_digit = true;
                i += 1;
            }
            _ => break,
        }
    }

    if !found_digit {
        return Err(ParseError::InvalidSyntax);
    }

    if negative {
        value = match value.checked_neg() {
            Some(v) => v,
            None => return Err(ParseError::InvalidSyntax),
        };
    }

    Ok(JsonValue::Number(value))
}

/// Get a field from a JSON object string.
const fn get_field<'a>(json: &'a str, key: &str) -> Option<JsonValue<'a>> {
    let bytes = json.as_bytes();

    if bytes.is_empty() || bytes[0] != b'{' {
        return None;
    }

    let mut i = 1; // Skip opening brace
    let mut escape_next = false;

    while i < bytes.len() {
        // Skip whitespace
        while i < bytes.len() && matches!(bytes[i], b' ' | b'\t' | b'\n' | b'\r') {
            i += 1;
        }

        if i >= bytes.len() || bytes[i] == b'}' {
            return None;
        }

        // Expect a string key
        if bytes[i] != b'"' {
            return None;
        }

        i += 1; // Skip opening quote
        let key_start = i;

        // Find end of key
        while i < bytes.len() {
            if escape_next {
                escape_next = false;
                i += 1;
                continue;
            }

            match bytes[i] {
                b'\\' => escape_next = true,
                b'"' => break,
                _ => {}
            }
            i += 1;
        }

        let key_end = i;
        i += 1; // Skip closing quote

        // Check if this key matches
        let current_key = unsafe_slice_str(bytes, key_start, key_end);
        let matches = str_eq(current_key, key);

        // Skip whitespace and colon
        while i < bytes.len() && matches!(bytes[i], b' ' | b'\t' | b'\n' | b'\r') {
            i += 1;
        }

        if i >= bytes.len() || bytes[i] != b':' {
            return None;
        }

        i += 1; // Skip colon

        // Skip whitespace before value
        while i < bytes.len() && matches!(bytes[i], b' ' | b'\t' | b'\n' | b'\r') {
            i += 1;
        }

        if matches {
            // Found our key! Parse the value
            let value_json = unsafe_slice_str(bytes, i, bytes.len());
            return match parse_value(value_json) {
                Ok(val) => Some(val),
                Err(_) => None,
            };
        }

        // Skip this value
        i = match skip_value(bytes, i) {
            Some(next) => next,
            None => return None,
        };

        // Skip whitespace and comma
        while i < bytes.len() && matches!(bytes[i], b' ' | b'\t' | b'\n' | b'\r') {
            i += 1;
        }

        if i < bytes.len() && bytes[i] == b',' {
            i += 1;
        }
    }

    None
}

/// Skip over a JSON value and return the index after it.
const fn skip_value(bytes: &[u8], mut i: usize) -> Option<usize> {
    if i >= bytes.len() {
        return None;
    }

    match bytes[i] {
        b'"' => {
            i += 1;
            let mut escape_next = false;
            while i < bytes.len() {
                if escape_next {
                    escape_next = false;
                    i += 1;
                    continue;
                }
                match bytes[i] {
                    b'\\' => escape_next = true,
                    b'"' => return Some(i + 1),
                    _ => {}
                }
                i += 1;
            }
            None
        }
        b'{' => {
            let mut depth = 0;
            let mut in_string = false;
            let mut escape_next = false;

            while i < bytes.len() {
                if escape_next {
                    escape_next = false;
                    i += 1;
                    continue;
                }

                match bytes[i] {
                    b'\\' if in_string => escape_next = true,
                    b'"' => in_string = !in_string,
                    b'{' if !in_string => depth += 1,
                    b'}' if !in_string => {
                        depth -= 1;
                        if depth == 0 {
                            return Some(i + 1);
                        }
                    }
                    _ => {}
                }
                i += 1;
            }
            None
        }
        b'[' => {
            let mut depth = 0;
            let mut in_string = false;
            let mut escape_next = false;

            while i < bytes.len() {
                if escape_next {
                    escape_next = false;
                    i += 1;
                    continue;
                }

                match bytes[i] {
                    b'\\' if in_string => escape_next = true,
                    b'"' => in_string = !in_string,
                    b'[' if !in_string => depth += 1,
                    b']' if !in_string => {
                        depth -= 1;
                        if depth == 0 {
                            return Some(i + 1);
                        }
                    }
                    _ => {}
                }
                i += 1;
            }
            None
        }
        b't' => {
            if i + 4 <= bytes.len()
                && bytes[i] == b't'
                && bytes[i + 1] == b'r'
                && bytes[i + 2] == b'u'
                && bytes[i + 3] == b'e'
            {
                Some(i + 4)
            } else {
                None
            }
        }
        b'f' => {
            if i + 5 <= bytes.len()
                && bytes[i] == b'f'
                && bytes[i + 1] == b'a'
                && bytes[i + 2] == b'l'
                && bytes[i + 3] == b's'
                && bytes[i + 4] == b'e'
            {
                Some(i + 5)
            } else {
                None
            }
        }
        b'n' => {
            if i + 4 <= bytes.len()
                && bytes[i] == b'n'
                && bytes[i + 1] == b'u'
                && bytes[i + 2] == b'l'
                && bytes[i + 3] == b'l'
            {
                Some(i + 4)
            } else {
                None
            }
        }
        b'-' | b'0'..=b'9' => {
            while i < bytes.len()
                && matches!(bytes[i], b'-' | b'0'..=b'9' | b'.' | b'e' | b'E' | b'+')
            {
                i += 1;
            }
            Some(i)
        }
        _ => None,
    }
}

/// Compare two strings for equality.
const fn str_eq(a: &str, b: &str) -> bool {
    let a_bytes = a.as_bytes();
    let b_bytes = b.as_bytes();

    if a_bytes.len() != b_bytes.len() {
        return false;
    }

    let mut i = 0;
    while i < a_bytes.len() {
        if a_bytes[i] != b_bytes[i] {
            return false;
        }
        i += 1;
    }

    true
}

/// Create a string slice from a byte slice (unsafe because we can't use range indexing in const).
const fn unsafe_slice_str(bytes: &[u8], start: usize, end: usize) -> &str {
    let len = end - start;
    let ptr = bytes.as_ptr();
    unsafe {
        let slice = core::slice::from_raw_parts(ptr.add(start), len);
        core::str::from_utf8_unchecked(slice)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_string() {
        const JSON: &str = r#""hello""#;
        const VALUE: Result<JsonValue, ParseError> = JsonValue::parse(JSON);
        assert!(matches!(VALUE, Ok(JsonValue::String("hello"))));
    }

    #[test]
    fn test_parse_number() {
        const JSON: &str = "42";
        const VALUE: Result<JsonValue, ParseError> = JsonValue::parse(JSON);
        assert!(matches!(VALUE, Ok(JsonValue::Number(42))));
    }

    #[test]
    fn test_parse_bool() {
        const JSON_TRUE: &str = "true";
        const JSON_FALSE: &str = "false";
        const VALUE_TRUE: Result<JsonValue, ParseError> = JsonValue::parse(JSON_TRUE);
        const VALUE_FALSE: Result<JsonValue, ParseError> = JsonValue::parse(JSON_FALSE);
        assert!(matches!(VALUE_TRUE, Ok(JsonValue::Bool(true))));
        assert!(matches!(VALUE_FALSE, Ok(JsonValue::Bool(false))));
    }

    #[test]
    fn test_parse_object_get_field() {
        const JSON: &str = r#"{"react": "^19.0.0", "react-dom": "^19.0.0"}"#;
        const VALUE: Result<JsonValue, ParseError> = JsonValue::parse(JSON);

        match VALUE {
            Ok(JsonValue::Object(obj)) => {
                let react_version = obj.get("react");
                match react_version {
                    Some(JsonValue::String(v)) => assert_eq!(v, "^19.0.0"),
                    _ => panic!("Expected string value"),
                }
            }
            _ => panic!("Expected object"),
        }
    }

    #[test]
    fn test_nested_object() {
        const JSON: &str = r#"{"dependencies": {"react": "^19.0.0"}}"#;
        const VALUE: Result<JsonValue, ParseError> = JsonValue::parse(JSON);

        match VALUE {
            Ok(JsonValue::Object(obj)) => {
                let deps = obj.get("dependencies");
                match deps {
                    Some(JsonValue::Object(deps_obj)) => {
                        let react = deps_obj.get("react");
                        match react {
                            Some(JsonValue::String(v)) => assert_eq!(v, "^19.0.0"),
                            _ => panic!("Expected string value"),
                        }
                    }
                    _ => panic!("Expected nested object"),
                }
            }
            _ => panic!("Expected object"),
        }
    }
}