2023-06-27 21:11:29 +02:00
|
|
|
use upend_base::{
|
|
|
|
addressing::{Address, AddressComponents},
|
2023-06-28 18:36:47 +02:00
|
|
|
constants,
|
2023-06-27 21:11:29 +02:00
|
|
|
error::UpEndError,
|
|
|
|
};
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
|
2023-06-28 14:25:11 +02:00
|
|
|
#[global_allocator]
|
|
|
|
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
|
|
|
|
|
2023-06-27 21:11:29 +02:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct WasmError(String);
|
|
|
|
|
|
|
|
impl std::fmt::Display for WasmError {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "{}", self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::error::Error for WasmError {}
|
|
|
|
|
2023-06-29 15:17:06 +02:00
|
|
|
#[allow(clippy::from_over_into)]
|
2023-06-27 21:11:29 +02:00
|
|
|
impl Into<JsValue> for WasmError {
|
|
|
|
fn into(self) -> JsValue {
|
|
|
|
JsValue::from_str(&self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub fn addr_to_components(address: String) -> Result<AddressComponents, WasmError> {
|
|
|
|
let address: Address = address
|
|
|
|
.parse()
|
|
|
|
.map_err(|e: UpEndError| WasmError(e.to_string()))?;
|
|
|
|
|
|
|
|
Ok(address.as_components())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub fn components_to_addr(components: AddressComponents) -> Result<String, WasmError> {
|
|
|
|
let address =
|
|
|
|
Address::from_components(components).map_err(|e: UpEndError| WasmError(e.to_string()))?;
|
|
|
|
|
|
|
|
Ok(address.to_string())
|
|
|
|
}
|
2023-06-28 18:36:47 +02:00
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub struct AddressTypeConstants {}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
2023-06-29 15:17:06 +02:00
|
|
|
#[allow(non_snake_case, clippy::new_without_default)]
|
2023-06-28 18:36:47 +02:00
|
|
|
impl AddressTypeConstants {
|
|
|
|
#[wasm_bindgen(constructor)]
|
|
|
|
pub fn new() -> Self {
|
|
|
|
AddressTypeConstants {}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen(getter)]
|
2023-06-28 18:44:08 +02:00
|
|
|
pub fn Hash(&self) -> String {
|
2023-06-28 18:36:47 +02:00
|
|
|
constants::TYPE_HASH_ADDRESS.to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen(getter)]
|
2023-06-28 18:44:08 +02:00
|
|
|
pub fn Uuid(&self) -> String {
|
2023-06-28 18:36:47 +02:00
|
|
|
constants::TYPE_UUID_ADDRESS.to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen(getter)]
|
2023-06-28 18:44:08 +02:00
|
|
|
pub fn Attribute(&self) -> String {
|
2023-06-28 18:36:47 +02:00
|
|
|
constants::TYPE_ATTRIBUTE_ADDRESS.to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen(getter)]
|
2023-06-28 18:44:08 +02:00
|
|
|
pub fn Url(&self) -> String {
|
2023-06-28 18:36:47 +02:00
|
|
|
constants::TYPE_URL_ADDRESS.to_string()
|
|
|
|
}
|
|
|
|
}
|