From 9e0f8a91b521a0181ca1362e00992745650b79a4 Mon Sep 17 00:00:00 2001 From: dogukan Date: Fri, 31 Jan 2025 20:14:50 +0300 Subject: [PATCH] refactor string ops module --- wasm/src/lib.rs | 10 +-- wasm/src/string_ops/basic.rs | 29 +++++++ wasm/src/string_ops/encoding.rs | 69 +++++++++++++++++ wasm/src/string_ops/hash.rs | 32 ++++++++ wasm/src/string_ops/mod.rs | 129 +------------------------------- wasm/src/utils.rs | 8 -- 6 files changed, 136 insertions(+), 141 deletions(-) create mode 100644 wasm/src/string_ops/basic.rs create mode 100644 wasm/src/string_ops/encoding.rs create mode 100644 wasm/src/string_ops/hash.rs diff --git a/wasm/src/lib.rs b/wasm/src/lib.rs index 4e493df..d6a9f55 100644 --- a/wasm/src/lib.rs +++ b/wasm/src/lib.rs @@ -3,12 +3,8 @@ mod image_ops; mod string_ops; use utils::set_panic_hook; use wasm_bindgen::prelude::*; -#[wasm_bindgen] -extern "C" { - fn alert(s: &str); -} -#[wasm_bindgen] -pub fn greet() { - alert("Hello, wasm!"); +#[wasm_bindgen(start)] +fn start() { + set_panic_hook(); } diff --git a/wasm/src/string_ops/basic.rs b/wasm/src/string_ops/basic.rs new file mode 100644 index 0000000..bb6e2a0 --- /dev/null +++ b/wasm/src/string_ops/basic.rs @@ -0,0 +1,29 @@ +use wasm_bindgen::prelude::*; +use crate::utils::process_lines; + +#[wasm_bindgen] +pub fn reverse_string(s: &str) -> String { + process_lines(s, |line| line.chars().rev().collect()) +} + +#[wasm_bindgen] +pub fn count_vowels(s: &str) -> u32 { + s.chars() + .filter(|c| "aeiou".contains(*c)) + .count() as u32 +} + +#[wasm_bindgen] +pub fn lowercase_string(s: &str) -> String { + process_lines(s, |line| line.to_ascii_lowercase()) +} + +#[wasm_bindgen] +pub fn uppercase_string(s: &str) -> String { + process_lines(s, |line| line.to_ascii_uppercase()) +} + +#[wasm_bindgen] +pub fn remove_whitespace(s: &str) -> String { + process_lines(s, |line| line.chars().filter(|c| !c.is_whitespace()).collect()) +} \ No newline at end of file diff --git a/wasm/src/string_ops/encoding.rs b/wasm/src/string_ops/encoding.rs new file mode 100644 index 0000000..8cdd32d --- /dev/null +++ b/wasm/src/string_ops/encoding.rs @@ -0,0 +1,69 @@ +use wasm_bindgen::prelude::*; +use base64::{Engine as _, engine::general_purpose}; +use data_encoding::{HEXLOWER as BASE16, BASE32}; +use base85::{encode, decode}; +use crate::utils::process_lines; + +#[wasm_bindgen] +pub fn encode_base16(s: &str) -> String { + process_lines(s, |line| BASE16.encode(line.as_bytes())) +} + +#[wasm_bindgen] +pub fn decode_base16(s: &str) -> String { + process_lines(s, |line| { + BASE16.decode(line.as_bytes()) + .map_or(String::new(), |v| String::from_utf8_lossy(&v).into_owned()) + }) +} + +#[wasm_bindgen] +pub fn encode_base32(s: &str) -> String { + process_lines(s, |line| BASE32.encode(line.as_bytes())) +} + +#[wasm_bindgen] +pub fn decode_base32(s: &str) -> String { + process_lines(s, |line| { + BASE32.decode(line.as_bytes()) + .map_or(String::new(), |v| String::from_utf8_lossy(&v).into_owned()) + }) +} + +#[wasm_bindgen] +pub fn encode_base85(s: &str) -> String { + process_lines(s, |line| encode(line.as_bytes())) +} + +#[wasm_bindgen] +pub fn decode_base85(s: &str) -> String { + process_lines(s, |line| { + decode(line).map_or(String::new(), |v| String::from_utf8_lossy(&v).into_owned()) + }) +} + +#[wasm_bindgen] +pub fn encode_base64_standard(s: &str) -> String { + process_lines(s, |line| general_purpose::STANDARD.encode(line.as_bytes())) +} + +#[wasm_bindgen] +pub fn decode_base64_standard(s: &str) -> String { + process_lines(s, |line| { + general_purpose::STANDARD.decode(line) + .map_or(String::new(), |v| String::from_utf8_lossy(&v).into_owned()) + }) +} + +#[wasm_bindgen] +pub fn encode_base64_url(s: &str) -> String { + process_lines(s, |line| general_purpose::URL_SAFE.encode(line.as_bytes())) +} + +#[wasm_bindgen] +pub fn decode_base64_url(s: &str) -> String { + process_lines(s, |line| { + general_purpose::URL_SAFE.decode(line) + .map_or(String::new(), |v| String::from_utf8_lossy(&v).into_owned()) + }) +} \ No newline at end of file diff --git a/wasm/src/string_ops/hash.rs b/wasm/src/string_ops/hash.rs new file mode 100644 index 0000000..86d126e --- /dev/null +++ b/wasm/src/string_ops/hash.rs @@ -0,0 +1,32 @@ +use wasm_bindgen::prelude::*; +use md5::{Md5, Digest}; +use sha1::Sha1; +use sha2::Sha256; +use crate::utils::process_lines; + +#[wasm_bindgen] +pub fn calculate_md5(s: &str) -> String { + process_lines(s, |line| { + let mut hasher = Md5::new(); + hasher.update(line.as_bytes()); + format!("{:x}", hasher.finalize()) + }) +} + +#[wasm_bindgen] +pub fn calculate_sha1(s: &str) -> String { + process_lines(s, |line| { + let mut hasher = Sha1::new(); + hasher.update(line.as_bytes()); + format!("{:x}", hasher.finalize()) + }) +} + +#[wasm_bindgen] +pub fn calculate_sha256(s: &str) -> String { + process_lines(s, |line| { + let mut hasher = Sha256::new(); + hasher.update(line.as_bytes()); + format!("{:x}", hasher.finalize()) + }) +} \ No newline at end of file diff --git a/wasm/src/string_ops/mod.rs b/wasm/src/string_ops/mod.rs index 74d5b83..6208860 100644 --- a/wasm/src/string_ops/mod.rs +++ b/wasm/src/string_ops/mod.rs @@ -1,126 +1,3 @@ -use wasm_bindgen::prelude::*; -use base64::{Engine as _, engine::general_purpose}; -use data_encoding::{HEXLOWER as BASE16, BASE32}; -use base85::{encode, decode}; -use crate::utils::process_lines; -use md5::{Md5, Digest as Md5Digest}; -use sha1::{Sha1, Digest as Sha1Digest}; -use sha2::{Sha256, Digest as Sha256Digest}; - -#[wasm_bindgen] -pub fn reverse_string(s: &str) -> String { - process_lines(s, |line| line.chars().rev().collect()) -} - -#[wasm_bindgen] -pub fn count_vowels(s: &str) -> u32 { - s.chars() - .filter(|c| "aeiou".contains(*c)) - .count() as u32 -} - -#[wasm_bindgen] -pub fn lowercase_string(s: &str) -> String { - process_lines(s, |line| line.to_ascii_lowercase()) -} - -#[wasm_bindgen] -pub fn uppercase_string(s: &str) -> String { - process_lines(s, |line| line.to_ascii_uppercase()) -} - -#[wasm_bindgen] -pub fn remove_whitespace(s: &str) -> String { - process_lines(s, |line| line.chars().filter(|c| !c.is_whitespace()).collect()) -} - -#[wasm_bindgen] -pub fn encode_base16(s: &str) -> String { - process_lines(s, |line| BASE16.encode(line.as_bytes())) -} - -#[wasm_bindgen] -pub fn decode_base16(s: &str) -> String { - process_lines(s, |line| { - BASE16.decode(line.as_bytes()) - .map_or(String::new(), |v| String::from_utf8_lossy(&v).into_owned()) - }) -} - -#[wasm_bindgen] -pub fn encode_base32(s: &str) -> String { - process_lines(s, |line| BASE32.encode(line.as_bytes())) -} - -#[wasm_bindgen] -pub fn decode_base32(s: &str) -> String { - process_lines(s, |line| { - BASE32.decode(line.as_bytes()) - .map_or(String::new(), |v| String::from_utf8_lossy(&v).into_owned()) - }) -} - -#[wasm_bindgen] -pub fn encode_base85(s: &str) -> String { - process_lines(s, |line| encode(line.as_bytes())) -} - -#[wasm_bindgen] -pub fn decode_base85(s: &str) -> String { - process_lines(s, |line| { - decode(line).map_or(String::new(), |v| String::from_utf8_lossy(&v).into_owned()) - }) -} - -#[wasm_bindgen] -pub fn encode_base64_standard(s: &str) -> String { - process_lines(s, |line| general_purpose::STANDARD.encode(line.as_bytes())) -} - -#[wasm_bindgen] -pub fn decode_base64_standard(s: &str) -> String { - process_lines(s, |line| { - general_purpose::STANDARD.decode(line) - .map_or(String::new(), |v| String::from_utf8_lossy(&v).into_owned()) - }) -} - -#[wasm_bindgen] -pub fn encode_base64_url(s: &str) -> String { - process_lines(s, |line| general_purpose::URL_SAFE.encode(line.as_bytes())) -} - -#[wasm_bindgen] -pub fn decode_base64_url(s: &str) -> String { - process_lines(s, |line| { - general_purpose::URL_SAFE.decode(line) - .map_or(String::new(), |v| String::from_utf8_lossy(&v).into_owned()) - }) -} - -#[wasm_bindgen] -pub fn calculate_md5(s: &str) -> String { - process_lines(s, |line| { - let mut hasher = Md5::new(); - hasher.update(line.as_bytes()); - format!("{:x}", hasher.finalize()) - }) -} - -#[wasm_bindgen] -pub fn calculate_sha1(s: &str) -> String { - process_lines(s, |line| { - let mut hasher = Sha1::new(); - hasher.update(line.as_bytes()); - format!("{:x}", hasher.finalize()) - }) -} - -#[wasm_bindgen] -pub fn calculate_sha256(s: &str) -> String { - process_lines(s, |line| { - let mut hasher = Sha256::new(); - hasher.update(line.as_bytes()); - format!("{:x}", hasher.finalize()) - }) -} \ No newline at end of file +mod basic; +mod encoding; +mod hash; \ No newline at end of file diff --git a/wasm/src/utils.rs b/wasm/src/utils.rs index 494640d..ddfae18 100644 --- a/wasm/src/utils.rs +++ b/wasm/src/utils.rs @@ -1,12 +1,4 @@ -use wasm_bindgen::prelude::*; - pub fn set_panic_hook() { - // When the `console_error_panic_hook` feature is enabled, we can call the - // `set_panic_hook` function at least once during initialization, and then - // we will get better error messages if our code ever panics. - // - // For more details see - // https://github.com/rustwasm/console_error_panic_hook#readme #[cfg(feature = "console_error_panic_hook")] console_error_panic_hook::set_once(); }