#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;
#[cfg(target_arch = "wasm32")]
use js_sys;
#[cfg(target_arch = "wasm32")]
use web_sys::console;
pub fn base64_encode(data: Vec<u8>) -> String {
base64::encode(&data)
}
pub fn base64_encode_string(data: String) -> String {
base64::encode(data.as_bytes())
}
pub fn base64_decode(data: String) -> Result<Vec<u8>, String> {
base64::decode(&data).map_err(|e| e.to_string())
}
pub fn base64_decode_string(data: String) -> Result<String, String> {
let bytes = base64::decode(&data).map_err(|e| e.to_string())?;
String::from_utf8(bytes).map_err(|e| e.to_string())
}
pub fn hex_encode(data: Vec<u8>) -> String {
hex::encode(&data)
}
pub fn hex_encode_string(data: String) -> String {
hex::encode(data.as_bytes())
}
pub fn hex_decode(data: String) -> Result<Vec<u8>, String> {
hex::decode(&data).map_err(|e| e.to_string())
}
pub fn hex_decode_string(data: String) -> Result<String, String> {
let bytes = hex::decode(&data).map_err(|e| e.to_string())?;
String::from_utf8(bytes).map_err(|e| e.to_string())
}
pub fn url_encode(data: String) -> String {
#[cfg(target_arch = "wasm32")]
{
match js_sys::encode_uri_component(&data).as_string() {
Some(encoded) => encoded,
None => {
console::warn_1(&"Failed to URL encode string".into());
data
}
}
}
#[cfg(not(target_arch = "wasm32"))]
{
data
}
}
pub fn url_decode(data: String) -> Result<String, String> {
#[cfg(target_arch = "wasm32")]
{
match js_sys::decode_uri_component(&data) {
Ok(decoded) => decoded
.as_string()
.ok_or_else(|| "Failed to convert decoded value to string".to_string()),
Err(_) => Err("Failed to decode URL component".to_string()),
}
}
#[cfg(not(target_arch = "wasm32"))]
{
Ok(data)
}
}
pub fn url_encode_component(data: String) -> String {
url_encode(data)
}
pub fn url_decode_component(data: String) -> Result<String, String> {
url_decode(data)
}