determs 0.1.0

Deterministic replay & proof engine for verifiable automated decisions — capture, verify, and replay records anyone can check. AI agents are the first profile.
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
use crate::value::Value;
use std::collections::BTreeMap;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct JsonError {
    pub message: String,
    pub position: usize,
}

impl JsonError {
    fn new(message: impl Into<String>, position: usize) -> Self {
        Self {
            message: message.into(),
            position,
        }
    }
}

impl core::fmt::Display for JsonError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "JSON error at byte {}: {}", self.position, self.message)
    }
}

pub fn parse(input: &str) -> Result<Value, JsonError> {
    let mut parser = Parser::new(input);
    let value = parser.parse_value()?;
    parser.skip_whitespace();
    if parser.peek().is_some() {
        return Err(JsonError::new(
            "Unexpected trailing characters",
            parser.position,
        ));
    }
    Ok(value)
}

pub fn to_canonical_string(value: &Value) -> String {
    let mut output = String::new();
    write_canonical(value, &mut output);
    output
}

pub fn to_pretty_string(value: &Value) -> String {
    let mut output = String::new();
    write_pretty(value, 0, &mut output);
    output
}

struct Parser<'a> {
    input: &'a str,
    bytes: &'a [u8],
    position: usize,
}

impl<'a> Parser<'a> {
    fn new(input: &'a str) -> Self {
        Self {
            input,
            bytes: input.as_bytes(),
            position: 0,
        }
    }

    fn peek(&self) -> Option<u8> {
        self.bytes.get(self.position).copied()
    }

    fn bump(&mut self) -> Option<u8> {
        let byte = self.peek()?;
        self.position += 1;
        Some(byte)
    }

    fn skip_whitespace(&mut self) {
        while let Some(byte) = self.peek() {
            if matches!(byte, b' ' | b'\n' | b'\r' | b'\t') {
                self.position += 1;
            } else {
                break;
            }
        }
    }

    fn parse_value(&mut self) -> Result<Value, JsonError> {
        self.skip_whitespace();
        match self.peek() {
            Some(b'n') => self.parse_null(),
            Some(b't') => self.parse_true(),
            Some(b'f') => self.parse_false(),
            Some(b'"') => self.parse_string().map(Value::String),
            Some(b'-' | b'0'..=b'9') => self.parse_number().map(Value::Number),
            Some(b'[') => self.parse_array(),
            Some(b'{') => self.parse_object(),
            Some(_) => Err(JsonError::new("Unexpected token", self.position)),
            None => Err(JsonError::new("Unexpected end of input", self.position)),
        }
    }

    fn parse_null(&mut self) -> Result<Value, JsonError> {
        self.expect_literal("null")?;
        Ok(Value::Null)
    }

    fn parse_true(&mut self) -> Result<Value, JsonError> {
        self.expect_literal("true")?;
        Ok(Value::Bool(true))
    }

    fn parse_false(&mut self) -> Result<Value, JsonError> {
        self.expect_literal("false")?;
        Ok(Value::Bool(false))
    }

    fn expect_literal(&mut self, literal: &str) -> Result<(), JsonError> {
        let start = self.position;
        for expected in literal.as_bytes() {
            match self.bump() {
                Some(byte) if byte == *expected => {}
                _ => return Err(JsonError::new(format!("Expected '{}'", literal), start)),
            }
        }
        Ok(())
    }

