Skip to main content

binky/
lib.rs

1#[cfg(feature = "napi")]
2mod napi_impls;
3
4#[cfg(feature = "wasm")]
5mod wasm_impls;
6
7#[cfg(feature = "pyo3")]
8mod pyo3_impls;
9
10#[cfg(feature = "napi")]
11pub use napi_impls::*;
12
13#[cfg(feature = "wasm")]
14pub use wasm_impls::*;
15
16#[cfg(feature = "pyo3")]
17pub use pyo3_impls::*;
18
19use std::string::FromUtf8Error;
20
21use chik_sdk_driver::{DriverError, OfferError};
22use chik_sdk_test::SimulatorError;
23use chik_sdk_utils::AddressError;
24use klvm_traits::{FromKlvmError, ToKlvmError};
25use klvmr::reduction::EvalErr;
26
27use num_bigint::{BigInt, ParseBigIntError, TryFromBigIntError};
28use thiserror::Error;
29
30#[derive(Debug, Error)]
31pub enum Error {
32    #[cfg(feature = "napi")]
33    #[error("NAPI error: {0}")]
34    Napi(#[from] napi::Error),
35
36    #[error("Wrong length, expected {expected} bytes, found {found}")]
37    WrongLength { expected: usize, found: usize },
38
39    #[error("Bech32m encoding error: {0}")]
40    Bech32(#[from] bech32::Error),
41
42    #[error("Bip39 error: {0}")]
43    Bip39(#[from] bip39::Error),
44
45    #[error("Address error: {0}")]
46    Address(#[from] AddressError),
47
48    #[error("Hex error: {0}")]
49    Hex(#[from] hex::FromHexError),
50
51    #[error("Bls error: {0}")]
52    Bls(#[from] chik_bls::Error),
53
54    #[error("Secp error: {0}")]
55    Secp(#[from] signature::Error),
56
57    #[error("Driver error: {0}")]
58    Driver(#[from] DriverError),
59
60    #[error("Offer error: {0}")]
61    Offer(#[from] OfferError),
62
63    #[error("Eval error: {0}")]
64    Eval(#[from] EvalErr),
65
66    #[error("Value is infinite")]
67    Infinite,
68
69    #[error("Value is NaN")]
70    NaN,
71
72    #[error("Value has a fractional part")]
73    Fractional,
74
75    #[error("Value is larger than MAX_SAFE_INTEGER")]
76    TooLarge,
77
78    #[error("Value is smaller than MIN_SAFE_INTEGER")]
79    TooSmall,
80
81    #[error("IO error: {0}")]
82    Io(#[from] std::io::Error),
83
84    #[error("UTF-8 error: {0}")]
85    Utf8(#[from] FromUtf8Error),
86
87    #[error("Atom expected")]
88    AtomExpected,
89
90    #[error("Pair expected")]
91    PairExpected,
92
93    #[error("To KLVM error: {0}")]
94    ToKlvm(#[from] ToKlvmError),
95
96    #[error("From KLVM error: {0}")]
97    FromKlvm(#[from] FromKlvmError),
98
99    #[error("Missing parent inner puzzle hash")]
100    MissingParentInnerPuzzleHash,
101
102    #[error("Simulator error: {0}")]
103    Simulator(#[from] SimulatorError),
104
105    #[error("BigInt parse error: {0}")]
106    BigIntParse(#[from] ParseBigIntError),
107
108    #[error("BigInt error: {0}")]
109    BigInt(#[from] TryFromBigIntError<BigInt>),
110
111    #[error("No spends")]
112    NoSpends,
113
114    #[error("{0}")]
115    Custom(String),
116
117    #[error("Reqwest error: {0}")]
118    Reqwest(#[from] reqwest::Error),
119
120    #[error("Streamable error: {0}")]
121    Streamable(#[from] chik_traits::Error),
122}
123
124pub type Result<T> = std::result::Result<T, Error>;
125
126pub trait IntoRust<T, C, L> {
127    fn into_rust(self, context: &C) -> Result<T>;
128}
129
130pub trait FromRust<T, C, L>: Sized {
131    fn from_rust(value: T, context: &C) -> Result<Self>;
132}
133
134#[macro_export]
135macro_rules! impl_self {
136    ( $ty:ty ) => {
137        impl<T, L> $crate::FromRust<$ty, T, L> for $ty {
138            fn from_rust(value: $ty, _context: &T) -> $crate::Result<Self> {
139                Ok(value)
140            }
141        }
142
143        impl<T, L> $crate::IntoRust<$ty, T, L> for $ty {
144            fn into_rust(self, _context: &T) -> $crate::Result<$ty> {
145                Ok(self)
146            }
147        }
148    };
149}
150
151impl_self!(bool);
152impl_self!(u8);
153impl_self!(i8);
154impl_self!(u16);
155impl_self!(i16);
156impl_self!(u32);
157impl_self!(i32);
158impl_self!(String);
159
160impl<R, B, C, L> IntoRust<Vec<R>, C, L> for Vec<B>
161where
162    B: IntoRust<R, C, L>,
163{
164    fn into_rust(self, context: &C) -> Result<Vec<R>> {
165        self.into_iter().map(|b| b.into_rust(context)).collect()
166    }
167}
168
169impl<R, B, C, L> FromRust<Vec<R>, C, L> for Vec<B>
170where
171    B: FromRust<R, C, L>,
172{
173    fn from_rust(value: Vec<R>, context: &C) -> Result<Self> {
174        value
175            .into_iter()
176            .map(|r| B::from_rust(r, context))
177            .collect()
178    }
179}
180
181impl<R, B, C, L> IntoRust<Option<R>, C, L> for Option<B>
182where
183    B: IntoRust<R, C, L>,
184{
185    fn into_rust(self, context: &C) -> Result<Option<R>> {
186        self.map(|b| b.into_rust(context)).transpose()
187    }
188}
189
190impl<R, B, C, L> FromRust<Option<R>, C, L> for Option<B>
191where
192    B: FromRust<R, C, L>,
193{
194    fn from_rust(value: Option<R>, context: &C) -> Result<Self> {
195        value.map(|r| B::from_rust(r, context)).transpose()
196    }
197}