Skip to content

Commit 311747f

Browse files
authored
Merge branch 'main' into feat/deserialize
2 parents 7b98204 + 3204933 commit 311747f

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

crates/starknet-types-core/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "starknet-types-core"
3-
version = "0.1.3"
3+
version = "0.1.4"
44
edition = "2021"
55
license = "MIT"
66
homepage = "https://github.com/starknet-io/types-rs"

crates/starknet-types-core/src/felt/mod.rs

+15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
mod felt_arbitrary;
33

44
use core::ops::{Add, Mul, Neg};
5+
use core::str::FromStr;
56

67
use num_bigint::{BigInt, BigUint, Sign};
78
use num_integer::Integer;
@@ -615,6 +616,20 @@ impl_from!(i32, i128);
615616
impl_from!(i64, i128);
616617
impl_from!(isize, i128);
617618

619+
impl FromStr for Felt {
620+
type Err = FromStrError;
621+
622+
/// Converts a hex (0x-prefixed) or decimal string to a [Felt].
623+
/// e.g., '0x123abc' or '1337'.
624+
fn from_str(s: &str) -> Result<Self, Self::Err> {
625+
if s.starts_with("0x") {
626+
Felt::from_hex(s)
627+
} else {
628+
Felt::from_dec_str(s)
629+
}
630+
}
631+
}
632+
618633
impl Add<&Felt> for u64 {
619634
type Output = Option<u64>;
620635

crates/starknet-types-core/src/felt/num_traits_impl.rs

+19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
use super::Felt;
2+
use num_bigint::{ToBigInt, ToBigUint};
23
use num_traits::{FromPrimitive, Inv, One, Pow, ToPrimitive, Zero};
34

5+
impl ToBigInt for Felt {
6+
/// Converts the value of `self` to a [`BigInt`].
7+
///
8+
/// Safe to unwrap, will always return `Some`.
9+
fn to_bigint(&self) -> Option<num_bigint::BigInt> {
10+
Some(self.to_bigint())
11+
}
12+
}
13+
14+
impl ToBigUint for Felt {
15+
/// Converts the value of `self` to a [`BigUint`].
16+
///
17+
/// Safe to unwrap, will always return `Some`.
18+
fn to_biguint(&self) -> Option<num_bigint::BigUint> {
19+
Some(self.to_biguint())
20+
}
21+
}
22+
423
impl FromPrimitive for Felt {
524
fn from_i64(value: i64) -> Option<Self> {
625
Some(value.into())

0 commit comments

Comments
 (0)