Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev(core): add DynGenericExporter and DynPolymorphicExporter #411

Merged
merged 1 commit into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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