Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement FromStr trait for Felt #75

Merged
merged 2 commits into from
Jun 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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> {
0xLucqs marked this conversation as resolved.
Show resolved Hide resolved
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
Loading