1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use wasm_bindgen::prelude::{wasm_bindgen, JsValue};

use cml_core::serialization::{Deserialize, Serialize};

pub mod metadata;
#[macro_use]
pub mod wasm_wrappers;

#[wasm_bindgen]
#[derive(Clone, Debug)]
pub struct Int(cml_core::Int);

#[wasm_bindgen]

impl Int {
    pub fn to_cbor_bytes(&self) -> Vec<u8> {
        Serialize::to_cbor_bytes(&self.0)
    }

    pub fn from_cbor_bytes(cbor_bytes: &[u8]) -> Result<Int, JsValue> {
        Deserialize::from_cbor_bytes(cbor_bytes)
            .map(Self)
            .map_err(|e| JsValue::from_str(&format!("from_bytes: {e}")))
    }

    pub fn to_json(&self) -> Result<String, JsValue> {
        serde_json::to_string_pretty(&self.0)
            .map_err(|e| JsValue::from_str(&format!("to_json: {e}")))
    }

    pub fn to_json_value(&self) -> Result<JsValue, JsValue> {
        serde_wasm_bindgen::to_value(&self.0)
            .map_err(|e| JsValue::from_str(&format!("to_js_value: {e}")))
    }

    pub fn from_json(json: &str) -> Result<Int, JsValue> {
        serde_json::from_str(json)
            .map(Self)
            .map_err(|e| JsValue::from_str(&format!("from_json: {e}")))
    }

    pub fn new(x: i64) -> Self {
        if x >= 0 {
            Self(cml_core::Int::new_uint(x as u64))
        } else {
            Self(cml_core::Int::new_nint((x + 1).unsigned_abs()))
        }
    }

    pub fn to_str(&self) -> String {
        self.0.to_string()
    }

    #[allow(clippy::should_implement_trait)]
    pub fn from_str(string: &str) -> Result<Int, JsValue> {
        // have to redefine so it's visible in WASM
        std::str::FromStr::from_str(string)
            .map(Self)
            .map_err(|e| JsValue::from_str(&format!("Int.from_str({string}): {e:?}")))
    }
}

impl From<cml_core::Int> for Int {
    fn from(native: cml_core::Int) -> Self {
        Self(native)
    }
}

impl From<Int> for cml_core::Int {
    fn from(wasm: Int) -> Self {
        wasm.0
    }
}

impl AsRef<cml_core::Int> for Int {
    fn as_ref(&self) -> &cml_core::Int {
        &self.0
    }
}