diff --git a/crates/typst-cli/src/compile.rs b/crates/typst-cli/src/compile.rs index 3397935b6..dd15c8fc6 100644 --- a/crates/typst-cli/src/compile.rs +++ b/crates/typst-cli/src/compile.rs @@ -9,6 +9,7 @@ use termcolor::{ColorChoice, StandardStream}; use typst::diag::{bail, At, Severity, SourceDiagnostic, StrResult}; use typst::eval::Tracer; use typst::foundations::Datetime; +use typst::layout::Frame; use typst::model::Document; use typst::syntax::{FileId, Source, Span}; use typst::visualize::Color; @@ -250,6 +251,38 @@ fn export_image( Ok(()) } +/// Caches exported files so that we can avoid re-exporting them if they haven't +/// changed. +/// +/// This is done by having a list of size `files.len()` that contains the hashes +/// of the last rendered frame in each file. If a new frame is inserted, this +/// will invalidate the rest of the cache, this is deliberate as to decrease the +/// complexity and memory usage of such a cache. +pub struct ExportCache { + /// The hashes of last compilation's frames. + pub cache: Vec, +} + +impl ExportCache { + /// Creates a new export cache. + pub fn new() -> Self { + Self { cache: Vec::with_capacity(32) } + } + + /// Returns true if the entry is cached and appends the new hash to the + /// cache (for the next compilation). + pub fn is_cached(&mut self, i: usize, frame: &Frame) -> bool { + let hash = typst::util::hash128(frame); + + if i >= self.cache.len() { + self.cache.push(hash); + return false; + } + + std::mem::replace(&mut self.cache[i], hash) == hash + } +} + /// Opens the given file using: /// - The default file viewer if `open` is `None`. /// - The given viewer provided by `open` if it is `Some`. diff --git a/crates/typst-cli/src/world.rs b/crates/typst-cli/src/world.rs index 994566793..8c60d93cd 100644 --- a/crates/typst-cli/src/world.rs +++ b/crates/typst-cli/src/world.rs @@ -8,13 +8,12 @@ use comemo::Prehashed; use ecow::eco_format; use typst::diag::{FileError, FileResult, StrResult}; use typst::foundations::{Bytes, Datetime}; -use typst::layout::Frame; use typst::syntax::{FileId, Source, VirtualPath}; use typst::text::{Font, FontBook}; -use typst::util::hash128; use typst::{Library, World}; use crate::args::SharedArgs; +use crate::compile::ExportCache; use crate::fonts::{FontSearcher, FontSlot}; use crate::package::prepare_package; @@ -319,38 +318,6 @@ impl SlotCell { } } -/// Caches exported files so that we can avoid re-exporting them if they haven't -/// changed. -/// -/// This is done by having a list of size `files.len()` that contains the hashes -/// of the last rendered frame in each file. If a new frame is inserted, this -/// will invalidate the rest of the cache, this is deliberate as to decrease the -/// complexity and memory usage of such a cache. -pub struct ExportCache { - /// The hashes of last compilation's frames. - pub cache: Vec, -} - -impl ExportCache { - /// Creates a new export cache. - pub fn new() -> Self { - Self { cache: Vec::with_capacity(32) } - } - - /// Returns true if the entry is cached and appends the new hash to the - /// cache (for the next compilation). - pub fn is_cached(&mut self, i: usize, frame: &Frame) -> bool { - let hash = hash128(frame); - - if i >= self.cache.len() { - self.cache.push(hash); - return false; - } - - std::mem::replace(&mut self.cache[i], hash) == hash - } -} - /// Read a file. fn read(path: &Path) -> FileResult> { let f = |e| FileError::from_io(e, path);