Skip to content

Commit

Permalink
dev(pkg::compiler): start error handling (#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
Myriad-Dreamin authored Sep 16, 2023
1 parent efb9d17 commit 19f6ef6
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions packages/compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,15 @@ impl FontLoader for SnapshotFontLoader {
// todo: remove this
unsafe impl Send for SnapshotFontLoader {}

// todo: design error handling
// todo: we return a string for now which is better than nothing
#[wasm_bindgen]
impl TypstCompiler {
pub fn reset(&mut self) {
pub fn reset(&mut self) -> Result<(), JsValue> {
// reset the world caches
self.compiler.reset().unwrap();
self.compiler.reset().map_err(|e| format!("{e:?}"))?;

Ok(())
}

pub fn add_source(&mut self, path: &str, content: &str, is_main: bool) -> bool {
Expand Down Expand Up @@ -155,13 +159,17 @@ impl TypstCompiler {
);

// compile and export document
let doc = self.compiler.compile().unwrap();
let doc = self.compiler.compile().map_err(|e| format!("{e:?}"))?;
let data = ast_exporter
.export(self.compiler.world(), Arc::new(doc))
.unwrap();
.map_err(|e| format!("{e:?}"))?;

let converted =
ansi_to_html::convert_escaped(String::from_utf8(data).unwrap().as_str()).unwrap();
let converted = ansi_to_html::convert_escaped(
String::from_utf8(data)
.map_err(|e| format!("{e:?}"))?
.as_str(),
)
.map_err(|e| format!("{e:?}"))?;
Ok(converted)
}

Expand All @@ -174,10 +182,10 @@ impl TypstCompiler {
typst_ts_svg_exporter::SvgModuleExporter::default(),
);

let doc = self.compiler.compile().unwrap();
let doc = self.compiler.compile().map_err(|e| format!("{e:?}"))?;
let artifact_bytes = ir_exporter
.export(self.compiler.world(), Arc::new(doc))
.unwrap();
.map_err(|e| format!("{e:?}"))?;
Ok(artifact_bytes)
}

Expand Down

0 comments on commit 19f6ef6

Please sign in to comment.