Skip to content

Commit 238eeb7

Browse files
committed
Add wasm support
1 parent 28956f2 commit 238eeb7

File tree

4 files changed

+154
-8
lines changed

4 files changed

+154
-8
lines changed

Cargo.lock

Lines changed: 130 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,13 @@ description = "Library for compressing and decompressing DNA sequences"
77
license = "MIT"
88
repository = "https://github.com/marshallku/base-sequence-compression"
99

10+
[lib]
11+
crate-type = ["cdylib", "rlib"]
12+
1013
[dependencies]
1114
flate2 = "1.0.35"
15+
wasm-bindgen = "0.2.99"
16+
17+
[target.'cfg(target_arch = "wasm32")'.dependencies]
18+
web-sys = { version = "0.3", features = ["File", "Blob"] }
19+
js-sys = "0.3"

src/lib.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use std::io::{self, Read, Write};
33
use flate2::read::ZlibDecoder;
44
use flate2::write::ZlibEncoder;
55
use flate2::Compression;
6+
#[allow(unused_imports)]
7+
use wasm_bindgen::prelude::wasm_bindgen;
68

79
/// The bit pattern for the base 'A' (00).
810
pub const A_BITS: u8 = 0b00;
@@ -26,6 +28,7 @@ pub const G_BITS: u8 = 0b11;
2628
/// # Returns
2729
///
2830
/// A vector of bytes containing the compressed DNA sequence.
31+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
2932
pub fn compress_sequence(sequence: &str) -> Vec<u8> {
3033
let mut compressed = Vec::with_capacity(sequence.len() / 4 + 1);
3134
let mut current_byte = 0u8;
@@ -166,18 +169,23 @@ pub fn compress_fasta(content: &str) -> Vec<u8> {
166169
/// # Errors
167170
///
168171
/// Returns an error if the file is too short or if the file is missing
169-
pub fn decompress_fasta(data: &[u8]) -> Result<String, String> {
172+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
173+
pub fn decompress_fasta(data: &[u8]) -> String {
170174
if data.len() < 12 {
171-
return Err("File is too short".to_string());
175+
return "".to_string();
172176
}
173177

174178
let header_len = u32::from_le_bytes(data[0..4].try_into().unwrap()) as usize;
175179

176180
if data.len() < 12 + header_len {
177-
return Err("File is too short for header".to_string());
181+
return "".to_string();
178182
}
179183

180-
let header = String::from_utf8(data[4..4 + header_len].to_vec()).map_err(|e| e.to_string())?;
184+
let header =
185+
match String::from_utf8(data[4..4 + header_len].to_vec()).map_err(|e| e.to_string()) {
186+
Ok(header) => header,
187+
Err(_) => return "".to_string(),
188+
};
181189

182190
let sequence_length =
183191
u32::from_le_bytes(data[4 + header_len..8 + header_len].try_into().unwrap()) as usize;
@@ -186,7 +194,7 @@ pub fn decompress_fasta(data: &[u8]) -> Result<String, String> {
186194
u32::from_le_bytes(data[8 + header_len..12 + header_len].try_into().unwrap()) as usize;
187195

188196
if data.len() < 12 + header_len + compressed_len {
189-
return Err("File is too short for compressed data".to_string());
197+
return "".to_string();
190198
}
191199

192200
let compressed_data = &data[12 + header_len..12 + header_len + compressed_len];
@@ -202,5 +210,5 @@ pub fn decompress_fasta(data: &[u8]) -> Result<String, String> {
202210
result.push('\n');
203211
}
204212

205-
Ok(result)
213+
result
206214
}

tests/compression.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ mod tests {
116116

117117
let content = std::fs::read_to_string(input_path).unwrap();
118118
let compressed = compress_fasta(&content);
119-
let decompressed = decompress_fasta(&compressed).unwrap();
119+
let decompressed = decompress_fasta(&compressed);
120120

121121
assert_eq!(content, decompressed);
122122
}
@@ -127,7 +127,7 @@ mod tests {
127127

128128
let content = std::fs::read_to_string(input_path).unwrap();
129129
let compressed = compress_fasta(&content);
130-
let decompressed = decompress_fasta(&compressed).unwrap();
130+
let decompressed = decompress_fasta(&compressed);
131131

132132
assert_eq!(content, decompressed);
133133
}

0 commit comments

Comments
 (0)