Skip to main content

kowito_json/
view.rs

1use crate::string::KString;
2
3/// A structural representation of JSON data using offsets.
4#[derive(Debug, Clone)]
5pub enum KNode<'a> {
6    Null,
7    Bool(bool),
8    Number(&'a [u8]), // Delayed number parsing
9    String(KString<'a>),
10    Array { start_idx: usize, len: usize },
11    Object { start_idx: usize, len: usize },
12}
13
14/// Takes a `&'a [u8]` and a reference to the structural index (Tape).
15/// Allows jumping directly to offsets without full deserialization.
16pub struct KView<'a> {
17    pub source: &'a [u8],
18    /// The tape is a sequence of structural elements represented as indices or tokens.
19    pub tape: &'a [u32],
20}
21
22impl<'a> KView<'a> {
23    #[inline(always)]
24    pub fn new(source: &'a [u8], tape: &'a [u32]) -> Self {
25        Self { source, tape }
26    }
27
28    // Future methods for random access querying without decoding.
29}
30
31#[cfg(test)]
32#[allow(clippy::identity_op)]
33mod tests {
34    use super::*;
35    use crate::scanner::{TOKEN_COLON, TOKEN_LBRACE, TOKEN_QUOTE, TOKEN_RBRACE};
36
37    #[test]
38    fn test_view_initialization() {
39        let json = b"{\"key\":\"value\"}";
40        let tape = vec![
41            TOKEN_LBRACE | 0,
42            TOKEN_QUOTE | 1,
43            TOKEN_QUOTE | 5,
44            TOKEN_COLON | 6,
45            TOKEN_QUOTE | 7,
46            TOKEN_QUOTE | 13,
47            TOKEN_RBRACE | 14,
48        ];
49        let view = KView::new(json, &tape);
50
51        assert_eq!(view.source.len(), 15);
52        assert_eq!(view.tape.len(), 7);
53    }
54}