Skip to content

Commit

Permalink
dev(core): add DynPolymorphicExporter (#411)
Browse files Browse the repository at this point in the history
  • Loading branch information
Myriad-Dreamin authored Nov 13, 2023
1 parent d615866 commit 13ce66a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
57 changes: 53 additions & 4 deletions core/src/exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use typst::{diag::SourceResult, World};
pub type DynExporter<Input, Output = ()> = Box<dyn Exporter<Input, Output> + Send>;

pub trait Transformer<Input, Output = ()> {
/// 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<Output>;
}

Expand All @@ -21,8 +21,8 @@ where
}

pub trait Exporter<Input, Output = ()> {
/// 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<Input>) -> SourceResult<Output>;
}

Expand All @@ -45,6 +45,55 @@ where
}
}

pub type DynGenericExporter<X, Input, Output = ()> =
Box<dyn GenericExporter<Input, Output, W = X> + Send>;

pub trait GenericTransformer<Input, Output = ()> {
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<Output>;
}

pub trait GenericExporter<Input, Output = ()> {
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<Input>) -> SourceResult<Output>;
}

pub enum DynPolymorphicExporter<W, Input, Output> {
/// It is just applied to exactly the same world type.
Just(DynGenericExporter<W, Input, Output>),
/// A dynamic exporter that can be applied to any dyn world.
Dyn(DynExporter<Input, Output>),
}

impl<X, Input, Output> DynPolymorphicExporter<X, Input, Output> {
pub fn new(exporter: DynExporter<Input, Output>) -> Self {
Self::Dyn(exporter)
}
pub fn new_dyn(exporter: DynExporter<Input, Output>) -> Self {
Self::Dyn(exporter)
}
}

impl<X, Input, Output> GenericExporter<Input, Output> for DynPolymorphicExporter<X, Input, Output>
where
X: World,
{
type W = X;

fn export(&self, world: &Self::W, output: Arc<Input>) -> SourceResult<Output> {
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};

Expand Down
4 changes: 3 additions & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 13ce66a

Please sign in to comment.