Skip to content

Commit

Permalink
feat: adjust new sink api (#594)
Browse files Browse the repository at this point in the history
* dev: delete eval crate

* dev: adjust new sink api
  • Loading branch information
Myriad-Dreamin authored Oct 19, 2024
1 parent 0a024e2 commit 3382633
Show file tree
Hide file tree
Showing 14 changed files with 118 additions and 130 deletions.
1 change: 1 addition & 0 deletions benches/lowering/src/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ fn compile(driver: &CompileDriver, src: &str) -> Arc<TypstDocument> {
std::marker::PhantomData.compile(&this.snapshot(), &mut Default::default())
})
.unwrap()
.output
}

fn main() {
Expand Down
4 changes: 2 additions & 2 deletions cli/src/query_repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl Completer for ReplContext {

Ok(autocomplete(
&world,
doc.as_ref().map(|f| f.as_ref()),
doc.as_ref().map(|f| f.output.as_ref()),
&main,
cursor,
true,
Expand Down Expand Up @@ -256,7 +256,7 @@ impl ReplContext {
.ok();
doc.and_then(|doc| {
driver
.query(line, &doc)
.query(line, &doc.output)
.map_err(|err| self.process_err(&driver, err))
.ok()
})
Expand Down
21 changes: 4 additions & 17 deletions crates/reflexo-typst/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,14 +320,11 @@ impl<F: CompilerFeat + Send + Sync + 'static> CompileActor<F> {

fn snapshot(&self, is_once: bool, reason: CompileReasons) -> CompileSnapshot<F> {
let world = self.verse.snapshot();
let mut env = self.make_env(if is_once {
let env = self.make_env(if is_once {
self.once_feature_set.clone()
} else {
self.watch_feature_set.clone()
});
if env.sink.is_none() {
env.sink = Some(Default::default());
}
CompileSnapshot {
world: Arc::new(world.clone()),
env: env.clone(),
Expand Down Expand Up @@ -397,19 +394,9 @@ impl<F: CompilerFeat + Send + Sync + 'static> CompileActor<F> {
}

let elapsed = start.elapsed().unwrap_or_default();
let rep;
match &compiled.doc {
Ok(..) => {
let warnings = compiled.env.sink.as_ref().unwrap().clone().warnings();
if warnings.is_empty() {
rep = CompileReport::CompileSuccess(id, warnings, elapsed);
} else {
rep = CompileReport::CompileWarning(id, warnings, elapsed);
}
}
Err(err) => {
rep = CompileReport::CompileError(id, err.clone(), elapsed);
}
let rep = match &compiled.doc {
Ok(..) => CompileReport::CompileSuccess(id, compiled.warnings.clone(), elapsed),
Err(err) => CompileReport::CompileError(id, err.clone(), elapsed),
};

let _ = ConsoleDiagReporter::default().export(
Expand Down
2 changes: 1 addition & 1 deletion crates/reflexo-typst/src/diag/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ where
// todo: log in browser compiler
#[cfg(feature = "system-compile")]
if _err.is_err() {
log::error!("failed to print diagnostics: {:?}", _err);
log::error!("failed to print diagnostics: {_err:?}");
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/reflexo-typst/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{

use reflexo_world::DETACHED_ENTRY;
use typst::{
diag::{eco_format, EcoString, SourceResult},
diag::{eco_format, EcoString, SourceResult, Warned},
foundations::Content,
};

Expand Down Expand Up @@ -47,7 +47,7 @@ impl<F: CompilerFeat, C: Compiler<W = CompilerWorld<F>>> CompileDriverImpl<C, F>
self.compiler.query(&self.snapshot(), selector, document)
}

pub fn compile(&mut self, env: &mut CompileEnv) -> SourceResult<Arc<TypstDocument>> {
pub fn compile(&mut self, env: &mut CompileEnv) -> SourceResult<Warned<Arc<TypstDocument>>> {
let world = self.snapshot();
self.compiler.ensure_main(&world)?;
self.compiler.compile(&world, env)
Expand Down
31 changes: 0 additions & 31 deletions crates/reflexo-typst/src/eval.rs

This file was deleted.

35 changes: 16 additions & 19 deletions crates/reflexo-typst/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use std::sync::Arc;

use reflexo_typst2vec::pass::Typst2VecPass;
use reflexo_world::{CompilerFeat, CompilerWorld};
use typst::{diag::SourceResult, World};
use typst::{
diag::{SourceResult, Warned},
World,
};

use crate::{
vector::ir::{LayoutRegion, LayoutRegionNode},
Expand Down Expand Up @@ -103,9 +106,13 @@ impl<C: Compiler> CompileMiddleware for CompileExporter<C> {
&mut self.compiler
}

fn wrap_compile(&mut self, world: &C::W, env: &mut CompileEnv) -> SourceResult<Arc<Document>> {
fn wrap_compile(
&mut self,
world: &C::W,
env: &mut CompileEnv,
) -> SourceResult<Warned<Arc<Document>>> {
let doc = self.inner_mut().compile(world, env)?;
self.exporter.export(world, doc.clone())?;
self.exporter.export(world, doc.output.clone())?;

Ok(doc)
}
Expand Down Expand Up @@ -198,7 +205,11 @@ where
&mut self.compiler
}

fn wrap_compile(&mut self, world: &C::W, env: &mut CompileEnv) -> SourceResult<Arc<Document>> {
fn wrap_compile(
&mut self,
world: &C::W,
env: &mut CompileEnv,
) -> SourceResult<Warned<Arc<Document>>> {
let start = reflexo::time::now();
// todo unwrap main id
let id = world.main_id().unwrap();
Expand All @@ -209,11 +220,6 @@ where
let _ = self.reporter.export(world, rep);
}

let sink = env.sink.take();
let origin = sink.is_some();

env.sink = Some(sink.unwrap_or_default());

let doc = self.inner_mut().compile(world, env);

let elapsed = start.elapsed().unwrap_or_default();
Expand All @@ -222,12 +228,7 @@ where

let doc = match doc {
Ok(doc) => {
let warnings = env.sink.as_ref().unwrap().clone().warnings();
if warnings.is_empty() {
rep = CompileReport::CompileSuccess(id, warnings, elapsed);
} else {
rep = CompileReport::CompileWarning(id, warnings, elapsed);
}
rep = CompileReport::CompileSuccess(id, doc.warnings.clone(), elapsed);

Ok(doc)
}
Expand All @@ -237,10 +238,6 @@ where
}
};

if !origin {
env.sink = None;
}

let rep = Arc::new((env.features.clone(), rep));
// we currently ignore export error here
let _ = self.reporter.export(world, rep);
Expand Down
16 changes: 11 additions & 5 deletions crates/reflexo-typst/src/export/dynamic_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use reflexo_typst2vec::pass::{CommandExecutor, Typst2VecPass};
use reflexo_typst2vec::IntoTypst;
use reflexo_vec2svg::{DynamicLayoutSvgExporter, MultiVecDocument};
use reflexo_world::TaskInputs;
use typst::diag::Warned;
use typst::foundations::IntoValue;
use typst::utils::LazyHash;
use typst::{diag::SourceResult, World};
Expand Down Expand Up @@ -148,7 +149,7 @@ impl<F: CompilerFeat, C: Compiler<W = CompilerWorld<F>>> DynamicLayoutCompiler<F
&mut self,
world: &CompilerWorld<F>,
env: &mut CompileEnv,
) -> SourceResult<(Arc<Document>, MultiVecDocument)> {
) -> SourceResult<(Warned<Arc<Document>>, MultiVecDocument)> {
let mut svg_exporter = DynamicLayoutSvgExporter::default();
svg_exporter.typst2vec.command_executor = self.command_executor.clone();
self.do_export_with(world, env, svg_exporter)
Expand All @@ -160,7 +161,7 @@ impl<F: CompilerFeat, C: Compiler<W = CompilerWorld<F>>> DynamicLayoutCompiler<F
world: &CompilerWorld<F>,
env: &mut CompileEnv,
mut svg_exporter: reflexo_vec2svg::DynamicLayoutSvgExporter,
) -> SourceResult<(Arc<Document>, MultiVecDocument)> {
) -> SourceResult<(Warned<Arc<Document>>, MultiVecDocument)> {
// self.export(doc.clone())?;
// checkout the entry file

Expand Down Expand Up @@ -191,10 +192,11 @@ impl<F: CompilerFeat, C: Compiler<W = CompilerWorld<F>>> DynamicLayoutCompiler<F

// compile and export document
let output = self.compiler.compile(&world, env)?;
let mut layout = svg_exporter.render(&output);
let mut layout = svg_exporter.render(&output.output);

if let Some(post_process_layout) = &self.post_process_layout {
layout = post_process_layout(&mut svg_exporter.typst2vec, output.clone(), layout);
layout =
post_process_layout(&mut svg_exporter.typst2vec, output.output.clone(), layout);
}
svg_exporter
.layouts
Expand Down Expand Up @@ -242,7 +244,11 @@ impl<F: CompilerFeat, C: Compiler<W = CompilerWorld<F>>> Compiler for DynamicLay
Ok(())
}

fn compile(&mut self, world: &Self::W, env: &mut CompileEnv) -> SourceResult<Arc<Document>> {
fn compile(
&mut self,
world: &Self::W,
env: &mut CompileEnv,
) -> SourceResult<Warned<Arc<Document>>> {
let (res, doc) = self.do_export(world, env)?;
std::fs::write(self.module_dest_path(), doc.to_bytes()).unwrap();
Ok(res)
Expand Down
Loading

0 comments on commit 3382633

Please sign in to comment.