    fn parse_string(&mut self) -> Result<String, JsonError> {
        if self.bump() != Some(b'"') {
            return Err(JsonError::new("Expected string", self.position));
        }

        let mut output = String::new();
        while let Some(byte) = self.bump() {
            match byte {
                b'"' => return Ok(output),
                b'\\' => {
                    let escaped = self.bump().ok_or_else(|| {
                        JsonError::new("Unexpected end of input in escape sequence", self.position)
                    })?;
                    match escaped {
                        b'"' => output.push('"'),
                        b'\\' => output.push('\\'),
                        b'/' => output.push('/'),
                        b'b' => output.push('\u{0008}'),
                        b'f' => output.push('\u{000C}'),
                        b'n' => output.push('\n'),
                        b'r' => output.push('\r'),
                        b't' => output.push('\t'),
                        b'u' => {
                            let codepoint = self.parse_unicode_escape()?;
                            let character = char::from_u32(codepoint as u32).ok_or_else(|| {
                                JsonError::new("Invalid unicode escape", self.position)
                            })?;
                            output.push(character);
                        }
                        _ => {
                            return Err(JsonError::new(
                                "Unsupported escape sequence",
                                self.position.saturating_sub(1),
                            ));
                        }
                    }
                }
                0x00..=0x1F => {
                    return Err(JsonError::new(
                        "Control characters are not allowed in JSON strings",
                        self.position.saturating_sub(1),
                    ));
                }
                _ => output.push(byte as char),
            }
        }

        Err(JsonError::new("Unterminated string", self.position))
    }

    fn parse_unicode_escape(&mut self) -> Result<u16, JsonError> {
        let start = self.position;
        let slice = self
            .input
            .get(self.position..self.position + 4)
            .ok_or_else(|| JsonError::new("Incomplete unicode escape", start))?;
        self.position += 4;
        u16::from_str_radix(slice, 16).map_err(|_| JsonError::new("Invalid unicode escape", start))
    }

    fn parse_number(&mut self) -> Result<f64, JsonError> {
        let start = self.position;

        if self.peek() == Some(b'-') {
            self.position += 1;
        }

        match self.peek() {
            Some(b'0') => self.position += 1,
            Some(b'1'..=b'9') => {
                self.position += 1;
                while matches!(self.peek(), Some(b'0'..=b'9')) {
                    self.position += 1;
                }
            }
            _ => return Err(JsonError::new("Invalid number", start)),
        }

        if self.peek() == Some(b'.') {
            self.position += 1;
            if !matches!(self.peek(), Some(b'0'..=b'9')) {
                return Err(JsonError::new("Invalid decimal number", self.position));
            }
            while matches!(self.peek(), Some(b'0'..=b'9')) {
                self.position += 1;
            }
        }

        if matches!(self.peek(), Some(b'e' | b'E')) {
            self.position += 1;
            if matches!(self.peek(), Some(b'+' | b'-')) {
                self.position += 1;
            }
            if !matches!(self.peek(), Some(b'0'..=b'9')) {
                return Err(JsonError::new("Invalid exponent", self.position));
            }
            while matches!(self.peek(), Some(b'0'..=b'9')) {
                self.position += 1;
            }
        }

        self.input[start..self.position]
            .parse::<f64>()
            .map_err(|_| JsonError::new("Invalid number", start))
    }

    fn parse_array(&mut self) -> Result<Value, JsonError> {
        self.bump();
        self.skip_whitespace();

        let mut values = Vec::new();
        if self.peek() == Some(b']') {
            self.bump();
            return Ok(Value::Array(values));
        }

        loop {
            values.push(self.parse_value()?);
            self.skip_whitespace();
            match self.bump() {
                Some(b',') => {
                    self.skip_whitespace();
                }
                Some(b']') => break,
                _ => return Err(JsonError::new("Expected ',' or ']'", self.position)),
            }
        }

        Ok(Value::Array(values))
    }

    fn parse_object(&mut self) -> Result<Value, JsonError> {
        self.bump();
        self.skip_whitespace();

        let mut values = BTreeMap::new();
        if self.peek() == Some(b'}') {
            self.bump();
            return Ok(Value::Object(values));
        }

        loop {
            let key = self.parse_string()?;
            self.skip_whitespace();
            if self.bump() != Some(b':') {
                return Err(JsonError::new("Expected ':'", self.position));
            }
            self.skip_whitespace();
            let value = self.parse_value()?;
            values.insert(key, value);
            self.skip_whitespace();
            match self.bump() {
                Some(b',') => {
                    self.skip_whitespace();
                }
                Some(b'}') => break,
                _ => return Err(JsonError::new("Expected ',' or '}'", self.position)),
            }
        }

        Ok(Value::Object(values))
    }
}

