-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4472f34
commit a2e3c7d
Showing
42 changed files
with
3,243 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
[package] | ||
name = "data-transformer" | ||
version = "1.0.0" | ||
edition.workspace = true | ||
|
||
[dependencies] | ||
anyhow.workspace = true | ||
serde_json.workspace = true | ||
serde.workspace = true | ||
starknet.workspace = true | ||
cairo-lang-utils.workspace = true | ||
cairo-lang-parser.workspace = true | ||
cairo-lang-syntax.workspace = true | ||
cairo-lang-diagnostics.workspace = true | ||
cairo-lang-filesystem.workspace = true | ||
itertools.workspace = true | ||
num-traits.workspace = true | ||
num-bigint.workspace = true | ||
test-case.workspace = true | ||
thiserror.workspace = true | ||
conversions = { path = "../conversions" } | ||
cairo-serde-macros = { path = "../conversions/cairo-serde-macros" } | ||
|
||
[dev-dependencies] | ||
indoc.workspace = true | ||
primitive-types.workspace = true | ||
tokio.workspace = true | ||
url.workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use cairo_serde_macros::{CairoDeserialize, CairoSerialize}; | ||
use starknet::core::types::{Felt, FromStrError}; | ||
use std::str::FromStr; | ||
|
||
#[derive(CairoDeserialize, CairoSerialize, Debug, Clone, Copy, PartialEq, Eq)] | ||
pub struct CairoBytes31 { | ||
inner: Felt, | ||
} | ||
|
||
impl CairoBytes31 { | ||
pub const MAX: CairoBytes31 = CairoBytes31 { | ||
inner: Felt::from_hex_unchecked( | ||
"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", | ||
), | ||
}; | ||
} | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)] | ||
pub enum ParseBytes31Error { | ||
#[error("Failed to parse as Cairo type")] | ||
CairoFromStrError, // `FromStrError` thrown on unsuccessful Felt parsing is useless. We cannot write anything beyond that | ||
#[error("Number is too large to fit in 31 bytes")] | ||
Overflow, | ||
} | ||
|
||
impl From<FromStrError> for ParseBytes31Error { | ||
fn from(_value: FromStrError) -> Self { | ||
ParseBytes31Error::CairoFromStrError | ||
} | ||
} | ||
|
||
impl FromStr for CairoBytes31 { | ||
type Err = ParseBytes31Error; | ||
|
||
fn from_str(input: &str) -> Result<Self, Self::Err> { | ||
let inner = input.parse::<Felt>()?; | ||
|
||
if inner > CairoBytes31::MAX.inner { | ||
return Err(ParseBytes31Error::Overflow); | ||
} | ||
|
||
Ok(CairoBytes31 { inner }) | ||
} | ||
} | ||
|
||
impl From<CairoBytes31> for Felt { | ||
fn from(value: CairoBytes31) -> Self { | ||
value.inner | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use num_bigint::BigUint; | ||
use thiserror; | ||
|
||
#[derive(Clone, Debug)] | ||
pub enum RadixInput { | ||
Decimal(Box<[u8]>), | ||
Hexadecimal(Box<[u8]>), | ||
} | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)] | ||
pub enum ParseRadixError { | ||
#[error("Input contains invalid digit")] | ||
InvalidString, | ||
#[error(transparent)] | ||
ParseIntError(#[from] std::num::ParseIntError), | ||
} | ||
|
||
impl<'input> TryFrom<&'input [u8]> for RadixInput { | ||
type Error = ParseRadixError; | ||
|
||
fn try_from(bytes: &'input [u8]) -> Result<Self, Self::Error> { | ||
let mut is_hex = false; | ||
|
||
if bytes.len() > 2 { | ||
is_hex = bytes[1] == b'x'; | ||
} | ||
|
||
let result = bytes | ||
.iter() | ||
.skip_while(|&&byte| byte == b'0' || byte == b'x') | ||
.filter(|&&byte| byte != b'_') | ||
.map(|&byte| { | ||
if byte.is_ascii_digit() { | ||
Ok(byte - b'0') | ||
} else if (b'a'..b'g').contains(&byte) { | ||
is_hex = true; | ||
Ok(byte - b'a' + 10) | ||
} else if (b'A'..b'G').contains(&byte) { | ||
is_hex = true; | ||
Ok(byte - b'A' + 10) | ||
} else { | ||
return Err(ParseRadixError::InvalidString); | ||
} | ||
}) | ||
.collect::<Result<Box<[u8]>, ParseRadixError>>()?; | ||
|
||
Ok(if is_hex { | ||
Self::Hexadecimal(result) | ||
} else { | ||
Self::Decimal(result) | ||
}) | ||
} | ||
} | ||
|
||
impl TryFrom<RadixInput> for BigUint { | ||
type Error = ParseRadixError; | ||
|
||
fn try_from(value: RadixInput) -> Result<Self, Self::Error> { | ||
match value { | ||
RadixInput::Decimal(digits) => { | ||
BigUint::from_radix_be(&digits, 10).ok_or(ParseRadixError::InvalidString) | ||
} | ||
RadixInput::Hexadecimal(digits) => { | ||
BigUint::from_radix_be(&digits, 16).ok_or(ParseRadixError::InvalidString) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
mod bytes31; | ||
mod helpers; | ||
mod u256; | ||
mod u384; | ||
mod u512; | ||
mod u96; | ||
|
||
pub use bytes31::{CairoBytes31, ParseBytes31Error}; | ||
pub use u256::{CairoU256, ParseCairoU256Error}; | ||
pub use u384::{CairoU384, ParseCairoU384Error}; | ||
pub use u512::{CairoU512, ParseCairoU512Error}; | ||
pub use u96::{CairoU96, ParseCairoU96Error}; |
Oops, something went wrong.