From e16967aa439553897b083ce01898561477a75e87 Mon Sep 17 00:00:00 2001 From: Myriad-Dreamin Date: Mon, 3 Feb 2025 12:16:40 +0800 Subject: [PATCH] fix: errors --- cli/src/export.rs | 15 ++++++----- crates/conversion/typst2vec/src/incr.rs | 6 ++--- crates/reflexo-typst/src/compile.rs | 6 ++--- crates/reflexo-typst/src/export.rs | 12 +++++++-- crates/reflexo-typst/src/lib.rs | 35 ++++++++++++++----------- fuzzers/incremental/src/main.rs | 2 +- packages/compiler/src/incr.rs | 2 +- packages/typst.node/src/lib.rs | 13 ++++----- tests/incremental/src/main.rs | 4 +-- 9 files changed, 55 insertions(+), 40 deletions(-) diff --git a/cli/src/export.rs b/cli/src/export.rs index 5316363bc..10b23f7b7 100644 --- a/cli/src/export.rs +++ b/cli/src/export.rs @@ -1,11 +1,11 @@ use std::path::{Path, PathBuf}; -use chrono::{Datelike, Timelike}; use reflexo_typst::exporter_builtins::{FsPathExporter, GroupExporter}; use reflexo_typst::program_meta::REPORT_BUG_MESSAGE; use reflexo_typst::svg::DefaultExportFeature; +use reflexo_typst::TypstDatetime; use reflexo_typst::TypstPagedDocument; -use reflexo_typst::{TypstDatetime, TypstTimestamp}; +use typst_pdf::Timestamp; use crate::{utils::current_dir, CompileArgs, ExportArgs}; @@ -162,16 +162,17 @@ pub fn prepare_exporters(args: &CompileArgs, entry_file: Option<&Path>) -> Group prepare_exporters_impl(args.export.clone(), output_dir, formats) } -/// Convert [`chrono::DateTime`] to [`TypstTimestamp`] -fn convert_datetime(date_time: chrono::DateTime) -> Option { - let typst_datetime = TypstDatetime::from_ymd_hms( +/// Convert [`chrono::DateTime`] to [`Timestamp`] +fn convert_datetime(date_time: chrono::DateTime) -> Option { + use chrono::{Datelike, Timelike}; + let datetime = TypstDatetime::from_ymd_hms( date_time.year(), date_time.month().try_into().ok()?, date_time.day().try_into().ok()?, date_time.hour().try_into().ok()?, date_time.minute().try_into().ok()?, date_time.second().try_into().ok()?, - )?; + ); - Some(TypstTimestamp::new_utc(typst_datetime)) + Some(Timestamp::new_utc(datetime.unwrap())) } diff --git a/crates/conversion/typst2vec/src/incr.rs b/crates/conversion/typst2vec/src/incr.rs index 9f78f3e16..1fb3c8393 100644 --- a/crates/conversion/typst2vec/src/incr.rs +++ b/crates/conversion/typst2vec/src/incr.rs @@ -33,7 +33,7 @@ impl IncrDocServer { } /// Pack the delta into a binary blob. - pub fn pack_delta(&mut self, output: TypstDocument) -> Vec { + pub fn pack_delta(&mut self, output: &TypstDocument) -> Vec { self.typst2vec.spans.reset(); // Increment the lifetime of all items to touch. @@ -44,8 +44,8 @@ impl IncrDocServer { // run typst2vec pass let pages = match output { - TypstDocument::Html(output) => self.typst2vec.html(&output.introspector, &output), - TypstDocument::Paged(output) => self.typst2vec.doc(&output.introspector, &output), + TypstDocument::Html(output) => self.typst2vec.html(&output.introspector, output), + TypstDocument::Paged(output) => self.typst2vec.doc(&output.introspector, output), }; // let new_items = builder.new_items.get_mut().len(); diff --git a/crates/reflexo-typst/src/compile.rs b/crates/reflexo-typst/src/compile.rs index 5bf7557d2..97eb87523 100644 --- a/crates/reflexo-typst/src/compile.rs +++ b/crates/reflexo-typst/src/compile.rs @@ -41,7 +41,7 @@ pub enum SucceededArtifact { } impl SucceededArtifact { - pub fn success_doc(&self) -> Option> { + pub fn success_doc(&self) -> Option { match self { SucceededArtifact::Compiled(artifact) => artifact.success_doc(), SucceededArtifact::Suspend(snapshot) => snapshot.success_doc.clone(), @@ -171,9 +171,9 @@ pub struct CompileActor { /// Estimated latest set of shadow files. estimated_shadow_files: HashSet>, /// The latest compiled document. - pub(crate) latest_doc: Option>, + pub(crate) latest_doc: Option, /// The latest successly compiled document. - latest_success_doc: Option>, + latest_success_doc: Option, /// feature set for compile_once mode. once_feature_set: Arc, /// Shared feature set for watch mode. diff --git a/crates/reflexo-typst/src/export.rs b/crates/reflexo-typst/src/export.rs index b2b23f5c2..4b7b47a28 100644 --- a/crates/reflexo-typst/src/export.rs +++ b/crates/reflexo-typst/src/export.rs @@ -1,5 +1,6 @@ use std::sync::Arc; +use reflexo::typst::TypstDocument; use reflexo_typst2vec::pass::Typst2VecPass; use typst::{ diag::{SourceResult, Warned}, @@ -85,8 +86,15 @@ impl CompileExporter { impl Exporter> for CompileExporter { /// Export a typst document using `reflexo_typst::DocumentExporter`. fn export(&self, world: &dyn World, output: Arc>) -> SourceResult<()> { - if let Ok(doc) = output.compile().doc { - self.exporter.export(world, doc)?; + if let Ok(doc) = output.as_ref().clone().compile().doc { + match doc { + TypstDocument::Paged(doc) => { + self.exporter.export(world, doc)?; + } + TypstDocument::Html(_doc) => { + todo!(); + } + } } Ok(()) diff --git a/crates/reflexo-typst/src/lib.rs b/crates/reflexo-typst/src/lib.rs index 009dc17d6..a962109c4 100644 --- a/crates/reflexo-typst/src/lib.rs +++ b/crates/reflexo-typst/src/lib.rs @@ -45,7 +45,6 @@ pub use exporter::ast::{dump_ast, AstExporter}; pub use exporter::json::JsonExporter; -use ::typst::diag::Warned; #[cfg(feature = "pdf")] pub use exporter::pdf::PdfDocExporter; #[cfg(feature = "pdf")] @@ -107,6 +106,9 @@ pub use world::package; pub use world::parser; pub use world::*; +#[cfg(feature = "pdf")] +pub use typst_pdf::Timestamp as TypstTimestamp; + #[cfg(feature = "system-watch")] mod watch; #[cfg(feature = "system-watch")] @@ -132,7 +134,7 @@ use std::sync::Arc; use std::sync::OnceLock; use ::typst::{ - diag::{At, SourceDiagnostic, SourceResult}, + diag::{At, SourceDiagnostic, SourceResult, Warned}, foundations::Content, syntax::Span, utils::Deferred, @@ -244,7 +246,11 @@ impl fmt::Display for CompileReportMsg<'_> { } } -type CompileRawResult = Deferred<(SourceResult>>, CompileEnv)>; +type CompileRawResult = Deferred<( + SourceResult, + EcoVec, + CompileEnv, +)>; type DocState = std::sync::OnceLock; /// A signal that possibly triggers an export. @@ -271,7 +277,7 @@ pub struct CompileSnapshot { /// Compiling the document. doc_state: Arc, /// The last successfully compiled document. - pub success_doc: Option>, + pub success_doc: Option, } impl CompileSnapshot { @@ -281,9 +287,12 @@ impl CompileSnapshot { let mut env = self.env.clone(); Deferred::new(move || { let w = w.as_ref(); - let mut c = std::marker::PhantomData; - let res = c.compile(w, &mut env); - (res, env) + let warned = std::marker::PhantomData.compile(&w, &mut env); + let (doc, warnings) = match warned { + Ok(doc) => (Ok(TypstDocument::Paged(doc.output)), doc.warnings), + Err(err) => (Err(err), EcoVec::default()), + }; + (doc, warnings, env) }) }) } @@ -311,11 +320,7 @@ impl CompileSnapshot { } pub fn compile(&self) -> CompiledArtifact { - let (doc, env) = self.start().wait().clone(); - let (doc, warnings) = match doc { - Ok(doc) => (Ok(doc.output), doc.warnings), - Err(err) => (Err(err), EcoVec::default()), - }; + let (doc, warnings, env) = self.start().wait().clone(); CompiledArtifact { signal: self.flags, world: self.world.clone(), @@ -349,9 +354,9 @@ pub struct CompiledArtifact { /// The diagnostics of the document. pub warnings: EcoVec, /// The compiled document. - pub doc: SourceResult>, + pub doc: SourceResult, /// The last successfully compiled document. - success_doc: Option>, + success_doc: Option, } impl Clone for CompiledArtifact { @@ -368,7 +373,7 @@ impl Clone for CompiledArtifact { } impl CompiledArtifact { - pub fn success_doc(&self) -> Option> { + pub fn success_doc(&self) -> Option { self.doc .as_ref() .ok() diff --git a/fuzzers/incremental/src/main.rs b/fuzzers/incremental/src/main.rs index 321e59e36..1255b3bd3 100644 --- a/fuzzers/incremental/src/main.rs +++ b/fuzzers/incremental/src/main.rs @@ -93,7 +93,7 @@ pub fn test_compiler( }) .unwrap(); - let delta = incr_server.pack_delta(TypstDocument::Paged(doc.output)); + let delta = incr_server.pack_delta(&TypstDocument::Paged(doc.output)); let delta = BytesModuleStream::from_slice(&delta).checkout_owned(); incr_client.merge_delta(delta); incr_client.set_layout(incr_client.doc.layouts[0].unwrap_single()); diff --git a/packages/compiler/src/incr.rs b/packages/compiler/src/incr.rs index d618d9325..95644c607 100644 --- a/packages/compiler/src/incr.rs +++ b/packages/compiler/src/incr.rs @@ -24,7 +24,7 @@ impl IncrServer { // evicted by compiler // comemo::evict(30); - self.inner.pack_delta(TypstDocument::Paged(doc)) + self.inner.pack_delta(&TypstDocument::Paged(doc)) } } diff --git a/packages/typst.node/src/lib.rs b/packages/typst.node/src/lib.rs index e06092941..95022f10e 100644 --- a/packages/typst.node/src/lib.rs +++ b/packages/typst.node/src/lib.rs @@ -14,7 +14,7 @@ use std::{ sync::Arc, }; -use chrono::{DateTime, Datelike, Timelike, Utc}; +use chrono::{DateTime, Utc}; use napi::bindgen_prelude::*; use napi_derive::napi; // use reflexo_typst::error::prelude::*; @@ -23,7 +23,7 @@ use reflexo_typst::{error::WithContext, foundations::IntoValue}; use reflexo_typst::{error_once, syntax::Span}; use reflexo_typst::{ Bytes, Compiler, DynamicLayoutCompiler, Exporter, ShadowApi, SystemCompilerFeat, TypstAbs, - TypstDatetime, TypstPagedDocument, TypstSystemWorld, TypstWorld, + TypstDatetime, TypstPagedDocument, TypstSystemWorld, TypstTimestamp, TypstWorld, }; use serde::{Deserialize, Serialize}; @@ -451,18 +451,19 @@ fn parse_source_date_epoch(timestamp: i64) -> Result, NodeError> { .ok_or_else(|| map_node_error(error_once!("timestamp out of range"))) } -/// Convert [`chrono::DateTime`] to [`TypstTimestamp`] +/// Convert [`chrono::DateTime`] to [`Timestamp`] fn convert_datetime(date_time: chrono::DateTime) -> Option { - let typst_datetime = TypstDatetime::from_ymd_hms( + use chrono::{Datelike, Timelike}; + let datetime = TypstDatetime::from_ymd_hms( date_time.year(), date_time.month().try_into().ok()?, date_time.day().try_into().ok()?, date_time.hour().try_into().ok()?, date_time.minute().try_into().ok()?, date_time.second().try_into().ok()?, - )?; + ); - Some(TypstTimestamp::new_utc(typst_datetime)) + Some(TypstTimestamp::new_utc(datetime.unwrap())) } #[derive(Default)] diff --git a/tests/incremental/src/main.rs b/tests/incremental/src/main.rs index bc2ec6cbc..d98bfbe76 100644 --- a/tests/incremental/src/main.rs +++ b/tests/incremental/src/main.rs @@ -61,7 +61,7 @@ pub fn test_compiler( driver.compile(&mut Default::default()) }) .unwrap(); - let server_delta = incr_server.pack_delta(TypstDocument::Paged(doc.output)); + let server_delta = incr_server.pack_delta(&TypstDocument::Paged(doc.output)); let server_delta = BytesModuleStream::from_slice(&server_delta).checkout_owned(); incr_client.merge_delta(server_delta); let _ = incr_svg_client.render_in_window(&mut incr_client, window); @@ -78,7 +78,7 @@ pub fn test_compiler( }) .unwrap(); - let server_delta = incr_server.pack_delta(TypstDocument::Paged(doc.output)); + let server_delta = incr_server.pack_delta(&TypstDocument::Paged(doc.output)); let sd = server_delta.len(); let server_delta = BytesModuleStream::from_slice(&server_delta).checkout_owned(); incr_client.merge_delta(server_delta);