fn write_canonical(value: &Value, output: &mut String) {
    match value {
        Value::Null => output.push_str("null"),
        Value::Bool(true) => output.push_str("true"),
        Value::Bool(false) => output.push_str("false"),
        Value::Number(number) => output.push_str(&number_to_string(*number)),
        Value::String(value) => write_string(value, output),
        Value::Array(values) => {
            output.push('[');
            for (index, value) in values.iter().enumerate() {
                if index > 0 {
                    output.push(',');
                }
                write_canonical(value, output);
            }
            output.push(']');
        }
        Value::Object(values) => {
            output.push('{');
            for (index, (key, value)) in values.iter().enumerate() {
                if index > 0 {
                    output.push(',');
                }
                write_string(key, output);
                output.push(':');
                write_canonical(value, output);
            }
            output.push('}');
        }
    }
}

fn write_pretty(value: &Value, depth: usize, output: &mut String) {
    match value {
        Value::Null | Value::Bool(_) | Value::Number(_) | Value::String(_) => {
            write_canonical(value, output);
        }
        Value::Array(values) => {
            if values.is_empty() {
                output.push_str("[]");
                return;
            }
            output.push_str("[\n");
            for (index, value) in values.iter().enumerate() {
                indent(depth + 1, output);
                write_pretty(value, depth + 1, output);
                if index + 1 != values.len() {
                    output.push(',');
                }
                output.push('\n');
            }
            indent(depth, output);
            output.push(']');
        }
        Value::Object(values) => {
            if values.is_empty() {
                output.push_str("{}");
                return;
            }
            output.push_str("{\n");
            let len = values.len();
            for (index, (key, value)) in values.iter().enumerate() {
                indent(depth + 1, output);
                write_string(key, output);
                output.push_str(": ");
                write_pretty(value, depth + 1, output);
                if index + 1 != len {
                    output.push(',');
                }
                output.push('\n');
            }
            indent(depth, output);
            output.push('}');
        }
    }
}

fn indent(depth: usize, output: &mut String) {
    for _ in 0..depth {
        output.push_str("  ");
    }
}

fn write_string(value: &str, output: &mut String) {
    output.push('"');
    for character in value.chars() {
        match character {
            '"' => output.push_str("\\\""),
            '\\' => output.push_str("\\\\"),
            '\n' => output.push_str("\\n"),
            '\r' => output.push_str("\\r"),
            '\t' => output.push_str("\\t"),
            '\u{0008}' => output.push_str("\\b"),
            '\u{000C}' => output.push_str("\\f"),
            character if character <= '\u{001F}' => {
                output.push_str(&format!("\\u{:04x}", character as u32));
            }
            character => output.push(character),
        }
    }
    output.push('"');
}

fn number_to_string(value: f64) -> String {
    if value.is_nan() || value.is_infinite() {
        return "null".to_string();
    }
    if value.fract() == 0.0 {
        format!("{:.0}", value)
    } else {
        let mut text = format!("{value}");
        if text.contains('E') {
            text = text.replace('E', "e");
        }
        text
    }
}

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

    #[test]
    fn test_parse_roundtrip() {
        let input = r#"{"a":1,"b":[true,false,null],"c":"ok"}"#;
        let value = parse(input).unwrap();
        assert_eq!(to_canonical_string(&value), input);
    }

    #[test]
    fn test_parse_string_escape() {
        let input = r#""line\nnext""#;
        let value = parse(input).unwrap();
        assert_eq!(value.as_str(), Some("line\nnext"));
    }

    #[test]
    fn test_pretty_print_contains_newlines() {
        let value = parse(r#"{"z":2,"a":[1,2]}"#).unwrap();
        let pretty = to_pretty_string(&value);
        assert!(pretty.contains('\n'));
    }
}