pub mod config;
pub mod error;
mod world;
#[cfg(feature = "wasm")]
pub mod wasm;
pub use config::Config;
pub use error::{Error, Result};
use std::path::Path;
pub fn compile(input: &Path, config: &Config) -> Result<Vec<u8>> {
if !input.exists() {
return Err(Error::InputNotFound(input.to_path_buf()));
}
let world = world::TypstWorld::from_file(input, config)?;
compile_world(&world)
}
fn compile_world(world: &world::TypstWorld) -> Result<Vec<u8>> {
let document = typst::compile(world)
.output
.map_err(|errors| Error::Compile(format_diagnostics(&errors)))?;
typst_pdf::pdf(&document, &typst_pdf::PdfOptions::default())
.map_err(|errors| Error::Compile(format_diagnostics(&errors)))
}
fn format_diagnostics(errors: &[typst::diag::SourceDiagnostic]) -> String {
errors
.iter()
.map(|error| {
let hints = error
.hints
.iter()
.map(|hint| format!("\n hint: {hint}"))
.collect::<String>();
format!("{}{}", error.message, hints)
})
.collect::<Vec<_>>()
.join("\n")
}