Skip to content

Commit

Permalink
fix: errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Myriad-Dreamin committed Feb 3, 2025
1 parent 4382006 commit e16967a
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 40 deletions.
15 changes: 8 additions & 7 deletions cli/src/export.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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<chrono::Utc>) -> Option<TypstTimestamp> {
let typst_datetime = TypstDatetime::from_ymd_hms(
/// Convert [`chrono::DateTime`] to [`Timestamp`]
fn convert_datetime(date_time: chrono::DateTime<chrono::Utc>) -> Option<Timestamp> {
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()))
}
6 changes: 3 additions & 3 deletions crates/conversion/typst2vec/src/incr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl IncrDocServer {
}

/// Pack the delta into a binary blob.
pub fn pack_delta(&mut self, output: TypstDocument) -> Vec<u8> {
pub fn pack_delta(&mut self, output: &TypstDocument) -> Vec<u8> {
self.typst2vec.spans.reset();

// Increment the lifetime of all items to touch.
Expand All @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions crates/reflexo-typst/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub enum SucceededArtifact<F: CompilerFeat> {
}

impl<F: CompilerFeat> SucceededArtifact<F> {
pub fn success_doc(&self) -> Option<Arc<TypstPagedDocument>> {
pub fn success_doc(&self) -> Option<TypstDocument> {
match self {
SucceededArtifact::Compiled(artifact) => artifact.success_doc(),
SucceededArtifact::Suspend(snapshot) => snapshot.success_doc.clone(),
Expand Down Expand Up @@ -171,9 +171,9 @@ pub struct CompileActor<F: CompilerFeat> {
/// Estimated latest set of shadow files.
estimated_shadow_files: HashSet<Arc<Path>>,
/// The latest compiled document.
pub(crate) latest_doc: Option<Arc<TypstPagedDocument>>,
pub(crate) latest_doc: Option<TypstDocument>,
/// The latest successly compiled document.
latest_success_doc: Option<Arc<TypstPagedDocument>>,
latest_success_doc: Option<TypstDocument>,
/// feature set for compile_once mode.
once_feature_set: Arc<FeatureSet>,
/// Shared feature set for watch mode.
Expand Down
12 changes: 10 additions & 2 deletions crates/reflexo-typst/src/export.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::sync::Arc;

use reflexo::typst::TypstDocument;
use reflexo_typst2vec::pass::Typst2VecPass;
use typst::{
diag::{SourceResult, Warned},
Expand Down Expand Up @@ -85,8 +86,15 @@ impl<C: Compiler> CompileExporter<C> {
impl<F: CompilerFeat + 'static, C: Compiler> Exporter<CompileSnapshot<F>> for CompileExporter<C> {
/// Export a typst document using `reflexo_typst::DocumentExporter`.
fn export(&self, world: &dyn World, output: Arc<CompileSnapshot<F>>) -> 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(())
Expand Down
35 changes: 20 additions & 15 deletions crates/reflexo-typst/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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")]
Expand All @@ -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,
Expand Down Expand Up @@ -244,7 +246,11 @@ impl fmt::Display for CompileReportMsg<'_> {
}
}

type CompileRawResult = Deferred<(SourceResult<Warned<Arc<TypstPagedDocument>>>, CompileEnv)>;
type CompileRawResult = Deferred<(
SourceResult<TypstDocument>,
EcoVec<SourceDiagnostic>,
CompileEnv,
)>;
type DocState = std::sync::OnceLock<CompileRawResult>;

/// A signal that possibly triggers an export.
Expand All @@ -271,7 +277,7 @@ pub struct CompileSnapshot<F: CompilerFeat> {
/// Compiling the document.
doc_state: Arc<DocState>,
/// The last successfully compiled document.
pub success_doc: Option<Arc<TypstPagedDocument>>,
pub success_doc: Option<TypstDocument>,
}

impl<F: CompilerFeat + 'static> CompileSnapshot<F> {
Expand All @@ -281,9 +287,12 @@ impl<F: CompilerFeat + 'static> CompileSnapshot<F> {
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)
})
})
}
Expand Down Expand Up @@ -311,11 +320,7 @@ impl<F: CompilerFeat + 'static> CompileSnapshot<F> {
}

pub fn compile(&self) -> CompiledArtifact<F> {
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(),
Expand Down Expand Up @@ -349,9 +354,9 @@ pub struct CompiledArtifact<F: CompilerFeat> {
/// The diagnostics of the document.
pub warnings: EcoVec<SourceDiagnostic>,
/// The compiled document.
pub doc: SourceResult<Arc<TypstPagedDocument>>,
pub doc: SourceResult<TypstDocument>,
/// The last successfully compiled document.
success_doc: Option<Arc<TypstPagedDocument>>,
success_doc: Option<TypstDocument>,
}

impl<F: CompilerFeat> Clone for CompiledArtifact<F> {
Expand All @@ -368,7 +373,7 @@ impl<F: CompilerFeat> Clone for CompiledArtifact<F> {
}

impl<F: CompilerFeat> CompiledArtifact<F> {
pub fn success_doc(&self) -> Option<Arc<TypstPagedDocument>> {
pub fn success_doc(&self) -> Option<TypstDocument> {
self.doc
.as_ref()
.ok()
Expand Down
2 changes: 1 addition & 1 deletion fuzzers/incremental/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler/src/incr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down
13 changes: 7 additions & 6 deletions packages/typst.node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand All @@ -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};

Expand Down Expand Up @@ -451,18 +451,19 @@ fn parse_source_date_epoch(timestamp: i64) -> Result<DateTime<Utc>, 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<chrono::Utc>) -> Option<TypstTimestamp> {
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)]
Expand Down
4 changes: 2 additions & 2 deletions tests/incremental/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit e16967a

Please sign in to comment.