oxedyne/fe2o3/fe2o3_austenite/src/eval/lib/model/document.rs
2.7 KiB, 5 runs
created by r1870400018:63254, 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 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Original work copyright (c) the Typst project authors (typst-library `model/document.rs`, version 0.15.1). |
| 3 | // Modified for Austenite: the element's fields, defaults and default show, ported to Hematite's types, IR and error handling. |
| 4 | // U5 owns this file: document. In Typst 0.15 a document element is only constructed in the bundle |
| 5 | // target; in a paged document `set document(..)` carries the metadata (title, author, ...) that the PDF |
| 6 | // writer and `title()` read from the style chain. |
| 7 | |
| 8 | use crate::diag::DiagnosticKind; |
| 9 | use crate::eval::content::{ |
| 10 | Content, |
| 11 | ElemKind, |
| 12 | FieldDefault, |
| 13 | FieldSpec, |
| 14 | FieldType, |
| 15 | }; |
| 16 | use crate::eval::lib::model::common::{ |
| 17 | self, |
| 18 | choice, |
| 19 | expect, |
| 20 | expect_cast, |
| 21 | to_content, |
| 22 | CastErr, |
| 23 | K, |
| 24 | }; |
| 25 | use crate::eval::styles::StyleChain; |
| 26 | use crate::eval::value::{ |
| 27 | Type, |
| 28 | Value, |
| 29 | }; |
| 30 | use crate::eval::Engine; |
| 31 | |
| 32 | use oxedyne_fe2o3_core::prelude::*; |
| 33 | |
| 34 | const ANY: FieldType = FieldType::Any; |
| 35 | |
| 36 | static DOCUMENT: [FieldSpec; 8] = [ |
| 37 | FieldSpec::required("path", FieldType::Of(Type::Str)), |
| 38 | FieldSpec::named("format", ANY, FieldDefault::Auto), |
| 39 | FieldSpec::named("title", ANY, FieldDefault::None), |
| 40 | FieldSpec::named("author", ANY, FieldDefault::EmptyArray), |
| 41 | FieldSpec::named("description", ANY, FieldDefault::None), |
| 42 | FieldSpec::named("keywords", ANY, FieldDefault::EmptyArray), |
| 43 | FieldSpec::named("date", ANY, FieldDefault::Auto), |
| 44 | FieldSpec::required("body", ANY), |
| 45 | ]; |
| 46 | |
| 47 | pub fn fields(kind: ElemKind) -> &'static [FieldSpec] { |
| 48 | match kind { |
| 49 | ElemKind::Document => &DOCUMENT, |
| 50 | _ => &[], |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | pub fn cast(_kind: ElemKind, name: &str, v: Value) -> Result<Value, CastErr> { |
| 55 | match name { |
| 56 | "path" => expect_cast(v, &[K::Str]), |
| 57 | "format" => choice(&v, &["pdf", "png", "svg", "html"], true, false).map(|_| v), |
| 58 | "title" | "description" => match v { |
| 59 | Value::None => Ok(v), |
| 60 | other => expect(&other, &[K::Content, K::None]).map(|_| to_content(other)), |
| 61 | }, |
| 62 | "author" | "keywords" => match v { |
| 63 | Value::Str(_) => Ok(Value::array(vec![v])), |
| 64 | Value::Array(a) => { |
| 65 | for x in a.iter() { |
| 66 | ok!(expect(x, &[K::Str])); |
| 67 | } |
| 68 | Ok(Value::Array(a)) |
| 69 | } |
| 70 | other => Err(CastErr::Type(common::mismatch("string or array", &other))), |
| 71 | }, |
| 72 | "date" => match v { |
| 73 | Value::Datetime(_) | Value::None | Value::Auto => Ok(v), |
| 74 | other => Err(CastErr::Type(common::mismatch("datetime, none, or auto", &other))), |
| 75 | }, |
| 76 | "body" => expect(&v, &[K::Content]).map(|_| to_content(v)), |
| 77 | _ => Ok(v), |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | pub fn show(engine: &mut Engine, elem: &Content, _styles: &StyleChain) -> Outcome<Option<Content>> { |
| 82 | Err(engine.error_hint(DiagnosticKind::Type, elem.span(), "constructing a document is only supported in the bundle target", |
| 83 | "or use a `set document(..)` rule to configure metadata")) |
| 84 | } |