From 02d288760f06a2d9ec7f8de175f75a6ace3289ca Mon Sep 17 00:00:00 2001 From: Evan Simkowitz Date: Wed, 19 Jul 2023 13:03:12 -0700 Subject: [PATCH 01/12] Init add js-sys for getting system tz from browser --- Cargo.toml | 5 ++++- src/system.rs | 21 ++++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 50ea6fd..7b791f1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,9 @@ cfg-if = "1.0.0" thiserror = { version = "1.0.30", optional = true } nom = { version = "7.1.0", optional = true } +[target.'cfg(wasm)'.dependencies] +js-sys = { version = "0.3.64", optional = true } + [target.'cfg(windows)'.dependencies] windows-sys = { version = "0.32.0", features = ["Win32_System_Time", "Win32_Foundation"], optional = true } @@ -31,7 +34,7 @@ serde = { version = "1.0.136", features = ["derive"] } [features] default = ["db"] -system = ["windows-sys", "thiserror", "db"] +system = ["windows-sys", "js-sys", "thiserror", "db"] posix-tz = ["nom", "thiserror", "db"] db = [] diff --git a/src/system.rs b/src/system.rs index ceabcbc..a55557b 100644 --- a/src/system.rs +++ b/src/system.rs @@ -30,6 +30,9 @@ use crate::timezones::get_by_name; use crate::Tz; use thiserror::Error; +#[cfg(wasm)] +use js_sys::{ Intl, Reflect }; + #[derive(Debug, Error)] pub enum Error { /// An IO error has occurred. @@ -52,6 +55,9 @@ pub enum Error { /// The timezone doesn't exist in the crate's database. #[error("unknown timezone name")] Unknown, + + #[error("unsupported platform")] + Unsupported, } pub fn get_timezone() -> Result<&'static Tz, Error> { @@ -67,7 +73,18 @@ pub fn get_timezone() -> Result<&'static Tz, Error> { } else { Err(Error::Undetermined) } - } else { + } else if #[cfg(wasm)] { + let options = Intl::DateTimeFormat::new(&Array::new(), &Object::new()) + .resolved_options(); + + let tz = Reflect::get(&options, &JsValue::from("timeZone")) + .expect("Cannot get timeZone") + .as_string() + .expect("timeZone is not a String"); + + let tz = get_by_name(&tz).ok_or(Error::Unknown)?; + Ok(tz) + } else if #[cfg(windows)] { unsafe { use windows_sys::Win32::System::Time::GetDynamicTimeZoneInformation; use windows_sys::Win32::System::Time::DYNAMIC_TIME_ZONE_INFORMATION; @@ -89,6 +106,8 @@ pub fn get_timezone() -> Result<&'static Tz, Error> { Ok(tz) } } + } else { + Err(Error::Unsupported) } } } From 19f1325bb7fcdc242656f10eecc5da9d6b095f35 Mon Sep 17 00:00:00 2001 From: Evan Simkowitz Date: Wed, 19 Jul 2023 13:58:47 -0700 Subject: [PATCH 02/12] fix cfg wasm issues --- Cargo.toml | 5 +++-- src/system.rs | 28 +++++++++++++++------------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7b791f1..9dd7fe8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,8 +20,9 @@ cfg-if = "1.0.0" thiserror = { version = "1.0.30", optional = true } nom = { version = "7.1.0", optional = true } -[target.'cfg(wasm)'.dependencies] +[target.'cfg(target_family = "wasm")'.dependencies] js-sys = { version = "0.3.64", optional = true } +wasm-bindgen = { version = "0.2.87", optional = true } [target.'cfg(windows)'.dependencies] windows-sys = { version = "0.32.0", features = ["Win32_System_Time", "Win32_Foundation"], optional = true } @@ -34,7 +35,7 @@ serde = { version = "1.0.136", features = ["derive"] } [features] default = ["db"] -system = ["windows-sys", "js-sys", "thiserror", "db"] +system = ["windows-sys", "js-sys", "wasm-bindgen", "thiserror", "db"] posix-tz = ["nom", "thiserror", "db"] db = [] diff --git a/src/system.rs b/src/system.rs index a55557b..b70c520 100644 --- a/src/system.rs +++ b/src/system.rs @@ -30,8 +30,10 @@ use crate::timezones::get_by_name; use crate::Tz; use thiserror::Error; -#[cfg(wasm)] -use js_sys::{ Intl, Reflect }; +#[cfg(target_family = "wasm")] +use js_sys::{ Intl, Reflect, Array, Object }; +#[cfg(target_family = "wasm")] +use wasm_bindgen::JsValue; #[derive(Debug, Error)] pub enum Error { @@ -73,17 +75,6 @@ pub fn get_timezone() -> Result<&'static Tz, Error> { } else { Err(Error::Undetermined) } - } else if #[cfg(wasm)] { - let options = Intl::DateTimeFormat::new(&Array::new(), &Object::new()) - .resolved_options(); - - let tz = Reflect::get(&options, &JsValue::from("timeZone")) - .expect("Cannot get timeZone") - .as_string() - .expect("timeZone is not a String"); - - let tz = get_by_name(&tz).ok_or(Error::Unknown)?; - Ok(tz) } else if #[cfg(windows)] { unsafe { use windows_sys::Win32::System::Time::GetDynamicTimeZoneInformation; @@ -106,6 +97,17 @@ pub fn get_timezone() -> Result<&'static Tz, Error> { Ok(tz) } } + } else if #[cfg(target_family = "wasm")] { + let options = Intl::DateTimeFormat::new(&Array::new(), &Object::new()) + .resolved_options(); + + let tz = Reflect::get(&options, &JsValue::from("timeZone")) + .expect("Cannot get timeZone") + .as_string() + .expect("timeZone is not a String"); + + let tz = get_by_name(&tz).ok_or(Error::Unknown)?; + Ok(tz) } else { Err(Error::Unsupported) } From ba9252df61693f1a8fb82806430467fea8b02a22 Mon Sep 17 00:00:00 2001 From: Evan Simkowitz Date: Wed, 19 Jul 2023 15:58:36 -0700 Subject: [PATCH 03/12] Add support for wasm-bindgen-test --- .cargo/config.toml | 2 ++ Cargo.toml | 11 +++++++++-- src/binary_search.rs | 40 ++++++++++++++++++++++++---------------- src/lib.rs | 12 ++++++++++++ src/posix_tz/parser.rs | 4 ++++ src/system.rs | 5 +++++ 6 files changed, 56 insertions(+), 18 deletions(-) create mode 100644 .cargo/config.toml diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..4ec2f3b --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,2 @@ +[target.wasm32-unknown-unknown] +runner = 'wasm-bindgen-test-runner' diff --git a/Cargo.toml b/Cargo.toml index 9dd7fe8..e59f978 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,9 +20,16 @@ cfg-if = "1.0.0" thiserror = { version = "1.0.30", optional = true } nom = { version = "7.1.0", optional = true } +[target.'cfg(not(target_family = "wasm"))'.dependencies] +time = { version = "0.3.7", features = ["macros"] } + [target.'cfg(target_family = "wasm")'.dependencies] js-sys = { version = "0.3.64", optional = true } -wasm-bindgen = { version = "0.2.87", optional = true } +time = { version = "0.3.7", features = ["macros", "wasm-bindgen"] } +wasm-bindgen = "0.2.87" + +[target.'cfg(target_family = "wasm")'.dev-dependencies] +wasm-bindgen-test = "0.3.37" [target.'cfg(windows)'.dependencies] windows-sys = { version = "0.32.0", features = ["Win32_System_Time", "Win32_Foundation"], optional = true } @@ -35,7 +42,7 @@ serde = { version = "1.0.136", features = ["derive"] } [features] default = ["db"] -system = ["windows-sys", "js-sys", "wasm-bindgen", "thiserror", "db"] +system = ["windows-sys", "js-sys", "thiserror", "db"] posix-tz = ["nom", "thiserror", "db"] db = [] diff --git a/src/binary_search.rs b/src/binary_search.rs index f17d72b..c699380 100644 --- a/src/binary_search.rs +++ b/src/binary_search.rs @@ -41,19 +41,27 @@ pub fn binary_search Ordering>(start: usize, end: usize, cmp: F) } } -#[test] -fn test_binary_search() { - assert_eq!(binary_search(0, 8, |x| x.cmp(&6)), Some(6)); - assert_eq!(binary_search(0, 5000, |x| x.cmp(&1337)), Some(1337)); - assert_eq!(binary_search(0, 5000, |x| x.cmp(&9000)), None); - assert_eq!(binary_search(30, 50, |x| x.cmp(&42)), Some(42)); - assert_eq!(binary_search(300, 500, |x| x.cmp(&42)), None); - assert_eq!( - binary_search(0, 500, |x| if x < 42 { - Ordering::Less - } else { - Ordering::Greater - }), - None - ); -} +#[cfg(test)] +mod tests { + #[cfg(target_family = "wasm")] + use wasm_bindgen_test::*; + + #[test] + #[cfg_attr(target_family = "wasm", wasm_bindgen_test)] + fn test_binary_search() { + assert_eq!(super::binary_search(0, 8, |x| x.cmp(&6)), Some(6)); + assert_eq!(super::binary_search(0, 5000, |x| x.cmp(&1337)), Some(1337)); + assert_eq!(super::binary_search(0, 5000, |x| x.cmp(&9000)), None); + assert_eq!(super::binary_search(30, 50, |x| x.cmp(&42)), Some(42)); + assert_eq!(super::binary_search(300, 500, |x| x.cmp(&42)), None); + assert_eq!( + super::binary_search(0, 500, |x| if x < 42 { + super::Ordering::Less + } else { + super::Ordering::Greater + }), + None + ); + } + +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index d77cd6c..f9f777d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -128,7 +128,11 @@ mod tests { use time::macros::{datetime, offset}; use time::OffsetDateTime; + #[cfg(target_family = "wasm")] + use wasm_bindgen_test::*; + #[test] + #[cfg_attr(target_family = "wasm", wasm_bindgen_test)] fn names() { //This test verifies that windows timezone names work fine. let shanghai = timezones::get_by_name("Asia/Shanghai"); @@ -139,6 +143,7 @@ mod tests { } #[test] + #[cfg_attr(target_family = "wasm", wasm_bindgen_test)] fn find() { let zones_iana = timezones::find_by_name("Asia"); //let zones_win = timezones::find_by_name("China Standard Time"); @@ -147,6 +152,7 @@ mod tests { } #[test] + #[cfg_attr(target_family = "wasm", wasm_bindgen_test)] fn offsets_and_name() { let tz = timezones::db::europe::LONDON; assert_eq!(tz.name(), "Europe/London"); @@ -155,6 +161,7 @@ mod tests { } #[test] + #[cfg_attr(target_family = "wasm", wasm_bindgen_test)] fn london_to_berlin() { let dt = datetime!(2016-10-8 17:0:0).assume_timezone_utc(timezones::db::europe::LONDON); let converted = dt.to_timezone(timezones::db::europe::BERLIN); @@ -164,6 +171,7 @@ mod tests { } #[test] + #[cfg_attr(target_family = "wasm", wasm_bindgen_test)] fn dst() { let london = timezones::db::europe::LONDON; let odt1 = datetime!(2021-01-01 12:0:0 UTC); @@ -178,6 +186,7 @@ mod tests { } #[test] + #[cfg_attr(target_family = "wasm", wasm_bindgen_test)] fn handles_forward_changeover() { assert_eq!( datetime!(2022-03-27 01:30) @@ -188,6 +197,7 @@ mod tests { } #[test] + #[cfg_attr(target_family = "wasm", wasm_bindgen_test)] fn handles_after_changeover() { assert_eq!( datetime!(2022-03-27 03:30) @@ -198,6 +208,7 @@ mod tests { } #[test] + #[cfg_attr(target_family = "wasm", wasm_bindgen_test)] fn handles_broken_time() { assert!(datetime!(2022-03-27 02:30) .assume_timezone(timezones::db::CET) @@ -205,6 +216,7 @@ mod tests { } #[test] + #[cfg_attr(target_family = "wasm", wasm_bindgen_test)] fn handles_backward_changeover() { // During backward changeover, the hour between 02:00 and 03:00 occurs twice, so either answer is correct assert_eq!( diff --git a/src/posix_tz/parser.rs b/src/posix_tz/parser.rs index 580ca29..8b3f97f 100644 --- a/src/posix_tz/parser.rs +++ b/src/posix_tz/parser.rs @@ -189,7 +189,11 @@ pub fn entry(input: &str) -> Result { mod tests { use crate::posix_tz::parser::{entry, Date, Dst, Offset, Rule, Std, Time, Tz}; + #[cfg(target_family = "wasm")] + use wasm_bindgen_test::*; + #[test] + #[cfg_attr(target_family = "wasm", wasm_bindgen_test)] fn basic() { let str = "ABC+1:00DEF,M1.2.3/4,56"; let (_, test) = entry(str).unwrap(); diff --git a/src/system.rs b/src/system.rs index b70c520..a69bc5b 100644 --- a/src/system.rs +++ b/src/system.rs @@ -58,6 +58,7 @@ pub enum Error { #[error("unknown timezone name")] Unknown, + /// The target platform is not supported. Windows, Unix, and WASM targets are the only supported for the system feature at this moment. #[error("unsupported platform")] Unsupported, } @@ -116,7 +117,11 @@ pub fn get_timezone() -> Result<&'static Tz, Error> { #[cfg(test)] mod tests { + #[cfg(target_family = "wasm")] + use wasm_bindgen_test::*; + #[test] + #[cfg_attr(target_family = "wasm", wasm_bindgen_test)] fn get_timezone() { let tz = super::get_timezone(); assert!(tz.is_ok()); From 13faa58f0a6b4c0c4f09fd4764bbc45a9b3fb586 Mon Sep 17 00:00:00 2001 From: Evan Simkowitz Date: Wed, 19 Jul 2023 16:07:08 -0700 Subject: [PATCH 04/12] fix doc --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index f9f777d..cd61cdc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -60,7 +60,7 @@ pub trait PrimitiveDateTimeExt: sealing::PrimitiveDateTimeExt { /// /// * `tz`: the target timezone. /// - /// returns: OffsetResult + /// returns: `OffsetResult` fn assume_timezone(&self, tz: &T) -> OffsetResult; /// Creates a new OffsetDateTime with the proper offset in the given timezone. From b710db962179887cb9b4a3e8427a9c65ff3790c3 Mon Sep 17 00:00:00 2001 From: Evan Simkowitz Date: Wed, 19 Jul 2023 16:53:55 -0700 Subject: [PATCH 05/12] Update documentation --- src/system.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/system.rs b/src/system.rs index a69bc5b..27d7d2b 100644 --- a/src/system.rs +++ b/src/system.rs @@ -26,6 +26,10 @@ // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +//! Support for getting time zone information from the target system. +//! +//! Currently only supported for Windows, Unix, and WASM targets. + use crate::timezones::get_by_name; use crate::Tz; use thiserror::Error; @@ -63,6 +67,12 @@ pub enum Error { Unsupported, } +/// Gets the current timezone from the system. +/// +/// Currently only supported for Windows, Unix, and WASM targets. +/// +/// # Errors +/// Returns an [Error](enum@Error) if the timezone cannot be determined. pub fn get_timezone() -> Result<&'static Tz, Error> { cfg_if::cfg_if! { if #[cfg(unix)] { From 8cdc9e76d6611170d436af4e68876decb50abec8 Mon Sep 17 00:00:00 2001 From: Evan Simkowitz Date: Wed, 19 Jul 2023 23:41:01 -0700 Subject: [PATCH 06/12] Add job for testing against WASM targets --- .github/workflows/dev.yml | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index b160b4a..4ef771d 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -36,3 +36,16 @@ jobs: # GitLab is far better because it is not downloading broken repositories. - name: Build and test run: cargo test --all-features + build-test-wasm: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + with: + submodules: recursive # garbage peace of shit mother fucking github downloading broken repositories!!!!! + # GitLab is far better because it is not downloading broken repositories. + - name: Install rustwasm + run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh + - name: Build and test for WASM - Chrome + run: wasm-pack test --headless --chrome --all-features + - name: Build and test for WASM - Firefox + run: wasm-pack test --headless --firefox --all-features From f5b67cba2cbf1b5900b047fec055a0b226449fd4 Mon Sep 17 00:00:00 2001 From: Evan Simkowitz Date: Wed, 19 Jul 2023 23:47:30 -0700 Subject: [PATCH 07/12] remove browser tests --- .github/workflows/dev.yml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index 4ef771d..a40026e 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -45,7 +45,5 @@ jobs: # GitLab is far better because it is not downloading broken repositories. - name: Install rustwasm run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh - - name: Build and test for WASM - Chrome - run: wasm-pack test --headless --chrome --all-features - - name: Build and test for WASM - Firefox - run: wasm-pack test --headless --firefox --all-features + - name: Build and test for WASM + run: wasm-pack test --all-features From 31237e4f3c457a9e12c07f2e8640fabce84b82aa Mon Sep 17 00:00:00 2001 From: Evan Simkowitz Date: Thu, 20 Jul 2023 00:13:29 -0700 Subject: [PATCH 08/12] Update to run in browser --- .github/workflows/dev.yml | 14 +++++++++----- src/binary_search.rs | 2 ++ src/lib.rs | 2 ++ src/posix_tz/parser.rs | 2 ++ src/system.rs | 2 ++ 5 files changed, 17 insertions(+), 5 deletions(-) diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index a40026e..3f4fef3 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -37,13 +37,17 @@ jobs: - name: Build and test run: cargo test --all-features build-test-wasm: - runs-on: ubuntu-latest + runs-on: macos-latest steps: - uses: actions/checkout@v2 with: - submodules: recursive # garbage peace of shit mother fucking github downloading broken repositories!!!!! - # GitLab is far better because it is not downloading broken repositories. + submodules: recursive - name: Install rustwasm run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh - - name: Build and test for WASM - run: wasm-pack test --all-features + - name: Build and test for WASM - Chrome + run: wasm-pack test --headless --chrome --all-features + - name: Build and test for WASM - Firefox + run: wasm-pack test --headless --firefox --all-features + - name: Build and test for WASM - Safari + run: wasm-pack test --headless --safari --all-features + diff --git a/src/binary_search.rs b/src/binary_search.rs index c699380..80b1e7f 100644 --- a/src/binary_search.rs +++ b/src/binary_search.rs @@ -45,6 +45,8 @@ pub fn binary_search Ordering>(start: usize, end: usize, cmp: F) mod tests { #[cfg(target_family = "wasm")] use wasm_bindgen_test::*; + #[cfg(target_family = "wasm")] + wasm_bindgen_test_configure!(run_in_browser); #[test] #[cfg_attr(target_family = "wasm", wasm_bindgen_test)] diff --git a/src/lib.rs b/src/lib.rs index cd61cdc..089ae5a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -130,6 +130,8 @@ mod tests { #[cfg(target_family = "wasm")] use wasm_bindgen_test::*; + #[cfg(target_family = "wasm")] + wasm_bindgen_test_configure!(run_in_browser); #[test] #[cfg_attr(target_family = "wasm", wasm_bindgen_test)] diff --git a/src/posix_tz/parser.rs b/src/posix_tz/parser.rs index 8b3f97f..574373d 100644 --- a/src/posix_tz/parser.rs +++ b/src/posix_tz/parser.rs @@ -191,6 +191,8 @@ mod tests { #[cfg(target_family = "wasm")] use wasm_bindgen_test::*; + #[cfg(target_family = "wasm")] + wasm_bindgen_test_configure!(run_in_browser); #[test] #[cfg_attr(target_family = "wasm", wasm_bindgen_test)] diff --git a/src/system.rs b/src/system.rs index 27d7d2b..1416c92 100644 --- a/src/system.rs +++ b/src/system.rs @@ -129,6 +129,8 @@ pub fn get_timezone() -> Result<&'static Tz, Error> { mod tests { #[cfg(target_family = "wasm")] use wasm_bindgen_test::*; + #[cfg(target_family = "wasm")] + wasm_bindgen_test_configure!(run_in_browser); #[test] #[cfg_attr(target_family = "wasm", wasm_bindgen_test)] From a59e5bf3545707460cf674def87e9428bd18be86 Mon Sep 17 00:00:00 2001 From: Evan Simkowitz Date: Thu, 20 Jul 2023 00:41:55 -0700 Subject: [PATCH 09/12] Add wrapper module to configure browser tests --- .cargo/config.toml | 2 -- src/binary_search.rs | 4 +--- src/lib.rs | 11 ++++++++--- src/posix_tz/parser.rs | 4 +--- src/system.rs | 4 +--- 5 files changed, 11 insertions(+), 14 deletions(-) delete mode 100644 .cargo/config.toml diff --git a/.cargo/config.toml b/.cargo/config.toml deleted file mode 100644 index 4ec2f3b..0000000 --- a/.cargo/config.toml +++ /dev/null @@ -1,2 +0,0 @@ -[target.wasm32-unknown-unknown] -runner = 'wasm-bindgen-test-runner' diff --git a/src/binary_search.rs b/src/binary_search.rs index 80b1e7f..2709fbc 100644 --- a/src/binary_search.rs +++ b/src/binary_search.rs @@ -44,9 +44,7 @@ pub fn binary_search Ordering>(start: usize, end: usize, cmp: F) #[cfg(test)] mod tests { #[cfg(target_family = "wasm")] - use wasm_bindgen_test::*; - #[cfg(target_family = "wasm")] - wasm_bindgen_test_configure!(run_in_browser); + use crate::wasm_bindgen_test_wrapper::*; #[test] #[cfg_attr(target_family = "wasm", wasm_bindgen_test)] diff --git a/src/lib.rs b/src/lib.rs index 089ae5a..af1b245 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -118,6 +118,13 @@ pub mod posix_tz; #[cfg(feature = "db")] pub use timezone_impl::Tz; +#[cfg(all(target_family = "wasm", test))] +pub mod wasm_bindgen_test_wrapper { + //! This module is used to wrap wasm_bindgen_test to configure testing in browser. + pub use wasm_bindgen_test::*; + wasm_bindgen_test_configure!(run_in_browser); +} + #[cfg(test)] mod tests { use crate::timezones; @@ -129,9 +136,7 @@ mod tests { use time::OffsetDateTime; #[cfg(target_family = "wasm")] - use wasm_bindgen_test::*; - #[cfg(target_family = "wasm")] - wasm_bindgen_test_configure!(run_in_browser); + use crate::wasm_bindgen_test_wrapper::*; #[test] #[cfg_attr(target_family = "wasm", wasm_bindgen_test)] diff --git a/src/posix_tz/parser.rs b/src/posix_tz/parser.rs index 574373d..42cc6a1 100644 --- a/src/posix_tz/parser.rs +++ b/src/posix_tz/parser.rs @@ -190,9 +190,7 @@ mod tests { use crate::posix_tz::parser::{entry, Date, Dst, Offset, Rule, Std, Time, Tz}; #[cfg(target_family = "wasm")] - use wasm_bindgen_test::*; - #[cfg(target_family = "wasm")] - wasm_bindgen_test_configure!(run_in_browser); + use crate::wasm_bindgen_test_wrapper::*; #[test] #[cfg_attr(target_family = "wasm", wasm_bindgen_test)] diff --git a/src/system.rs b/src/system.rs index 1416c92..8c7c277 100644 --- a/src/system.rs +++ b/src/system.rs @@ -128,9 +128,7 @@ pub fn get_timezone() -> Result<&'static Tz, Error> { #[cfg(test)] mod tests { #[cfg(target_family = "wasm")] - use wasm_bindgen_test::*; - #[cfg(target_family = "wasm")] - wasm_bindgen_test_configure!(run_in_browser); + use crate::wasm_bindgen_test_wrapper::*; #[test] #[cfg_attr(target_family = "wasm", wasm_bindgen_test)] From eba0ba5f2ecc1e623192b4e9e0ca4154df09da31 Mon Sep 17 00:00:00 2001 From: Evan Simkowitz Date: Thu, 20 Jul 2023 22:49:05 -0700 Subject: [PATCH 10/12] removing test changes --- .github/workflows/dev.yml | 15 --------------- src/binary_search.rs | 4 ---- src/lib.rs | 19 ------------------- src/posix_tz/parser.rs | 4 ---- src/system.rs | 4 ---- 5 files changed, 46 deletions(-) diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index 3f4fef3..b160b4a 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -36,18 +36,3 @@ jobs: # GitLab is far better because it is not downloading broken repositories. - name: Build and test run: cargo test --all-features - build-test-wasm: - runs-on: macos-latest - steps: - - uses: actions/checkout@v2 - with: - submodules: recursive - - name: Install rustwasm - run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh - - name: Build and test for WASM - Chrome - run: wasm-pack test --headless --chrome --all-features - - name: Build and test for WASM - Firefox - run: wasm-pack test --headless --firefox --all-features - - name: Build and test for WASM - Safari - run: wasm-pack test --headless --safari --all-features - diff --git a/src/binary_search.rs b/src/binary_search.rs index 2709fbc..10de82f 100644 --- a/src/binary_search.rs +++ b/src/binary_search.rs @@ -43,11 +43,7 @@ pub fn binary_search Ordering>(start: usize, end: usize, cmp: F) #[cfg(test)] mod tests { - #[cfg(target_family = "wasm")] - use crate::wasm_bindgen_test_wrapper::*; - #[test] - #[cfg_attr(target_family = "wasm", wasm_bindgen_test)] fn test_binary_search() { assert_eq!(super::binary_search(0, 8, |x| x.cmp(&6)), Some(6)); assert_eq!(super::binary_search(0, 5000, |x| x.cmp(&1337)), Some(1337)); diff --git a/src/lib.rs b/src/lib.rs index af1b245..5a01231 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -118,13 +118,6 @@ pub mod posix_tz; #[cfg(feature = "db")] pub use timezone_impl::Tz; -#[cfg(all(target_family = "wasm", test))] -pub mod wasm_bindgen_test_wrapper { - //! This module is used to wrap wasm_bindgen_test to configure testing in browser. - pub use wasm_bindgen_test::*; - wasm_bindgen_test_configure!(run_in_browser); -} - #[cfg(test)] mod tests { use crate::timezones; @@ -135,11 +128,7 @@ mod tests { use time::macros::{datetime, offset}; use time::OffsetDateTime; - #[cfg(target_family = "wasm")] - use crate::wasm_bindgen_test_wrapper::*; - #[test] - #[cfg_attr(target_family = "wasm", wasm_bindgen_test)] fn names() { //This test verifies that windows timezone names work fine. let shanghai = timezones::get_by_name("Asia/Shanghai"); @@ -150,7 +139,6 @@ mod tests { } #[test] - #[cfg_attr(target_family = "wasm", wasm_bindgen_test)] fn find() { let zones_iana = timezones::find_by_name("Asia"); //let zones_win = timezones::find_by_name("China Standard Time"); @@ -159,7 +147,6 @@ mod tests { } #[test] - #[cfg_attr(target_family = "wasm", wasm_bindgen_test)] fn offsets_and_name() { let tz = timezones::db::europe::LONDON; assert_eq!(tz.name(), "Europe/London"); @@ -168,7 +155,6 @@ mod tests { } #[test] - #[cfg_attr(target_family = "wasm", wasm_bindgen_test)] fn london_to_berlin() { let dt = datetime!(2016-10-8 17:0:0).assume_timezone_utc(timezones::db::europe::LONDON); let converted = dt.to_timezone(timezones::db::europe::BERLIN); @@ -178,7 +164,6 @@ mod tests { } #[test] - #[cfg_attr(target_family = "wasm", wasm_bindgen_test)] fn dst() { let london = timezones::db::europe::LONDON; let odt1 = datetime!(2021-01-01 12:0:0 UTC); @@ -193,7 +178,6 @@ mod tests { } #[test] - #[cfg_attr(target_family = "wasm", wasm_bindgen_test)] fn handles_forward_changeover() { assert_eq!( datetime!(2022-03-27 01:30) @@ -204,7 +188,6 @@ mod tests { } #[test] - #[cfg_attr(target_family = "wasm", wasm_bindgen_test)] fn handles_after_changeover() { assert_eq!( datetime!(2022-03-27 03:30) @@ -215,7 +198,6 @@ mod tests { } #[test] - #[cfg_attr(target_family = "wasm", wasm_bindgen_test)] fn handles_broken_time() { assert!(datetime!(2022-03-27 02:30) .assume_timezone(timezones::db::CET) @@ -223,7 +205,6 @@ mod tests { } #[test] - #[cfg_attr(target_family = "wasm", wasm_bindgen_test)] fn handles_backward_changeover() { // During backward changeover, the hour between 02:00 and 03:00 occurs twice, so either answer is correct assert_eq!( diff --git a/src/posix_tz/parser.rs b/src/posix_tz/parser.rs index 42cc6a1..580ca29 100644 --- a/src/posix_tz/parser.rs +++ b/src/posix_tz/parser.rs @@ -189,11 +189,7 @@ pub fn entry(input: &str) -> Result { mod tests { use crate::posix_tz::parser::{entry, Date, Dst, Offset, Rule, Std, Time, Tz}; - #[cfg(target_family = "wasm")] - use crate::wasm_bindgen_test_wrapper::*; - #[test] - #[cfg_attr(target_family = "wasm", wasm_bindgen_test)] fn basic() { let str = "ABC+1:00DEF,M1.2.3/4,56"; let (_, test) = entry(str).unwrap(); diff --git a/src/system.rs b/src/system.rs index 8c7c277..e996edb 100644 --- a/src/system.rs +++ b/src/system.rs @@ -127,11 +127,7 @@ pub fn get_timezone() -> Result<&'static Tz, Error> { #[cfg(test)] mod tests { - #[cfg(target_family = "wasm")] - use crate::wasm_bindgen_test_wrapper::*; - #[test] - #[cfg_attr(target_family = "wasm", wasm_bindgen_test)] fn get_timezone() { let tz = super::get_timezone(); assert!(tz.is_ok()); From 64049b795ab3470326a8eaaef7afff2bcaee30b9 Mon Sep 17 00:00:00 2001 From: Evan Simkowitz Date: Fri, 21 Jul 2023 09:43:47 -0700 Subject: [PATCH 11/12] remove wasm-bindgen-test dependency for now --- Cargo.toml | 3 --- 1 file changed, 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e59f978..fef04db 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,9 +28,6 @@ js-sys = { version = "0.3.64", optional = true } time = { version = "0.3.7", features = ["macros", "wasm-bindgen"] } wasm-bindgen = "0.2.87" -[target.'cfg(target_family = "wasm")'.dev-dependencies] -wasm-bindgen-test = "0.3.37" - [target.'cfg(windows)'.dependencies] windows-sys = { version = "0.32.0", features = ["Win32_System_Time", "Win32_Foundation"], optional = true } From 9bad169699e6f0af98e1a2550e4cb991cade858d Mon Sep 17 00:00:00 2001 From: Evan Simkowitz Date: Fri, 21 Jul 2023 09:59:59 -0700 Subject: [PATCH 12/12] improve error handling --- src/system.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/system.rs b/src/system.rs index e996edb..4945c6b 100644 --- a/src/system.rs +++ b/src/system.rs @@ -113,9 +113,9 @@ pub fn get_timezone() -> Result<&'static Tz, Error> { .resolved_options(); let tz = Reflect::get(&options, &JsValue::from("timeZone")) - .expect("Cannot get timeZone") + .map_err(|_| Error::Undetermined)? .as_string() - .expect("timeZone is not a String"); + .ok_or(Error::Unicode)?; let tz = get_by_name(&tz).ok_or(Error::Unknown)?; Ok(tz)