typst_library/
symbols.rs

1//! Modifiable symbols.
2
3use crate::foundations::{Module, Scope, Symbol, Value};
4
5/// Hook up all `symbol` definitions.
6pub(super) fn define(global: &mut Scope) {
7    global.start_category(crate::Category::Symbols);
8    extend_scope_from_codex_module(global, codex::ROOT);
9    global.reset_category();
10}
11
12/// Hook up all math `symbol` definitions, i.e., elements of the `sym` module.
13pub(super) fn define_math(math: &mut Scope) {
14    extend_scope_from_codex_module(math, codex::SYM);
15}
16
17fn extend_scope_from_codex_module(scope: &mut Scope, module: codex::Module) {
18    for (name, binding) in module.iter() {
19        let value = match binding.def {
20            codex::Def::Symbol(s) => Value::Symbol(s.into()),
21            codex::Def::Module(m) => Value::Module(Module::new(name, m.into())),
22        };
23
24        let scope_binding = scope.define(name, value);
25        if let Some(message) = binding.deprecation {
26            scope_binding.deprecated(message);
27        }
28    }
29}
30
31impl From<codex::Module> for Scope {
32    fn from(module: codex::Module) -> Scope {
33        let mut scope = Self::new();
34        extend_scope_from_codex_module(&mut scope, module);
35        scope
36    }
37}
38
39impl From<codex::Symbol> for Symbol {
40    fn from(symbol: codex::Symbol) -> Self {
41        match symbol {
42            codex::Symbol::Single(c) => Symbol::single(c),
43            codex::Symbol::Multi(list) => Symbol::list(list),
44        }
45    }
46}