Skip to content

Commit

Permalink
use lzma-rs on wasm32
Browse files Browse the repository at this point in the history
  • Loading branch information
MinusKelvin committed Oct 29, 2020
1 parent 86cd709 commit 9005b89
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
5 changes: 5 additions & 0 deletions opening-book/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
21 changes: 19 additions & 2 deletions opening-book/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,27 @@ pub use builder::BookBuilder;
pub struct Book(HashMap<Position, Vec<(Sequence, Option<CompactPiece>)>>);

impl Book {
pub fn load(from: impl Read) -> Result<Self, bincode::Error> {
bincode::deserialize_from(xz2::read::XzDecoder::new(from))
#[cfg(not(target_arch = "wasm32"))]
pub fn load(from: impl BufRead) -> Result<Self, bincode::Error> {
bincode::deserialize_from(xz2::bufread::XzDecoder::new(from))
}

#[cfg(target_arch = "wasm32")]
pub fn load(from: impl BufRead) -> Result<Self, bincode::Error> {
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)?;
Expand Down

0 comments on commit 9005b89

Please sign in to comment.