oxedyne/fe2o3/fe2o3_austenite/src/eval/lib/mod.rs
2.4 KiB, 2 runs
created by r1870400018:63246, 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 | //! Contract (U0, 2026-09-23): the standard library. Every area file exposes |
| 2 | //! `define(&mut Scope)` for its globals, its inner native enum and `call(f, engine, args)`; an area |
| 3 | //! whose type has methods also exposes `method(name) -> Option<XFn>`. An element family file |
| 4 | //! (`text`, `layout`, `grid`, `visual`, `math`, `intro`, `model`) additionally exposes `fields(kind)`, |
| 5 | //! `construct(engine, kind, args) -> Outcome<Option<Content>>` (`None` to use the generic schema walk) |
| 6 | //! and `show(engine, elem, styles) -> Outcome<Option<Content>>` (`None` for a primitive flow lays out). |
| 7 | //! Global element functions are registered here generically; `math.*` elements are the math area's. |
| 8 | //! The file is `string.rs`, not `str.rs`, because a module named `str` shadows the primitive type. |
| 9 | |
| 10 | pub mod array; |
| 11 | pub mod calc; |
| 12 | pub mod color; |
| 13 | pub mod data; |
| 14 | pub mod datetime; |
| 15 | pub mod decimal; |
| 16 | pub mod dict; |
| 17 | pub mod foundations; |
| 18 | pub mod geom; |
| 19 | pub mod grid; |
| 20 | pub mod intro; |
| 21 | pub mod layout; |
| 22 | pub mod math; |
| 23 | pub mod model; |
| 24 | pub mod numbering; |
| 25 | pub mod pdf; |
| 26 | pub mod string; |
| 27 | pub mod sym; |
| 28 | pub mod text; |
| 29 | pub mod visual; |
| 30 | |
| 31 | use crate::eval::content::ElemKind; |
| 32 | use crate::eval::func::Func; |
| 33 | use crate::eval::scope::Scope; |
| 34 | use crate::eval::value::Value; |
| 35 | |
| 36 | /// The root scope every module's scope chains to. |
| 37 | pub fn library() -> Scope { |
| 38 | let mut s = Scope::new(); |
| 39 | for k in ElemKind::ALL { |
| 40 | if k.is_global() { |
| 41 | s.define(k.path(), Value::Func(Func::Element(*k))); |
| 42 | } |
| 43 | } |
| 44 | foundations::define(&mut s); |
| 45 | calc::define(&mut s); |
| 46 | string::define(&mut s); |
| 47 | array::define(&mut s); |
| 48 | dict::define(&mut s); |
| 49 | numbering::define(&mut s); |
| 50 | color::define(&mut s); |
| 51 | datetime::define(&mut s); |
| 52 | data::define(&mut s); |
| 53 | sym::define(&mut s); |
| 54 | geom::define(&mut s); |
| 55 | text::define(&mut s); |
| 56 | layout::define(&mut s); |
| 57 | grid::define(&mut s); |
| 58 | model::define(&mut s); |
| 59 | math::define(&mut s); |
| 60 | intro::define(&mut s); |
| 61 | pdf::define(&mut s); |
| 62 | // `std` is the library as a module, so a name a document redefines stays reachable as `std.text`. |
| 63 | let std = s.clone(); |
| 64 | s.define("std", Value::Module(std::sync::Arc::new(crate::eval::value::Module::new("std", std)))); |
| 65 | s |
| 66 | } |
| 67 | |
| 68 | /// Defines every element whose path starts with `prefix.` under its last segment, for a module scope |
| 69 | /// such as `math`. |
| 70 | pub fn define_elements(scope: &mut Scope, prefix: &str) { |
| 71 | for k in ElemKind::ALL { |
| 72 | let p = k.path(); |
| 73 | if p.len() > prefix.len() + 1 && p.starts_with(prefix) && p.as_bytes().get(prefix.len()) == Some(&b'.') { |
| 74 | scope.define(k.name(), Value::Func(Func::Element(*k))); |
| 75 | } |
| 76 | } |
| 77 | } |