|
| 1 | +// Copyright (C) 2019-2025 Provable Inc. |
| 2 | +// This file is part of the Provable SDK library. |
| 3 | + |
| 4 | +// The Provable SDK library is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | + |
| 9 | +// The Provable SDK library is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | + |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with the Provable SDK library. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +use crate::{ |
| 18 | + from_js_typed_array, |
| 19 | + to_bits_array_le, |
| 20 | + types::native::*, |
| 21 | +}; |
| 22 | +use js_sys::{Array, Uint8Array}; |
| 23 | +use once_cell::sync::OnceCell; |
| 24 | +use std::{ops::Deref, str::FromStr}; |
| 25 | +use wasm_bindgen::prelude::*; |
| 26 | + |
| 27 | +macro_rules! impl_integer { |
| 28 | + ($name:ident, $native:ty) => { |
| 29 | + #[wasm_bindgen] |
| 30 | + #[derive(Clone, Debug, Eq, PartialEq)] |
| 31 | + pub struct $name(pub $native); |
| 32 | + |
| 33 | + #[wasm_bindgen] |
| 34 | + impl $name { |
| 35 | + /// Creates from string. |
| 36 | + #[wasm_bindgen(js_name = "fromString")] |
| 37 | + pub fn from_string(s: &str) -> Result<$name, String> { |
| 38 | + Ok(Self(<$native>::from_str(s).map_err(|e| e.to_string())?)) |
| 39 | + } |
| 40 | + |
| 41 | + /// To string. |
| 42 | + #[wasm_bindgen(js_name = "toString")] |
| 43 | + pub fn to_string(&self) -> String { |
| 44 | + self.0.to_string() |
| 45 | + } |
| 46 | + |
| 47 | + /// From bytes (LE). |
| 48 | + #[wasm_bindgen(js_name = "fromBytesLe")] |
| 49 | + pub fn from_bytes_le(bytes: &Uint8Array) -> Result<$name, String> { |
| 50 | + let bytes = bytes.to_vec(); |
| 51 | + let val = <$native>::from_bytes_le(&bytes).map_err(|e| e.to_string())?; |
| 52 | + Ok(Self(val)) |
| 53 | + } |
| 54 | + |
| 55 | + /// To bytes (LE). |
| 56 | + #[wasm_bindgen(js_name = "toBytesLe")] |
| 57 | + pub fn to_bytes_le(&self) -> Result<Uint8Array, String> { |
| 58 | + let bytes = self.0.to_bytes_le().map_err(|e| e.to_string())?; |
| 59 | + Ok(Uint8Array::from(bytes.as_slice())) |
| 60 | + } |
| 61 | + |
| 62 | + /// From bits. |
| 63 | + #[wasm_bindgen(js_name = "fromBitsLe")] |
| 64 | + pub fn from_bits_le(bits: &Array) -> Result<$name, String> { |
| 65 | + let bit_vec = from_js_typed_array!(bits, as_bool, "boolean")?; |
| 66 | + let val = <$native>::from_bits_le(&bit_vec).map_err(|e| e.to_string())?; |
| 67 | + Ok(Self(val)) |
| 68 | + } |
| 69 | + |
| 70 | + /// To bits. |
| 71 | + #[wasm_bindgen(js_name = "toBitsLe")] |
| 72 | + pub fn to_bits_le(&self) -> Array { |
| 73 | + to_bits_array_le!(self) |
| 74 | + } |
| 75 | + |
| 76 | + /// Clone. |
| 77 | + pub fn clone(&self) -> $name { |
| 78 | + $name(self.0) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + impl Deref for $name { |
| 83 | + type Target = $native; |
| 84 | + |
| 85 | + fn deref(&self) -> &Self::Target { |
| 86 | + &self.0 |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + impl From<$native> for $name { |
| 91 | + fn from(n: $native) -> Self { |
| 92 | + Self(n) |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + impl From<$name> for $native { |
| 97 | + fn from(n: $name) -> Self { |
| 98 | + n.0 |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + impl From<&$native> for $name { |
| 103 | + fn from(n: &$native) -> Self { |
| 104 | + Self(*n) |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + impl From<&$name> for $native { |
| 109 | + fn from(n: &$name) -> Self { |
| 110 | + n.0 |
| 111 | + } |
| 112 | + } |
| 113 | + }; |
| 114 | +} |
| 115 | + |
| 116 | +// Instantiate for all supported integer types |
| 117 | +impl_integer!(I8, I8Native); |
| 118 | +impl_integer!(I16, I16Native); |
| 119 | +impl_integer!(I32, I32Native); |
| 120 | +impl_integer!(I64, I64Native); |
| 121 | +impl_integer!(I128, I128Native); |
| 122 | + |
| 123 | +impl_integer!(U8, U8Native); |
| 124 | +impl_integer!(U16, U16Native); |
| 125 | +impl_integer!(U32, U32Native); |
| 126 | +impl_integer!(U64, U64Native); |
| 127 | +impl_integer!(U128, U128Native); |
0 commit comments