Skip to content

Commit

Permalink
Implement FromStr trait for Felt (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
kariy authored Jun 25, 2024
1 parent 5ae413c commit d3d77a5
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions crates/starknet-types-core/src/felt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
mod felt_arbitrary;

use core::ops::{Add, Mul, Neg};
use core::str::FromStr;

use num_bigint::{BigInt, BigUint, Sign};
use num_integer::Integer;
Expand Down Expand Up @@ -615,6 +616,20 @@ impl_from!(i32, i128);
impl_from!(i64, i128);
impl_from!(isize, i128);

impl FromStr for Felt {
type Err = FromStrError;

/// Converts a hex (0x-prefixed) or decimal string to a [Felt].
/// e.g., '0x123abc' or '1337'.
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.starts_with("0x") {
Felt::from_hex(s)
} else {
Felt::from_dec_str(s)
}
}
}

impl Add<&Felt> for u64 {
type Output = Option<u64>;

Expand Down

0 comments on commit d3d77a5

Please sign in to comment.