diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 4dd1560..812c2b3 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -20,3 +20,7 @@ jobs: run: cargo build --verbose - name: Run tests run: cargo test --verbose + - name: Check wasm32-unknown-unknown + run: | + rustup target add wasm32-unknown-unknown + cargo check --verbose --target wasm32-unknown-unknown --no-default-features diff --git a/CHANGELOG.md b/CHANGELOG.md index e3fd3cc..8679ac2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file. This projec ## [2.0.1] - Unreleased +### Added +- `wasm32-unknown-unknown` support: the current time is read through `web-time` on that target, where `std::time::SystemTime::now()` panics. Build with `default-features = false`, since `gethostname` does not support it (#26). + ### Fixed - ASCII control characters other than tab, CR and LF in display names, group names and unstructured text headers are written as RFC 2047 encoded-words; they were written raw inside quoted strings and text (#53). - Multipart bodies no longer start with an empty line before the first boundary (#14). diff --git a/Cargo.toml b/Cargo.toml index 2325f6b..e100c1b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,11 +19,17 @@ gethostname = ["dep:gethostname"] gethostname = { version = "1.0.0", optional = true } encodify = "1.0" +[target.'cfg(all(target_family = "wasm", target_os = "unknown"))'.dependencies] +web-time = "1.1" + [dev-dependencies] mail-parser = "1.0" serde = { version = "1.0", features = ["derive"]} serde_yaml = "0.9.10" serde_json = "1.0" +[target.'cfg(all(target_family = "wasm", target_os = "unknown"))'.dev-dependencies] +wasm-bindgen-test = "0.3" + [lib] doctest = false diff --git a/README.md b/README.md index 8ad43ce..6533b33 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ _mail-builder_ is a flexible **e-mail builder library** written in Rust. It incl - Generates **e-mail** messages conforming to the Internet Message Format standard (_RFC 5322_). - Full **MIME** support (_RFC 2045 - 2049_) with automatic selection of the most optimal encoding for each message body part. - Fast **Base64**, **Quoted-Printable** and **RFC 2047** encoding through [`encodify`](https://crates.io/crates/encodify), its only required dependency (`gethostname` is optional). +- Runs on `wasm32-unknown-unknown` with `default-features = false` (the clock is read through [`web-time`](https://crates.io/crates/web-time) on that target). Please note that this library does not support sending or parsing e-mail messages as these functionalities are provided by the crates [`mail-send`](https://crates.io/crates/mail-send) and [`mail-parser`](https://crates.io/crates/mail-parser). @@ -167,6 +168,12 @@ To run the testsuite: $ cargo test --all-features ``` +to run the WebAssembly tests (requires [`wasm-pack`](https://rustwasm.github.io/wasm-pack/) and Node.js): + +```bash + $ wasm-pack test --node --no-default-features +``` + or, to run the testsuite with MIRI: ```bash diff --git a/src/headers/date.rs b/src/headers/date.rs index b0a76c7..f4b0d6f 100644 --- a/src/headers/date.rs +++ b/src/headers/date.rs @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 OR MIT */ -use std::time::SystemTime; +use crate::{SystemTime, UNIX_EPOCH}; pub static DOW: &[&str] = &["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; pub static MONTH: &[&str] = &[ @@ -88,7 +88,7 @@ impl Date { pub fn now() -> Self { Self { date: SystemTime::now() - .duration_since(SystemTime::UNIX_EPOCH) + .duration_since(UNIX_EPOCH) .map(|d| d.as_secs()) .unwrap_or(0) as i64, } diff --git a/src/lib.rs b/src/lib.rs index b1f2ced..f1b08a4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,6 +20,11 @@ use std::{ #[cfg(feature = "gethostname")] use std::sync::OnceLock; +#[cfg(not(all(target_family = "wasm", target_os = "unknown")))] +pub(crate) use std::time::{SystemTime, UNIX_EPOCH}; +#[cfg(all(target_family = "wasm", target_os = "unknown"))] +pub(crate) use web_time::{SystemTime, UNIX_EPOCH}; + use headers::{ Header, HeaderType, address::Address, diff --git a/src/mime.rs b/src/mime.rs index 3c8c425..0c8869c 100644 --- a/src/mime.rs +++ b/src/mime.rs @@ -5,6 +5,7 @@ */ use crate::{ + SystemTime, UNIX_EPOCH, encoders::encode::BodyEncoding, headers::{ Header, HeaderType, content_type::ContentType, message_id::MessageId, raw::Raw, text::Text, @@ -16,7 +17,6 @@ use std::{ borrow::Cow, cell::Cell, sync::atomic::{AtomicU64, Ordering}, - time::{SystemTime, UNIX_EPOCH}, }; /// MIME part of an e-mail. diff --git a/tests/wasm.rs b/tests/wasm.rs new file mode 100644 index 0000000..c13e27a --- /dev/null +++ b/tests/wasm.rs @@ -0,0 +1,22 @@ +#![cfg(all(target_family = "wasm", target_os = "unknown"))] + +use mail_builder::MessageBuilder; +use wasm_bindgen_test::wasm_bindgen_test; + +// Default Date, Message-ID and boundaries read the clock without panicking on wasm32-unknown-unknown. +#[wasm_bindgen_test] +fn builds_message_with_generated_headers() { + let message = MessageBuilder::new() + .from(("John Doe", "john@doe.com")) + .to("jane@doe.com") + .subject("Hello") + .text_body("text") + .html_body("

html

") + .write_to_string() + .unwrap(); + + assert!(message.contains("\r\nDate: ")); + assert!(message.contains("Message-ID: <")); + assert!(message.contains("multipart/alternative")); + assert!(!message.contains("Date: Thu, 1 Jan 1970")); +}