Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/interplane-mv/blob/main/provenance/src/strict.rs # interplane-mv INTERPLANE: open interoperability protocol between AI models and agent runtimes. Models express intent. Runtimes retain authority. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers 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 5 use serde::de::{self, Deserialize, Deserializer, MapAccess, SeqAccess, Visitor}; 6 use serde_json::{Map, Number, Value}; 7 use std::fmt; 8 9 struct Strict(Value); 10 11 impl<'de> Deserialize<'de> for Strict { 12 fn deserialize>(d: D) -> Result { 13 d.deserialize_any(StrictVisitor).map(Strict) 14 } 15 } 16 17 struct StrictVisitor; 18 19 impl<'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(self) -> Result { 26 Ok(Value::Null) 27 } 28 fn visit_bool(self, b: bool) -> Result { 29 Ok(Value::Bool(b)) 30 } 31 fn visit_i64(self, n: i64) -> Result { 32 Ok(n.into()) 33 } 34 fn visit_u64(self, n: u64) -> Result { 35 Ok(n.into()) 36 } 37 fn visit_f64(self, n: f64) -> Result { 38 Number::from_f64(n) 39 .map(Value::Number) 40 .ok_or_else(|| E::custom("non-finite number")) 41 } 42 fn visit_str(self, s: &str) -> Result { 43 Ok(Value::String(s.to_string())) 44 } 45 fn visit_string(self, s: String) -> Result { 46 Ok(Value::String(s)) 47 } 48 fn visit_seq>(self, mut a: A) -> Result { 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>(self, mut a: A) -> Result { 56 let mut out = Map::new(); 57 while let Some(k) = a.next_key::()? { 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. 69 pub fn parse(b: &[u8]) -> Option { 70 serde_json::from_slice::(b).ok().map(|s| s.0) 71 } 72 73 #[cfg(test)] 74 mod 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 }