diff --git a/core/src/exporter.rs b/core/src/exporter.rs index 272f5035f..be387d3ae 100644 --- a/core/src/exporter.rs +++ b/core/src/exporter.rs @@ -5,8 +5,8 @@ use typst::{diag::SourceResult, World}; pub type DynExporter = Box + Send>; pub trait Transformer { - /// Export the given input with given world. - /// the writable world is hiden by trait itself. + /// Export the given input with given world. the writable world is hiden by + /// trait itself. fn export(&self, world: &dyn World, output: Input) -> SourceResult; } @@ -21,8 +21,8 @@ where } pub trait Exporter { - /// Export the given input with given world. - /// the writable world is hiden by trait itself. + /// Export the given input with given world. the writable world is hiden by + /// trait itself. fn export(&self, world: &dyn World, output: Arc) -> SourceResult; } @@ -45,6 +45,55 @@ where } } +pub type DynGenericExporter = + Box + Send>; + +pub trait GenericTransformer { + type W; + + /// Export the given input with given world. the writable world is hiden by + /// trait itself. + fn export(&self, world: &Self::W, output: Input) -> SourceResult; +} + +pub trait GenericExporter { + type W; + + /// Export the given input with given world. the writable world is hiden by + /// trait itself. + fn export(&self, world: &Self::W, output: Arc) -> SourceResult; +} + +pub enum DynPolymorphicExporter { + /// It is just applied to exactly the same world type. + Just(DynGenericExporter), + /// A dynamic exporter that can be applied to any dyn world. + Dyn(DynExporter), +} + +impl DynPolymorphicExporter { + pub fn new(exporter: DynExporter) -> Self { + Self::Dyn(exporter) + } + pub fn new_dyn(exporter: DynExporter) -> Self { + Self::Dyn(exporter) + } +} + +impl GenericExporter for DynPolymorphicExporter +where + X: World, +{ + type W = X; + + fn export(&self, world: &Self::W, output: Arc) -> SourceResult { + match self { + Self::Just(exporter) => exporter.export(world, output), + Self::Dyn(exporter) => exporter.export(world, output), + } + } +} + pub mod builtins { use std::{fs::File, sync::Arc}; diff --git a/core/src/lib.rs b/core/src/lib.rs index 3379f7a8e..5bd30ede5 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -23,7 +23,9 @@ pub mod config; // Core mechanism of typst-ts. pub(crate) mod exporter; pub use exporter::{builtins as exporter_builtins, utils as exporter_utils}; -pub use exporter::{DynExporter, Exporter, Transformer}; +pub use exporter::{ + DynExporter, DynGenericExporter, DynPolymorphicExporter, Exporter, GenericExporter, Transformer, +}; pub mod font; pub use font::{FontLoader, FontResolver, FontSlot}; pub mod package;