Skip to content

Commit

Permalink
wasm: add feature flag support for compiling on wasm targets
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcnamara committed Aug 24, 2023
1 parent c3bd9d8 commit b0c2a68
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ zip = {version = "0.6.4 ", default-features = false, features = ["deflate"]}
regex = "1.7.3"
lazy_static = "1.4.0"
polars-error= {version = "0.32.1", default-features = false, features = [], optional = true }
js-sys = {version = "0.3.64", optional = true}

[dev-dependencies]
pretty_assertions = "1.3.0"
Expand All @@ -37,6 +38,9 @@ chrono = ["dep:chrono"]
# easier to write.
polars = ["dep:polars-error"]

# `wasm`: todo.
wasm = ["js-sys"]

# `test-resave`: Developer only testing feature.
test-resave = []

Expand Down
29 changes: 28 additions & 1 deletion src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

#![warn(missing_docs)]
use regex::Regex;

#[cfg(not(all(
feature = "wasm",
target_arch = "wasm32",
not(any(target_os = "emscripten", target_os = "wasi"))
)))]
use std::time::SystemTime;

#[cfg(feature = "chrono")]
Expand Down Expand Up @@ -1175,11 +1181,32 @@ impl ExcelDateTime {
// Get the current UTC time. This is used to set some Excel metadata
// timestamps.
pub(crate) fn utc_now() -> String {
let timestamp = Self::system_now();
Self::unix_time_to_rfc3339(timestamp)
}

// Get the current time from the system time.
#[cfg(not(all(
feature = "wasm",
target_arch = "wasm32",
not(any(target_os = "emscripten", target_os = "wasi"))
)))]
fn system_now() -> u64 {
let timestamp = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.expect("SystemTime::now() is before Unix epoch");
timestamp.as_secs()
}

Self::unix_time_to_rfc3339(timestamp.as_secs())
// Get the current time on Wasm/JS systems.
#[cfg(all(
feature = "wasm",
target_arch = "wasm32",
not(any(target_os = "emscripten", target_os = "wasi"))
))]
fn system_now() -> u64 {
let timestamp = js_sys::Date::now();
timestamp as u64
}

// Convert to UTC date in RFC 3339 format. This is used in custom
Expand Down
9 changes: 9 additions & 0 deletions src/packager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@

use std::collections::HashSet;
use std::io::{Seek, Write};

#[cfg(not(feature = "wasm"))]
use std::thread;

use zip::write::FileOptions;
Expand Down Expand Up @@ -110,6 +112,7 @@ impl<W: Write + Seek + Send> Packager<W> {
// Assemble, but don't write, the worksheet files in parallel. These are
// generally the largest files and the threading can help performance if
// there are multiple large worksheets.
#[cfg(not(feature = "wasm"))]
thread::scope(|scope| {
for worksheet in &mut workbook.worksheets {
scope.spawn(|| {
Expand All @@ -118,6 +121,12 @@ impl<W: Write + Seek + Send> Packager<W> {
}
});

#[cfg(feature = "wasm")]
// For wasm targets don't use threading.
for worksheet in &mut workbook.worksheets {
worksheet.assemble_xml_file();
}

// Write the worksheet file and and associated rel files.
for (index, worksheet) in workbook.worksheets.iter_mut().enumerate() {
self.write_worksheet_file(worksheet, index + 1)?;
Expand Down

0 comments on commit b0c2a68

Please sign in to comment.