Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_austenite/tests/eval_oracle/json.rs

9.4 KiB, 1 run

created by r1870400018:63616, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1// The one value shape both sides of the comparison are brought to. The oracle's JSON is parsed into
2// it and Austenite's values are serialised into it, so a comparison never goes through text.
3//
4// A reader of its own rather than `fe2o3_jdat`'s: the comparison depends on two facts JSON text
5// carries and a general decoder is free to normalise away -- whether a number was written as an
6// integer or a float (`1` is an `int`, `1.0` a `float`, and Typst tells them apart), and the order
7// of an object's keys (a Typst dictionary iterates in insertion order).
8
9use oxedyne_fe2o3_core::prelude::*;
10
11#[derive(Clone, Debug)]
12pub enum J {
13 Null,
14 Bool(bool),
15 Int(i64),
16 Float(f64),
17 Str(String),
18 Arr(Vec<J>),
19 Obj(Vec<(String, J)>), // in source order
20}
21
22impl J {
23 pub fn str<S: Into<String>>(s: S) -> Self { J::Str(s.into()) }
24
25 pub fn get(&self, key: &str) -> Option<&J> {
26 match self {
27 J::Obj(kv) => kv.iter().find(|(k, _)| k == key).map(|(_, v)| v),
28 _ => None,
29 }
30 }
31
32 pub fn as_str(&self) -> Option<&str> {
33 match self {
34 J::Str(s) => Some(s),
35 _ => None,
36 }
37 }
38
39 pub fn as_i64(&self) -> Option<i64> {
40 match self {
41 J::Int(i) => Some(*i),
42 _ => None,
43 }
44 }
45
46 /// Is this a Typst content object (`{"func": ..., ...}`), whose keys are fields rather than a
47 /// dictionary's ordered entries?
48 pub fn is_content(&self) -> bool {
49 matches!(self.get("func"), Some(J::Str(_)))
50 }
51
52 /// Compact JSON, for reports.
53 pub fn render(&self) -> String {
54 let mut s = String::new();
55 self.push(&mut s);
56 s
57 }
58
59 fn push(&self, s: &mut String) {
60 match self {
61 J::Null => s.push_str("null"),
62 J::Bool(b) => s.push_str(if *b { "true" } else { "false" }),
63 J::Int(i) => s.push_str(&i.to_string()),
64 J::Float(f) => {
65 let t = fmt!("{:?}", f);
66 s.push_str(&t);
67 }
68 J::Str(t) => push_str_lit(s, t),
69 J::Arr(a) => {
70 s.push('[');
71 for (i, v) in a.iter().enumerate() {
72 if i > 0 {
73 s.push(',');
74 }
75 v.push(s);
76 }
77 s.push(']');
78 }
79 J::Obj(kv) => {
80 s.push('{');
81 for (i, (k, v)) in kv.iter().enumerate() {
82 if i > 0 {
83 s.push(',');
84 }
85 push_str_lit(s, k);
86 s.push(':');
87 v.push(s);
88 }
89 s.push('}');
90 }
91 }
92 }
93}
94
95fn push_str_lit(s: &mut String, t: &str) {
96 s.push('"');
97 for c in t.chars() {
98 match c {
99 '"' => s.push_str("\\\""),
100 '\\' => s.push_str("\\\\"),
101 '\n' => s.push_str("\\n"),
102 '\t' => s.push_str("\\t"),
103 c if (c as u32) < 0x20 => s.push_str(&fmt!("\\u{:04x}", c as u32)),
104 c => s.push(c),
105 }
106 }
107 s.push('"');
108}
109
110/// Parses one JSON document; trailing non-space text is an error.
111pub fn parse(text: &str) -> Outcome<J> {
112 let mut p = Parser { b: text.as_bytes(), i: 0 };
113 let v = res!(p.value(0));
114 p.ws();
115 if p.i != p.b.len() {
116 return Err(err!("JSON: trailing text at byte {}", p.i; Decode, Invalid));
117 }
118 Ok(v)
119}
120
121struct Parser<'a> {
122 b: &'a [u8],
123 i: usize,
124}
125
126impl<'a> Parser<'a> {
127 fn ws(&mut self) {
128 while self.i < self.b.len() && matches!(self.b[self.i], b' ' | b'\n' | b'\r' | b'\t') {
129 self.i += 1;
130 }
131 }
132
133 fn peek(&self) -> Option<u8> { self.b.get(self.i).copied() }
134
135 fn expect(&mut self, c: u8) -> Outcome<()> {
136 self.ws();
137 if self.peek() == Some(c) {
138 self.i += 1;
139 return Ok(());
140 }
141 Err(err!("JSON: expected '{}' at byte {}", c as char, self.i; Decode, Invalid))
142 }
143
144 fn lit(&mut self, word: &str, v: J) -> Outcome<J> {
145 if self.b[self.i..].starts_with(word.as_bytes()) {
146 self.i += word.len();
147 return Ok(v);
148 }
149 Err(err!("JSON: unexpected text at byte {}", self.i; Decode, Invalid))
150 }
151
152 fn value(&mut self, depth: usize) -> Outcome<J> {
153 if depth > 512 {
154 return Err(err!("JSON: nesting deeper than 512"; Decode, Excessive));
155 }
156 self.ws();
157 match self.peek() {
158 Some(b'n') => self.lit("null", J::Null),
159 Some(b't') => self.lit("true", J::Bool(true)),
160 Some(b'f') => self.lit("false", J::Bool(false)),
161 Some(b'"') => Ok(J::Str(res!(self.string()))),
162 Some(b'[') => {
163 self.i += 1;
164 let mut out = Vec::new();
165 self.ws();
166 if self.peek() == Some(b']') {
167 self.i += 1;
168 return Ok(J::Arr(out));
169 }
170 loop {
171 out.push(res!(self.value(depth + 1)));
172 self.ws();
173 match self.peek() {
174 Some(b',') => self.i += 1,
175 Some(b']') => {
176 self.i += 1;
177 return Ok(J::Arr(out));
178 }
179 _ => return Err(err!("JSON: expected ',' or ']' at byte {}", self.i; Decode, Invalid)),
180 }
181 }
182 }
183 Some(b'{') => {
184 self.i += 1;
185 let mut out = Vec::new();
186 self.ws();
187 if self.peek() == Some(b'}') {
188 self.i += 1;
189 return Ok(J::Obj(out));
190 }
191 loop {
192 self.ws();
193 let k = res!(self.string());
194 res!(self.expect(b':'));
195 let v = res!(self.value(depth + 1));
196 out.push((k, v));
197 self.ws();
198 match self.peek() {
199 Some(b',') => self.i += 1,
200 Some(b'}') => {
201 self.i += 1;
202 return Ok(J::Obj(out));
203 }
204 _ => return Err(err!("JSON: expected ',' or '}}' at byte {}", self.i; Decode, Invalid)),
205 }
206 }
207 }
208 Some(c) if c == b'-' || c.is_ascii_digit() => self.number(),
209 _ => Err(err!("JSON: unexpected byte at {}", self.i; Decode, Invalid)),
210 }
211 }
212
213 fn number(&mut self) -> Outcome<J> {
214 let start = self.i;
215 let mut float = false;
216 while let Some(c) = self.peek() {
217 match c {
218 b'0'..=b'9' | b'-' | b'+' => (),
219 b'.' | b'e' | b'E' => float = true,
220 _ => break,
221 }
222 self.i += 1;
223 }
224 let t = match std::str::from_utf8(&self.b[start..self.i]) {
225 Ok(t) => t,
226 Err(_) => return Err(err!("JSON: bad number at byte {}", start; Decode, Invalid)),
227 };
228 if !float {
229 if let Ok(i) = t.parse::<i64>() {
230 return Ok(J::Int(i));
231 }
232 }
233 match t.parse::<f64>() {
234 Ok(f) => Ok(J::Float(f)),
235 Err(_) => Err(err!("JSON: bad number '{}' at byte {}", t, start; Decode, Invalid)),
236 }
237 }
238
239 fn string(&mut self) -> Outcome<String> {
240 if self.peek() != Some(b'"') {
241 return Err(err!("JSON: expected a string at byte {}", self.i; Decode, Invalid));
242 }
243 self.i += 1;
244 let mut out: Vec<u8> = Vec::new();
245 loop {
246 let c = match self.peek() {
247 Some(c) => c,
248 None => return Err(err!("JSON: unterminated string"; Decode, Invalid)),
249 };
250 self.i += 1;
251 match c {
252 b'"' => break,
253 b'\\' => {
254 let e = match self.peek() {
255 Some(e) => e,
256 None => return Err(err!("JSON: unterminated escape"; Decode, Invalid)),
257 };
258 self.i += 1;
259 match e {
260 b'n' => out.push(b'\n'),
261 b't' => out.push(b'\t'),
262 b'r' => out.push(b'\r'),
263 b'b' => out.push(0x08),
264 b'f' => out.push(0x0c),
265 b'u' => {
266 let mut cp = res!(self.hex4());
267 if (0xd800..0xdc00).contains(&cp) && self.b[self.i..].starts_with(b"\\u") {
268 self.i += 2;
269 let lo = res!(self.hex4());
270 cp = 0x10000 + ((cp - 0xd800) << 10) + (lo.wrapping_sub(0xdc00) & 0x3ff);
271 }
272 let ch = char::from_u32(cp).unwrap_or('\u{fffd}');
273 let mut buf = [0u8; 4];
274 out.extend_from_slice(ch.encode_utf8(&mut buf).as_bytes());
275 }
276 other => out.push(other),
277 }
278 }
279 c => out.push(c),
280 }
281 }
282 match String::from_utf8(out) {
283 Ok(s) => Ok(s),
284 Err(_) => Err(err!("JSON: a string is not UTF-8"; Decode, Invalid)),
285 }
286 }
287
288 fn hex4(&mut self) -> Outcome<u32> {
289 let end = self.i + 4;
290 if end > self.b.len() {
291 return Err(err!("JSON: short \\u escape"; Decode, Invalid));
292 }
293 let t = match std::str::from_utf8(&self.b[self.i..end]) {
294 Ok(t) => t,
295 Err(_) => return Err(err!("JSON: bad \\u escape"; Decode, Invalid)),
296 };
297 self.i = end;
298 match u32::from_str_radix(t, 16) {
299 Ok(v) => Ok(v),
300 Err(_) => Err(err!("JSON: bad \\u escape '{}'", t; Decode, Invalid)),
301 }
302 }
303}
304
305/// Differences between an oracle value and Austenite's, each as `path: what`. Floats agree within
306/// 1e-9, relative for magnitudes above one; a content object's keys are compared as a set, a
307/// dictionary's in order.
308pub fn diff(path: &str, want: &J, got: &J, out: &mut Vec<String>) {
309 if out.len() >= 20 {
310 return;
311 }
312 match (want, got) {
313 (J::Null, J::Null) => (),
314 (J::Bool(a), J::Bool(b)) if a == b => (),
315 (J::Int(a), J::Int(b)) if a == b => (),
316 (J::Float(a), J::Float(b)) if close(*a, *b) => (),
317 (J::Str(a), J::Str(b)) if a == b => (),
318 (J::Arr(a), J::Arr(b)) => {
319 if a.len() != b.len() {
320 out.push(fmt!("{}: typst has {} item(s), austenite {}", path, a.len(), b.len()));
321 }
322 for (i, (x, y)) in a.iter().zip(b.iter()).enumerate() {
323 diff(&fmt!("{}[{}]", path, i), x, y, out);
324 }
325 }
326 (J::Obj(a), J::Obj(b)) => {
327 let unordered = want.is_content() && got.is_content();
328 let ka: Vec<&str> = a.iter().map(|(k, _)| k.as_str()).collect();
329 let kb: Vec<&str> = b.iter().map(|(k, _)| k.as_str()).collect();
330 if unordered {
331 let mut sa = ka.clone();
332 let mut sb = kb.clone();
333 sa.sort();
334 sb.sort();
335 if sa != sb {
336 out.push(fmt!("{}: typst fields {:?}, austenite {:?}", path, ka, kb));
337 }
338 } else if ka != kb {
339 out.push(fmt!("{}: typst keys {:?}, austenite {:?}", path, ka, kb));
340 }
341 for (k, x) in a {
342 if let Some(y) = got.get(k) {
343 diff(&fmt!("{}.{}", path, k), x, y, out);
344 }
345 }
346 }
347 _ => out.push(fmt!("{}: typst {}, austenite {}", path, clip(&want.render()), clip(&got.render()))),
348 }
349}
350
351fn close(a: f64, b: f64) -> bool {
352 if a == b || (a.is_nan() && b.is_nan()) {
353 return true;
354 }
355 (a - b).abs() <= 1e-9 * a.abs().max(b.abs()).max(1.0)
356}
357
358pub fn clip(s: &str) -> String {
359 if s.chars().count() <= 160 {
360 return s.to_string();
361 }
362 let mut t: String = s.chars().take(157).collect();
363 t.push_str("...");
364 t
365}