@@ -3,6 +3,8 @@ use std::io::{self, Read, Write};
33use flate2:: read:: ZlibDecoder ;
44use flate2:: write:: ZlibEncoder ;
55use flate2:: Compression ;
6+ #[ allow( unused_imports) ]
7+ use wasm_bindgen:: prelude:: wasm_bindgen;
68
79/// The bit pattern for the base 'A' (00).
810pub 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) ]
2932pub 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}
0 commit comments