From 9005b894c4ddc54ec566ba89efaab70ca04beb56 Mon Sep 17 00:00:00 2001 From: MinusKelvin Date: Thu, 29 Oct 2020 11:24:41 +1100 Subject: [PATCH] use lzma-rs on wasm32 --- opening-book/Cargo.toml | 5 +++++ opening-book/src/lib.rs | 21 +++++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/opening-book/Cargo.toml b/opening-book/Cargo.toml index 3b1c25a..873738b 100644 --- a/opening-book/Cargo.toml +++ b/opening-book/Cargo.toml @@ -12,7 +12,12 @@ enumset = { version = "0.4.0", features = ["serde"] } serde = "1.0" bincode = "1.3.1" arrayvec = "0.5.1" + +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] xz2 = "0.1.6" +[target.'cfg(target_arch = "wasm32")'.dependencies] +lzma-rs = "0.1.3" + [features] builder = [] diff --git a/opening-book/src/lib.rs b/opening-book/src/lib.rs index da1720b..cd24cce 100644 --- a/opening-book/src/lib.rs +++ b/opening-book/src/lib.rs @@ -15,10 +15,27 @@ pub use builder::BookBuilder; pub struct Book(HashMap)>>); impl Book { - pub fn load(from: impl Read) -> Result { - bincode::deserialize_from(xz2::read::XzDecoder::new(from)) + #[cfg(not(target_arch = "wasm32"))] + pub fn load(from: impl BufRead) -> Result { + bincode::deserialize_from(xz2::bufread::XzDecoder::new(from)) } + #[cfg(target_arch = "wasm32")] + pub fn load(from: impl BufRead) -> Result { + let mut buf = vec![]; + lzma_rs::xz_decompress(&mut {from}, &mut buf).map_err(|e| match e { + lzma_rs::error::Error::IOError(e) => e.into(), + lzma_rs::error::Error::LZMAError(e) => Box::new( + bincode::ErrorKind::Custom(format!("LZMA error: {}", e)) + ), + lzma_rs::error::Error::XZError(e) => Box::new( + bincode::ErrorKind::Custom(format!("XZ error: {}", e)) + ) + })?; + bincode::deserialize_from(&*buf) + } + + #[cfg(not(target_arch = "wasm32"))] pub fn save(&self, to: impl Write) -> Result<(), bincode::Error> { let mut to = xz2::write::XzEncoder::new(to, 9); bincode::serialize_into(&mut to, self)?;