oxedyne/fe2o3/fe2o3_austenite/tests/eval_oracle/austenite.rs
21.2 KiB, 1 run
created by r1870400018:63612, 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 | // Austenite's side of every level, computed from the evaluator's own public surface: evaluate the |
| 2 | // fixture, run the outer fixpoint, then read the introspector (levels 1 and 2), realise the module |
| 3 | // once more for its structure (level 3) and read the laid-out pages (level 4). |
| 4 | // |
| 5 | // Each fixture runs on its own thread with a deadline and a panic guard, so a panic or an unbounded |
| 6 | // run is reported against the fixture and the run goes on; both always fail the suite. Every step |
| 7 | // records its own failure, so a unit whose layer works is measured even while a later layer is a |
| 8 | // stub. |
| 9 | |
| 10 | use crate::harness::corpus::Fixture; |
| 11 | use crate::harness::json::J; |
| 12 | use crate::harness::layout::{ |
| 13 | APage, |
| 14 | ARun, |
| 15 | }; |
| 16 | use crate::harness::structure::{ |
| 17 | self, |
| 18 | Sk, |
| 19 | }; |
| 20 | use crate::harness::PosRow; |
| 21 | |
| 22 | use oxedyne_fe2o3_austenite::eval::content::{ |
| 23 | Content, |
| 24 | ElemKind, |
| 25 | FieldId, |
| 26 | }; |
| 27 | use oxedyne_fe2o3_austenite::eval::fixpoint::{ |
| 28 | self, |
| 29 | Laid, |
| 30 | PageSink, |
| 31 | }; |
| 32 | use oxedyne_fe2o3_austenite::eval::intro::Introspector; |
| 33 | use oxedyne_fe2o3_austenite::eval::lib::foundations; |
| 34 | use oxedyne_fe2o3_austenite::eval::package; |
| 35 | use oxedyne_fe2o3_austenite::eval::ops; |
| 36 | use oxedyne_fe2o3_austenite::eval::realise::{ |
| 37 | realise_structure, |
| 38 | RealiseMode, |
| 39 | }; |
| 40 | use oxedyne_fe2o3_austenite::eval::select::Selector; |
| 41 | use oxedyne_fe2o3_austenite::eval::styles::StyleChain; |
| 42 | use oxedyne_fe2o3_austenite::eval::value::{ |
| 43 | Label, |
| 44 | Module, |
| 45 | Value, |
| 46 | }; |
| 47 | use oxedyne_fe2o3_austenite::eval::{ |
| 48 | eval_source, |
| 49 | Engine, |
| 50 | World, |
| 51 | }; |
| 52 | use oxedyne_fe2o3_austenite::page::{ |
| 53 | Page, |
| 54 | PlacedKind, |
| 55 | }; |
| 56 | |
| 57 | use oxedyne_fe2o3_core::prelude::*; |
| 58 | |
| 59 | use std::sync::mpsc; |
| 60 | use std::time::Duration; |
| 61 | |
| 62 | pub const PROBE: &str = "probe"; |
| 63 | |
| 64 | /// Keeps the final pass's pages for level 4, which reads them. The harness may hold a document's pages; |
| 65 | /// the pipeline's own sinks never do. |
| 66 | #[derive(Debug, Default)] |
| 67 | pub struct Keep { |
| 68 | pub pages: Vec<Page>, |
| 69 | } |
| 70 | |
| 71 | impl PageSink for Keep { |
| 72 | fn page(&mut self, _engine: &mut Engine, page: Page) -> Outcome<()> { |
| 73 | self.pages.push(page); |
| 74 | Ok(()) |
| 75 | } |
| 76 | |
| 77 | fn discard_pass(&mut self) -> Outcome<()> { |
| 78 | self.pages.clear(); |
| 79 | Ok(()) |
| 80 | } |
| 81 | |
| 82 | fn finish(&mut self, _engine: &mut Engine, _intro: &Introspector) -> Outcome<()> { Ok(()) } |
| 83 | } |
| 84 | |
| 85 | /// Where level-1 values were read from. |
| 86 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 87 | pub enum ProbeSource { |
| 88 | Introspector, // the query Typst itself answers, after layout |
| 89 | Content, // the evaluated module's content, for a fixture with no show rule or context |
| 90 | } |
| 91 | |
| 92 | #[derive(Debug, Default)] |
| 93 | pub struct AusOut { |
| 94 | pub eval: Option<String>, // the evaluation failure, if any |
| 95 | pub layout: Option<String>, // the fixpoint failure, if any |
| 96 | pub diags: Vec<String>, |
| 97 | pub first_error: Option<(String, Option<(usize, usize)>)>, // message, then line and column |
| 98 | pub first_file: Option<String>, // the file of the first error, relative to the root as Typst prints it |
| 99 | pub probes: Option<std::result::Result<(Vec<J>, ProbeSource), String>>, |
| 100 | pub positions: Option<std::result::Result<Vec<PosRow>, String>>, |
| 101 | pub skeleton: Option<std::result::Result<Vec<Sk>, String>>, |
| 102 | pub pages: Option<std::result::Result<Vec<APage>, String>>, |
| 103 | } |
| 104 | |
| 105 | pub enum RunEnd { |
| 106 | Done(AusOut), |
| 107 | Panic(String), |
| 108 | Timeout(u64), |
| 109 | } |
| 110 | |
| 111 | /// Which levels to compute; the harness asks only for what it will compare. |
| 112 | #[derive(Clone, Copy, Debug)] |
| 113 | pub struct Want { |
| 114 | pub levels: [bool; 4], |
| 115 | } |
| 116 | |
| 117 | pub fn run(fx: &Fixture, want: Want, timeout: Duration) -> RunEnd { |
| 118 | let (tx, rx) = mpsc::channel(); |
| 119 | let path = fx.path.clone(); |
| 120 | let root = fx.root.clone(); |
| 121 | let contextual = fx.contextual(); |
| 122 | let spawned = std::thread::Builder::new() |
| 123 | .name(fmt!("eval-oracle {}", fx.id())) |
| 124 | .stack_size(256 << 20) |
| 125 | .spawn(move || { |
| 126 | let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { |
| 127 | compute(&path, &root, want, contextual) |
| 128 | })); |
| 129 | let _ = tx.send(match r { |
| 130 | Ok(out) => Ok(out), |
| 131 | Err(p) => Err(panic_text(p)), |
| 132 | }); |
| 133 | }); |
| 134 | if let Err(e) = spawned { |
| 135 | return RunEnd::Panic(fmt!("could not start the fixture thread: {}", e)); |
| 136 | } |
| 137 | match rx.recv_timeout(timeout) { |
| 138 | Ok(Ok(out)) => RunEnd::Done(out), |
| 139 | Ok(Err(p)) => RunEnd::Panic(p), |
| 140 | Err(mpsc::RecvTimeoutError::Timeout) => RunEnd::Timeout(timeout.as_secs()), |
| 141 | Err(mpsc::RecvTimeoutError::Disconnected) => RunEnd::Panic("the fixture thread ended without a result".to_string()), |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | fn panic_text(p: Box<dyn std::any::Any + Send>) -> String { |
| 146 | if let Some(s) = p.downcast_ref::<&str>() { |
| 147 | return s.to_string(); |
| 148 | } |
| 149 | if let Some(s) = p.downcast_ref::<String>() { |
| 150 | return s.clone(); |
| 151 | } |
| 152 | "a panic with no message".to_string() |
| 153 | } |
| 154 | |
| 155 | /// Supplies the packages a fixture's area imports, as a host would: the area's own `packages` directory, |
| 156 | /// then Typst's package cache, where the real packages the corpus imports are kept. Austenite never |
| 157 | /// fetches a package, so one in neither place is an import error. |
| 158 | pub fn supply_packages(root: &std::path::Path) { |
| 159 | let own = root.join("packages"); |
| 160 | if own.is_dir() { |
| 161 | let _ = package::add_dir(own); |
| 162 | } |
| 163 | let cache = match std::env::var("TYPST_PACKAGE_CACHE_PATH") { |
| 164 | Ok(p) if !p.is_empty() => std::path::PathBuf::from(p), |
| 165 | _ => match std::env::var("XDG_CACHE_HOME") { |
| 166 | Ok(p) if !p.is_empty() => std::path::PathBuf::from(p).join("typst").join("packages"), |
| 167 | _ => std::path::PathBuf::from(std::env::var("HOME").unwrap_or_default()) |
| 168 | .join(".cache").join("typst").join("packages"), |
| 169 | }, |
| 170 | }; |
| 171 | if cache.is_dir() { |
| 172 | let _ = package::add_dir(cache); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | fn compute(path: &std::path::Path, root: &std::path::Path, want: Want, contextual: bool) -> AusOut { |
| 177 | let mut out = AusOut::default(); |
| 178 | supply_packages(root); |
| 179 | let mut world = World::new(root.to_path_buf()); |
| 180 | let id = match world.load(path) { |
| 181 | Ok(id) => id, |
| 182 | Err(e) => { |
| 183 | out.eval = Some(fmt!("load: {}", plain(&e))); |
| 184 | return out; |
| 185 | } |
| 186 | }; |
| 187 | let mut engine = Engine::new(world); |
| 188 | let module = match eval_source(&mut engine, id) { |
| 189 | Ok(m) => m, |
| 190 | Err(e) => { |
| 191 | out.eval = Some(plain(&e)); |
| 192 | out.diags = render_diags(&engine); |
| 193 | out.first_error = first_error(&engine); |
| 194 | out.first_file = first_file(&engine); |
| 195 | return out; |
| 196 | } |
| 197 | }; |
| 198 | let mut kept = Keep::default(); |
| 199 | let laid = match fixpoint::run(&mut engine, &module, &mut kept) { |
| 200 | Ok(l) => Some(l), |
| 201 | Err(e) => { |
| 202 | out.layout = Some(plain(&e)); |
| 203 | None |
| 204 | } |
| 205 | }; |
| 206 | if want.levels[0] { |
| 207 | out.probes = Some(probes(&module, laid.as_ref(), contextual)); |
| 208 | } |
| 209 | if want.levels[1] { |
| 210 | out.positions = Some(match &laid { |
| 211 | Some(l) => positions(l), |
| 212 | None => Err("no layout: the fixpoint did not run".to_string()), |
| 213 | }); |
| 214 | } |
| 215 | if want.levels[2] { |
| 216 | out.skeleton = Some(skeleton(&mut engine, &module)); |
| 217 | } |
| 218 | if want.levels[3] { |
| 219 | out.pages = Some(match &laid { |
| 220 | Some(_) => Ok(pages(&kept.pages)), |
| 221 | None => Err("no layout: the fixpoint did not run".to_string()), |
| 222 | }); |
| 223 | } |
| 224 | out.diags = render_diags(&engine); |
| 225 | out.first_error = first_error(&engine); |
| 226 | out.first_file = first_file(&engine); |
| 227 | out |
| 228 | } |
| 229 | |
| 230 | fn render_diags(engine: &Engine) -> Vec<String> { |
| 231 | engine.diags.iter().take(8).map(|d| d.render(&engine.world.sources)).collect() |
| 232 | } |
| 233 | |
| 234 | // The first error diagnostic's message and 1-based line and column, as Typst reports them. |
| 235 | fn first_error(engine: &Engine) -> Option<(String, Option<(usize, usize)>)> { |
| 236 | engine.diags.iter().find(|d| d.is_error()).map(|d| { |
| 237 | let pos = engine.world.sources.iter() |
| 238 | .find(|src| src.id == d.span.file && !d.span.is_detached()) |
| 239 | .map(|src| src.line_col(d.span.start)); |
| 240 | (d.message.clone(), pos) |
| 241 | }) |
| 242 | } |
| 243 | |
| 244 | // The first error's file as Typst prints it: relative to the project root, or `@ns/name:ver/path` inside a |
| 245 | // package. |
| 246 | fn first_file(engine: &Engine) -> Option<String> { |
| 247 | let d = engine.diags.iter().find(|d| d.is_error())?; |
| 248 | let src = engine.world.sources.iter().find(|s| s.id == d.span.file && !d.span.is_detached())?; |
| 249 | Some(match src.path.strip_prefix(&engine.world.root) { |
| 250 | Ok(rel) => rel.display().to_string(), |
| 251 | Err(_) => src.path.display().to_string(), |
| 252 | }) |
| 253 | } |
| 254 | |
| 255 | // Level 1 |
| 256 | |
| 257 | fn probes(module: &Module, laid: Option<&Laid>, contextual: bool) |
| 258 | -> std::result::Result<(Vec<J>, ProbeSource), String> |
| 259 | { |
| 260 | let from_intro = match laid { |
| 261 | Some(l) => match l.intro.query(&Selector::Label(Label::new(PROBE))) { |
| 262 | Ok(elems) => { |
| 263 | let mut v = Vec::new(); |
| 264 | for e in &elems { |
| 265 | match e.field("value") { |
| 266 | Some(x) => v.push(to_json(x)), |
| 267 | None => return Err(fmt!("a <{}> element ({}) has no `value` field", PROBE, |
| 268 | e.kind().map(|k| k.path()).unwrap_or("sequence"))), |
| 269 | } |
| 270 | } |
| 271 | Some(Ok((v, ProbeSource::Introspector))) |
| 272 | } |
| 273 | Err(e) => Some(Err(fmt!("introspector query: {}", plain(&e)))), |
| 274 | }, |
| 275 | None => None, |
| 276 | }; |
| 277 | match from_intro { |
| 278 | Some(Ok(v)) => Ok(v), |
| 279 | // Without layout, a fixture free of show rules and context has the same probes in its |
| 280 | // evaluated content as Typst's query finds; one with either does not, so it waits for layout. |
| 281 | other if !contextual => { |
| 282 | let mut v = Vec::new(); |
| 283 | let mut fault = None; |
| 284 | collect_probes(&module.content, &mut v, &mut fault, 0); |
| 285 | match fault { |
| 286 | Some(f) => Err(f), |
| 287 | None => match other { |
| 288 | Some(Err(e)) if v.is_empty() => Err(e), |
| 289 | _ => Ok((v, ProbeSource::Content)), |
| 290 | }, |
| 291 | } |
| 292 | } |
| 293 | Some(Err(e)) => Err(e), |
| 294 | None => Err("no layout, and the fixture uses show rules or context, so its probes \ |
| 295 | exist only after realisation".to_string()), |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | fn collect_probes(c: &Content, out: &mut Vec<J>, fault: &mut Option<String>, depth: usize) { |
| 300 | if depth > 256 || fault.is_some() { |
| 301 | return; |
| 302 | } |
| 303 | match c { |
| 304 | Content::Sequence(_) | Content::Styled(_) => for k in c.children() { |
| 305 | collect_probes(k, out, fault, depth + 1); |
| 306 | }, |
| 307 | Content::Elem(e) => { |
| 308 | if e.kind == ElemKind::Metadata && e.label.as_ref().map(|l| l.as_str() == PROBE).unwrap_or(false) { |
| 309 | match c.field("value") { |
| 310 | Some(v) => out.push(to_json(v)), |
| 311 | None => *fault = Some(fmt!("a <{}> metadata element has no `value` field", PROBE)), |
| 312 | } |
| 313 | return; |
| 314 | } |
| 315 | for (_, v) in &e.fields { |
| 316 | collect_value_content(v, out, fault, depth + 1); |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | fn collect_value_content(v: &Value, out: &mut Vec<J>, fault: &mut Option<String>, depth: usize) { |
| 323 | match v { |
| 324 | Value::Content(c) => collect_probes(c, out, fault, depth), |
| 325 | Value::Array(a) => for x in a.iter() { |
| 326 | collect_value_content(x, out, fault, depth + 1); |
| 327 | }, |
| 328 | _ => (), |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | /// A value as Typst's `typst eval` serialises it: JSON's own types as themselves, a symbol as its |
| 333 | /// text, content as `{"func": .., fields.., "label": ..}`, and everything else as its `repr`. |
| 334 | pub fn to_json(v: &Value) -> J { |
| 335 | match v { |
| 336 | Value::None => J::Null, |
| 337 | Value::Bool(b) => J::Bool(*b), |
| 338 | Value::Int(i) => J::Int(*i), |
| 339 | Value::Float(f) => if f.is_finite() { J::Float(*f) } else { J::Null }, |
| 340 | Value::Str(s) => J::Str(s.to_string()), |
| 341 | Value::Symbol(s) => J::Str(ops::symbol_text(s)), |
| 342 | Value::Array(a) => J::Arr(a.iter().map(to_json).collect()), |
| 343 | Value::Dict(d) => J::Obj(d.iter().map(|(k, x)| (k.to_string(), to_json(x))).collect()), |
| 344 | Value::Content(c) => content_json(c), |
| 345 | other => J::Str(foundations::repr(other)), |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | fn content_json(c: &Content) -> J { |
| 350 | let mut kv: Vec<(String, J)> = Vec::new(); |
| 351 | match c { |
| 352 | Content::Sequence(s) => { |
| 353 | kv.push(("func".to_string(), J::str("sequence"))); |
| 354 | kv.push(("children".to_string(), J::Arr(s.children.iter().map(content_json).collect()))); |
| 355 | } |
| 356 | Content::Styled(s) => { |
| 357 | kv.push(("func".to_string(), J::str("styled"))); |
| 358 | kv.push(("child".to_string(), content_json(&s.child))); |
| 359 | kv.push(("styles".to_string(), J::str("styles(..)"))); |
| 360 | } |
| 361 | Content::Elem(e) => { |
| 362 | kv.push(("func".to_string(), J::str(e.kind.name()))); |
| 363 | let mut fields: Vec<&(FieldId, Value)> = e.fields.iter().collect(); |
| 364 | fields.sort_by_key(|(id, _)| id.0); |
| 365 | for (id, v) in fields { |
| 366 | let name = match e.kind.field_spec(*id) { |
| 367 | // Typst serialises only the fields `fields()` shows. |
| 368 | Some(spec) if spec.internal => continue, |
| 369 | Some(spec) => spec.name.to_string(), |
| 370 | None => fmt!("<field {} of {} outside its schema>", id.0, e.kind.path()), |
| 371 | }; |
| 372 | kv.push((name, to_json(v))); |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | if let Some(l) = c.label() { |
| 377 | kv.push(("label".to_string(), J::Str(fmt!("<{}>", l.as_str())))); |
| 378 | } |
| 379 | J::Obj(kv) |
| 380 | } |
| 381 | |
| 382 | // Level 2 |
| 383 | |
| 384 | pub fn positions(laid: &Laid) -> std::result::Result<Vec<PosRow>, String> { |
| 385 | let mut rows = Vec::new(); |
| 386 | for e in laid.intro.elems() { |
| 387 | let kind = match e.kind() { |
| 388 | Some(k) => k, |
| 389 | None => continue, |
| 390 | }; |
| 391 | let probe = kind == ElemKind::Metadata && e.label().map(|l| l.as_str() == PROBE).unwrap_or(false); |
| 392 | let tracked = matches!(kind, ElemKind::Heading | ElemKind::Figure | ElemKind::Equation | ElemKind::Footnote); |
| 393 | if !(tracked || probe) { |
| 394 | continue; |
| 395 | } |
| 396 | let loc = match e.location() { |
| 397 | Some(l) => l, |
| 398 | None => return Err(fmt!("a realised {} has no location", kind.path())), |
| 399 | }; |
| 400 | match laid.intro.position(loc) { |
| 401 | Some(p) => rows.push(PosRow { func: kind.name().to_string(), page: p.page, x: p.x.to_pt(), y: p.y.to_pt() }), |
| 402 | None => return Err(fmt!("the introspector has no position for a {}", kind.path())), |
| 403 | } |
| 404 | } |
| 405 | Ok(rows) |
| 406 | } |
| 407 | |
| 408 | // Level 3 |
| 409 | |
| 410 | fn skeleton(engine: &mut Engine, module: &Module) -> std::result::Result<Vec<Sk>, String> { |
| 411 | engine.locator.reset(); |
| 412 | let mut out = Vec::new(); |
| 413 | match walk(engine, &module.content, &StyleChain::root(), RealiseMode::Document, &mut out, 0) { |
| 414 | Ok(()) => Ok(structure::normalise(out)), |
| 415 | Err(e) => Err(fmt!("realise: {}", plain(&e))), |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | fn field_content(c: &Content, name: &str) -> Option<Content> { |
| 420 | match c.field(name) { |
| 421 | Some(Value::Content(x)) => Some(x.clone()), |
| 422 | Some(Value::Str(s)) => Some(Content::text(s)), |
| 423 | _ => None, |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | fn field_int(c: &Content, name: &str) -> Option<i64> { |
| 428 | match c.field(name) { |
| 429 | Some(Value::Int(i)) => Some(*i), |
| 430 | _ => None, |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | fn field_bool(c: &Content, name: &str) -> bool { |
| 435 | matches!(c.field(name), Some(Value::Bool(true))) |
| 436 | } |
| 437 | |
| 438 | fn walk( |
| 439 | engine: &mut Engine, |
| 440 | content: &Content, |
| 441 | styles: &StyleChain, |
| 442 | mode: RealiseMode, |
| 443 | out: &mut Vec<Sk>, |
| 444 | depth: usize, |
| 445 | ) |
| 446 | -> Outcome<()> |
| 447 | { |
| 448 | if depth > 128 { |
| 449 | return Err(err!("the realised tree is deeper than 128 levels"; Excessive)); |
| 450 | } |
| 451 | let pairs = res!(realise_structure(engine, content, styles, mode)); |
| 452 | for p in pairs { |
| 453 | res!(element(engine, &p.content, &p.styles, mode, out, depth + 1)); |
| 454 | } |
| 455 | Ok(()) |
| 456 | } |
| 457 | |
| 458 | // One realised element into skeleton nodes. The tag table mirrors what Typst's HTML export writes for |
| 459 | // each model element; layout wrappers are transparent, as `div` and `span` are on the oracle side. |
| 460 | fn element( |
| 461 | engine: &mut Engine, |
| 462 | c: &Content, |
| 463 | styles: &StyleChain, |
| 464 | mode: RealiseMode, |
| 465 | out: &mut Vec<Sk>, |
| 466 | depth: usize, |
| 467 | ) |
| 468 | -> Outcome<()> |
| 469 | { |
| 470 | let kind = match c.kind() { |
| 471 | Some(k) => k, |
| 472 | None => return walk(engine, c, styles, mode, out, depth), |
| 473 | }; |
| 474 | let inline = RealiseMode::Inline; |
| 475 | let flow = RealiseMode::Flow; |
| 476 | let tagged = |name: &str, body: Option<Content>, m: RealiseMode, engine: &mut Engine, out: &mut Vec<Sk>| -> Outcome<()> { |
| 477 | let mut kids = Vec::new(); |
| 478 | if let Some(b) = body { |
| 479 | res!(walk(engine, &b, styles, m, &mut kids, depth)); |
| 480 | } |
| 481 | out.push(Sk::Tag(name.to_string(), kids)); |
| 482 | Ok(()) |
| 483 | }; |
| 484 | match kind { |
| 485 | ElemKind::Text => if let Some(Value::Str(s)) = c.get(FieldId(0)) { |
| 486 | out.push(Sk::Text(s.to_string())); |
| 487 | }, |
| 488 | ElemKind::Space => out.push(Sk::Text(" ".to_string())), |
| 489 | ElemKind::SmartQuote => out.push(Sk::Text(if field_bool(c, "double") { "\"" } else { "'" }.to_string())), |
| 490 | ElemKind::Linebreak => out.push(Sk::Tag("br".to_string(), Vec::new())), |
| 491 | ElemKind::Par => res!(tagged("p", field_content(c, "body"), inline, engine, out)), |
| 492 | ElemKind::Heading => { |
| 493 | let level = field_int(c, "level").or_else(|| field_int(c, "depth")).unwrap_or(1).clamp(1, 5); |
| 494 | res!(tagged(&fmt!("h{}", level + 1), field_content(c, "body"), inline, engine, out)); |
| 495 | } |
| 496 | ElemKind::Strong => res!(tagged("strong", field_content(c, "body"), inline, engine, out)), |
| 497 | ElemKind::Emph => res!(tagged("em", field_content(c, "body"), inline, engine, out)), |
| 498 | ElemKind::Super => res!(tagged("sup", field_content(c, "body"), inline, engine, out)), |
| 499 | ElemKind::Sub => res!(tagged("sub", field_content(c, "body"), inline, engine, out)), |
| 500 | ElemKind::Highlight => res!(tagged("mark", field_content(c, "body"), inline, engine, out)), |
| 501 | ElemKind::Link => res!(tagged("a", field_content(c, "body"), inline, engine, out)), |
| 502 | ElemKind::Quote => { |
| 503 | if field_bool(c, "block") { |
| 504 | res!(tagged("blockquote", field_content(c, "body"), flow, engine, out)); |
| 505 | } else { |
| 506 | res!(walk(engine, &field_content(c, "body").unwrap_or_default(), styles, inline, out, depth)); |
| 507 | } |
| 508 | } |
| 509 | ElemKind::Raw => { |
| 510 | let text = match c.field("text") { |
| 511 | Some(Value::Str(s)) => s.to_string(), |
| 512 | _ => String::new(), |
| 513 | }; |
| 514 | let code = Sk::Tag("code".to_string(), vec![Sk::Text(text)]); |
| 515 | if field_bool(c, "block") { |
| 516 | out.push(Sk::Tag("pre".to_string(), vec![code])); |
| 517 | } else { |
| 518 | out.push(code); |
| 519 | } |
| 520 | } |
| 521 | ElemKind::List | ElemKind::Enum | ElemKind::Terms => { |
| 522 | let name = match kind { |
| 523 | ElemKind::List => "ul", |
| 524 | ElemKind::Enum => "ol", |
| 525 | _ => "dl", |
| 526 | }; |
| 527 | let mut kids = Vec::new(); |
| 528 | if let Some(Value::Array(items)) = c.field("children") { |
| 529 | for it in items.iter() { |
| 530 | if let Value::Content(ic) = it { |
| 531 | res!(element(engine, ic, styles, flow, &mut kids, depth + 1)); |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | out.push(Sk::Tag(name.to_string(), kids)); |
| 536 | } |
| 537 | ElemKind::ListItem | ElemKind::EnumItem => res!(tagged("li", field_content(c, "body"), flow, engine, out)), |
| 538 | ElemKind::TermItem => { |
| 539 | res!(tagged("dt", field_content(c, "term"), inline, engine, out)); |
| 540 | res!(tagged("dd", field_content(c, "description"), flow, engine, out)); |
| 541 | } |
| 542 | ElemKind::Figure => { |
| 543 | let mut kids = Vec::new(); |
| 544 | if let Some(b) = field_content(c, "body") { |
| 545 | res!(walk(engine, &b, styles, flow, &mut kids, depth)); |
| 546 | } |
| 547 | if let Some(cap) = field_content(c, "caption") { |
| 548 | if cap.is(ElemKind::FigureCaption) { |
| 549 | res!(element(engine, &cap, styles, flow, &mut kids, depth + 1)); |
| 550 | } else { |
| 551 | let mut ck = Vec::new(); |
| 552 | res!(walk(engine, &cap, styles, inline, &mut ck, depth)); |
| 553 | kids.push(Sk::Tag("figcaption".to_string(), ck)); |
| 554 | } |
| 555 | } |
| 556 | out.push(Sk::Tag("figure".to_string(), kids)); |
| 557 | } |
| 558 | ElemKind::FigureCaption => res!(tagged("figcaption", field_content(c, "body"), inline, engine, out)), |
| 559 | ElemKind::Table => { |
| 560 | let mut kids = Vec::new(); |
| 561 | if let Some(Value::Array(items)) = c.field("children") { |
| 562 | for it in items.iter() { |
| 563 | if let Value::Content(ic) = it { |
| 564 | if ic.is(ElemKind::TableCell) { |
| 565 | res!(element(engine, ic, styles, flow, &mut kids, depth + 1)); |
| 566 | } else { |
| 567 | let mut ck = Vec::new(); |
| 568 | res!(walk(engine, ic, styles, flow, &mut ck, depth)); |
| 569 | kids.push(Sk::Tag("td".to_string(), ck)); |
| 570 | } |
| 571 | } |
| 572 | } |
| 573 | } |
| 574 | out.push(Sk::Tag("table".to_string(), kids)); |
| 575 | } |
| 576 | ElemKind::TableCell => res!(tagged("td", field_content(c, "body"), flow, engine, out)), |
| 577 | ElemKind::Equation => out.push(Sk::Tag("math".to_string(), Vec::new())), |
| 578 | ElemKind::Image => out.push(Sk::Tag("img".to_string(), Vec::new())), |
| 579 | ElemKind::Divider => out.push(Sk::Tag("hr".to_string(), Vec::new())), |
| 580 | // Left to levels 2 and 4 (see `structure.rs`), and invisible. |
| 581 | ElemKind::Footnote | ElemKind::FootnoteEntry | ElemKind::Metadata | ElemKind::CounterUpdate |
| 582 | | ElemKind::StateUpdate | ElemKind::Parbreak => (), |
| 583 | // Transparent: whatever body the element carries, realised in the mode it sits in. |
| 584 | _ => { |
| 585 | let m = match kind { |
| 586 | ElemKind::Block | ElemKind::Align | ElemKind::Pad | ElemKind::Columns => flow, |
| 587 | _ => mode, |
| 588 | }; |
| 589 | if let Some(b) = field_content(c, "body") { |
| 590 | res!(walk(engine, &b, styles, m, out, depth)); |
| 591 | } |
| 592 | } |
| 593 | } |
| 594 | Ok(()) |
| 595 | } |
| 596 | |
| 597 | // Level 4 |
| 598 | |
| 599 | pub fn pages(pages: &[Page]) -> Vec<APage> { |
| 600 | pages.iter().map(|p| APage { |
| 601 | width: p.geom.width.to_pt(), |
| 602 | height: p.geom.height.to_pt(), |
| 603 | runs: p.frame.placed.iter().filter_map(|pl| match &pl.kind { |
| 604 | PlacedKind::Text(st) => Some(ARun { |
| 605 | text: st.source().to_string(), |
| 606 | x0: pl.x.to_pt(), |
| 607 | x1: (pl.x + pl.dims.width).to_pt(), |
| 608 | base: (pl.y + pl.dims.height).to_pt(), |
| 609 | }), |
| 610 | _ => None, |
| 611 | }).collect(), |
| 612 | }).collect() |
| 613 | } |
| 614 | |
| 615 | /// A module's content, evaluated only: for the package corpus, which has no oracle. |
| 616 | pub fn eval_only(path: &std::path::Path, root: &std::path::Path, timeout: Duration) -> RunEnd { |
| 617 | let (tx, rx) = mpsc::channel(); |
| 618 | let path = path.to_path_buf(); |
| 619 | let root = root.to_path_buf(); |
| 620 | let spawned = std::thread::Builder::new() |
| 621 | .stack_size(256 << 20) |
| 622 | .spawn(move || { |
| 623 | let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { |
| 624 | let mut out = AusOut::default(); |
| 625 | supply_packages(&root); |
| 626 | let mut world = World::new(root); |
| 627 | match world.load(&path) { |
| 628 | Ok(id) => { |
| 629 | let mut engine = Engine::new(world); |
| 630 | if let Err(e) = eval_source(&mut engine, id) { |
| 631 | out.eval = Some(plain(&e)); |
| 632 | } |
| 633 | } |
| 634 | Err(e) => out.eval = Some(fmt!("load: {}", plain(&e))), |
| 635 | } |
| 636 | out |
| 637 | })); |
| 638 | let _ = tx.send(match r { |
| 639 | Ok(out) => Ok(out), |
| 640 | Err(p) => Err(panic_text(p)), |
| 641 | }); |
| 642 | }); |
| 643 | if let Err(e) = spawned { |
| 644 | return RunEnd::Panic(fmt!("could not start the eval thread: {}", e)); |
| 645 | } |
| 646 | match rx.recv_timeout(timeout) { |
| 647 | Ok(Ok(out)) => RunEnd::Done(out), |
| 648 | Ok(Err(p)) => RunEnd::Panic(p), |
| 649 | Err(mpsc::RecvTimeoutError::Timeout) => RunEnd::Timeout(timeout.as_secs()), |
| 650 | Err(mpsc::RecvTimeoutError::Disconnected) => RunEnd::Panic("the eval thread ended without a result".to_string()), |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | /// An error's text without the terminal colour codes `Error`'s display adds. |
| 655 | pub fn plain<E: std::fmt::Display>(e: &E) -> String { |
| 656 | let s = fmt!("{}", e); |
| 657 | let mut out = String::with_capacity(s.len()); |
| 658 | let mut chars = s.chars(); |
| 659 | while let Some(c) = chars.next() { |
| 660 | if c == '\u{1b}' { |
| 661 | for d in chars.by_ref() { |
| 662 | if d.is_ascii_alphabetic() { |
| 663 | break; |
| 664 | } |
| 665 | } |
| 666 | continue; |
| 667 | } |
| 668 | out.push(c); |
| 669 | } |
| 670 | out |
| 671 | } |