use std::fmt::{self, Debug, Formatter};
use std::hash::{Hash, Hasher};
use comemo::{Tracked, TrackedMut};
use typst_syntax::{FileId, RangeMapper, Span, SyntaxMode};
use typst_utils::LazyHash;
use crate::diag::SourceResult;
use crate::engine::{Engine, Route, Sink, Traced};
use crate::foundations::{
Args, Closure, Content, Context, Func, Module, NativeRuleMap, Scope, StyleChain,
Styles, Value,
};
use crate::introspection::{Introspector, Locator, SplitLocator};
use crate::layout::{Frame, Region};
use crate::model::DocumentInfo;
use crate::visualize::Color;
use crate::{Library, World};
macro_rules! routines {
($(
$(#[$attr:meta])*
fn $name:ident $(<$($time:lifetime),*>)? ($($args:tt)*) -> $ret:ty
)*) => {
pub struct Routines {
$(
$(#[$attr])*
pub $name: $(for<$($time),*>)? fn ($($args)*) -> $ret
),*
}
impl Hash for Routines {
fn hash<H: Hasher>(&self, _: &mut H) {}
}
impl Debug for Routines {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.pad("Routines(..)")
}
}
};
}
routines! {
fn rules() -> NativeRuleMap
fn eval_string(
world: Tracked<dyn World + '_>,
library: &LazyHash<Library>,
sink: TrackedMut<Sink>,
introspector: Tracked<dyn Introspector + '_>,
context: Tracked<Context>,
string: &str,
spans: SpanMode,
mode: SyntaxMode,
scope: Scope,
) -> SourceResult<Value>
fn eval_closure(
func: &Func,
closure: &LazyHash<Closure>,
world: Tracked<dyn World + '_>,
library: &LazyHash<Library>,
introspector: Tracked<dyn Introspector + '_>,
traced: Tracked<Traced>,
sink: TrackedMut<Sink>,
route: Tracked<Route>,
context: Tracked<Context>,
args: Args,
) -> SourceResult<Value>
fn realize<'a>(
kind: RealizationKind,
engine: &mut Engine,
locator: &mut SplitLocator,
arenas: &'a Arenas,
content: &'a Content,
styles: StyleChain<'a>,
) -> SourceResult<Vec<Pair<'a>>>
fn layout_frame(
engine: &mut Engine,
content: &Content,
locator: Locator,
styles: StyleChain,
region: Region,
) -> SourceResult<Frame>
fn html_module() -> Module
fn html_mathml_body<'a>(
content: &'a Content,
styles: StyleChain<'a>,
) -> Option<Option<&'a Content>>
fn html_span_filled(content: Content, color: Color) -> Content
}
#[derive(Hash)]
pub enum SpanMode<'a> {
Uniform(Span),
Mapped {
id: FileId,
mapper: &'a RangeMapper,
mapper_error_span: Span,
},
}
pub enum RealizationKind<'a> {
Bundle,
Document { info: &'a mut DocumentInfo },
Fragment { kind: &'a mut FragmentKind },
Par,
Math,
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum FragmentKind {
Inline,
Block,
}
#[derive(Default)]
pub struct Arenas {
pub content: typed_arena::Arena<Content>,
pub styles: typed_arena::Arena<Styles>,
pub bump: bumpalo::Bump,
}
pub type Pair<'a> = (&'a Content, StyleChain<'a>);