40 lines
980 B
Rust
40 lines
980 B
Rust
|
use upend_base::{
|
||
|
addressing::{Address, AddressComponents},
|
||
|
error::UpEndError,
|
||
|
};
|
||
|
use wasm_bindgen::prelude::*;
|
||
|
|
||
|
#[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 {}
|
||
|
|
||
|
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())
|
||
|
}
|