interplane.Get Omega
Read in your language

interplane-mv

INTERPLANE: open interoperability protocol between AI models and agent runtimes. Models express intent. Runtimes retain authority.

Brought by drakestapleton

branch main Branches Tags History

interplane-mv / provenance / src / strict.rs

91 lines · 3190 bytes Raw Blame History
1//! Strict JSON for retained bytes: exactly one value, and no object repeats a key at any depth.
2//! A last-wins reader (serde_json's `Value`) and a first-wins reader could see different values
3//! in the same bytes, so a duplicate key makes the record unparseable here (#76 review).
4
5use serde::de::{self, Deserialize, Deserializer, MapAccess, SeqAccess, Visitor};
6use serde_json::{Map, Number, Value};
7use std::fmt;
8
9struct Strict(Value);
10
11impl<'de> Deserialize<'de> for Strict {
12 fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
13 d.deserialize_any(StrictVisitor).map(Strict)
14 }
15}
16
17struct StrictVisitor;
18
19impl<'de> Visitor<'de> for StrictVisitor {
20 type Value = Value;
21
22 fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
23 f.write_str("a JSON value without duplicate object keys")
24 }
25 fn visit_unit<E>(self) -> Result<Value, E> {
26 Ok(Value::Null)
27 }
28 fn visit_bool<E>(self, b: bool) -> Result<Value, E> {
29 Ok(Value::Bool(b))
30 }
31 fn visit_i64<E>(self, n: i64) -> Result<Value, E> {
32 Ok(n.into())
33 }
34 fn visit_u64<E>(self, n: u64) -> Result<Value, E> {
35 Ok(n.into())
36 }
37 fn visit_f64<E: de::Error>(self, n: f64) -> Result<Value, E> {
38 Number::from_f64(n)
39 .map(Value::Number)
40 .ok_or_else(|| E::custom("non-finite number"))
41 }
42 fn visit_str<E>(self, s: &str) -> Result<Value, E> {
43 Ok(Value::String(s.to_string()))
44 }
45 fn visit_string<E>(self, s: String) -> Result<Value, E> {
46 Ok(Value::String(s))
47 }
48 fn visit_seq<A: SeqAccess<'de>>(self, mut a: A) -> Result<Value, A::Error> {
49 let mut out = Vec::new();
50 while let Some(Strict(v)) = a.next_element()? {
51 out.push(v);
52 }
53 Ok(Value::Array(out))
54 }
55 fn visit_map<A: MapAccess<'de>>(self, mut a: A) -> Result<Value, A::Error> {
56 let mut out = Map::new();
57 while let Some(k) = a.next_key::<String>()? {
58 let Strict(v) = a.next_value()?;
59 if out.contains_key(&k) {
60 return Err(de::Error::custom(format!("duplicate key {k:?}")));
61 }
62 out.insert(k, v);
63 }
64 Ok(Value::Object(out))
65 }
66}
67
68/// Parse `b` as one JSON value; None on any syntax error, trailing data or duplicate key.
69pub fn parse(b: &[u8]) -> Option<Value> {
70 serde_json::from_slice::<Strict>(b).ok().map(|s| s.0)
71}
72
73#[cfg(test)]
74mod tests {
75 use super::parse;
76
77 #[test]
78 fn duplicate_keys_are_refused_at_any_depth() {
79 assert!(parse(br#"{"a":1,"b":[{"c":true}]}"#).is_some());
80 assert!(parse(br#"{"a":1,"a":2}"#).is_none());
81 assert!(parse(br#"{"x":{"a":1,"a":1}}"#).is_none());
82 assert!(parse(br#"[{"a":1},{"b":{"c":[{"d":1,"d":2}]}}]"#).is_none());
83 // An escaped spelling of the same key is the same key.
84 assert!(parse(br#"{"a":1,"a":2}"#).is_none());
85 assert!(parse(br#"{"a":1} {}"#).is_none());
86 assert_eq!(
87 parse(br#"{"n":-3,"u":18446744073709551615,"f":0.5,"z":null}"#).unwrap(),
88 serde_json::json!({"n":-3,"u":18446744073709551615u64,"f":0.5,"z":null})
89 );
90 }
91}