oxedyne/fe2o3/fe2o3_austenite/src/eval/lib/model/lookup.rs
3.4 KiB, 1 run
created by r1870400018:63270, 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 | // U5 owns this file: what the model's shows ask of the previous pass -- the element a label names, a |
| 2 | // counter's numbers at a location, a query's matches. Every such question goes through here, and here |
| 3 | // through U8's recorded introspection (`eval::intro`), so the fixpoint sees each answer the model's |
| 4 | // shows depend on and lays out again when one moves. A show runs as its element's own context, so a |
| 5 | // label is resolved without the `context` check `locate` makes. |
| 6 | |
| 7 | use crate::eval::content::{ |
| 8 | Content, |
| 9 | ElemKind, |
| 10 | }; |
| 11 | use crate::eval::intro::{ |
| 12 | self, |
| 13 | Counter, |
| 14 | }; |
| 15 | use crate::eval::locate::Location; |
| 16 | use crate::eval::select::Selector; |
| 17 | use crate::eval::value::{ |
| 18 | Label, |
| 19 | Value, |
| 20 | }; |
| 21 | use crate::eval::Engine; |
| 22 | use crate::syntax::Span; |
| 23 | |
| 24 | use oxedyne_fe2o3_core::prelude::*; |
| 25 | |
| 26 | /// The one element carrying `label`, with Typst's errors for none and for several. |
| 27 | pub fn label(engine: &mut Engine, label: &Label, span: Span) -> Outcome<Content> { |
| 28 | intro::query_label(engine, label, span) |
| 29 | } |
| 30 | |
| 31 | /// As [`label`], but a label absent from the document is `None`, not an error. |
| 32 | pub fn label_opt(engine: &mut Engine, label: &Label, span: Span) -> Outcome<Option<Content>> { |
| 33 | intro::query_label_opt(engine, label, span) |
| 34 | } |
| 35 | |
| 36 | /// The elements a selector matches, in document order. |
| 37 | pub fn query(engine: &mut Engine, selector: &Selector, span: Span) -> Outcome<Vec<Content>> { |
| 38 | match intro::query(engine, selector) { |
| 39 | Ok(v) => Ok(v), |
| 40 | Err(e) => Err(engine.adopt(engine.diags.len(), span, e)), |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | /// A counter's numbers at `loc`: its initial state moved by every update and counted element up to and |
| 45 | /// including the one at `loc`. A location the previous pass did not see reads every update, as Typst's |
| 46 | /// first pass does. |
| 47 | pub fn counter_at(engine: &mut Engine, counter: &Counter, loc: Location, span: Span) -> Outcome<Vec<u64>> { |
| 48 | match intro::counter_at(engine, counter, loc) { |
| 49 | Ok(v) => Ok(v), |
| 50 | Err(e) => Err(engine.adopt(engine.diags.len(), span, e)), |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /// A counter at `loc` displayed with `numbering` (a pattern string or a function), which runs in a |
| 55 | /// context at `loc`. |
| 56 | pub fn display_counter( |
| 57 | engine: &mut Engine, |
| 58 | counter: &Counter, |
| 59 | loc: Location, |
| 60 | numbering: &Value, |
| 61 | span: Span, |
| 62 | ) |
| 63 | -> Outcome<Content> |
| 64 | { |
| 65 | display(engine, counter, loc, numbering, false, span) |
| 66 | } |
| 67 | |
| 68 | /// As [`display_counter`], with a pattern's first prefix and its suffix dropped: the number a reference |
| 69 | /// shows, `1` for a heading numbered `"1."`. A numbering function is called as it is. |
| 70 | pub fn display_counter_trimmed( |
| 71 | engine: &mut Engine, |
| 72 | counter: &Counter, |
| 73 | loc: Location, |
| 74 | numbering: &Value, |
| 75 | span: Span, |
| 76 | ) |
| 77 | -> Outcome<Content> |
| 78 | { |
| 79 | display(engine, counter, loc, numbering, true, span) |
| 80 | } |
| 81 | |
| 82 | fn display( |
| 83 | engine: &mut Engine, |
| 84 | counter: &Counter, |
| 85 | loc: Location, |
| 86 | numbering: &Value, |
| 87 | trimmed: bool, |
| 88 | span: Span, |
| 89 | ) |
| 90 | -> Outcome<Content> |
| 91 | { |
| 92 | let v = match intro::display_at(engine, counter, loc, numbering, trimmed, None) { |
| 93 | Ok(v) => v, |
| 94 | Err(e) => return Err(engine.adopt(engine.diags.len(), span, e)), |
| 95 | }; |
| 96 | super::common::shown(engine, v, span) |
| 97 | } |
| 98 | |
| 99 | /// The counter an element kind counts on: `counter(heading)`. |
| 100 | pub fn elem_counter(kind: ElemKind) -> Counter { Counter::of(kind) } |
| 101 | |
| 102 | /// Does a bibliography in the document hold `key`? |
| 103 | pub fn bib_has(engine: &mut Engine, key: &Label) -> bool { |
| 104 | match intro::query(engine, &Selector::Elem(ElemKind::Bibliography, None)) { |
| 105 | Ok(bibs) => bibs.iter().any(|b| super::bibliography::has_key(b, key)), |
| 106 | Err(_) => false, |
| 107 | } |
| 108 | } |