Skip to content

Commit

Permalink
feat: add support for signed integers (dojoengine#2161)
Browse files Browse the repository at this point in the history
* feat: add support for signed integers

* chore: run scripts/rust_fmt.sh

* feat: add support for signed integers

* chore: run scripts/rust_fmt.sh

* feat: decode signed integer

* test: test signed integers decoder

* refac: remove redundant type casting

* refac: modify signed integer decoder

* feat: implement try from felt for signed integers

* Delete scripts/prettier.sh

* chore: add comments

* fix: ensure signed integers are correctly printed

* fix: add more tests

---------

Co-authored-by: glihm <[email protected]>
  • Loading branch information
2 people authored and Larkooo committed Jul 17, 2024
1 parent 8e19183 commit 19389b8
Show file tree
Hide file tree
Showing 24 changed files with 657 additions and 56 deletions.
66 changes: 66 additions & 0 deletions bin/sozo/src/commands/calldata_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ impl CalldataDecoder for ShortStrCalldataDecoder {
}
}

/// Decodes a signed integer into a [`Felt`]
struct SignedIntegerCalldataDecoder;
impl CalldataDecoder for SignedIntegerCalldataDecoder {
fn decode(&self, input: &str) -> DecoderResult<Vec<Felt>> {
if let Ok(value) = input.parse::<i128>() {
Ok(vec![value.into()])
} else {
Err(CalldataDecoderError::ParseError("Invalid numeric string".to_string()))
}
}
}

/// Decodes a string into a [`Felt`], either from hexadecimal or decimal string.
struct DefaultCalldataDecoder;
impl CalldataDecoder for DefaultCalldataDecoder {
Expand Down Expand Up @@ -150,6 +162,7 @@ fn decode_inner(item: &str) -> DecoderResult<Vec<Felt>> {
"u256" => U256CalldataDecoder.decode(value)?,
"str" => StrCalldataDecoder.decode(value)?,
"sstr" => ShortStrCalldataDecoder.decode(value)?,
"int" => SignedIntegerCalldataDecoder.decode(value)?,
_ => DefaultCalldataDecoder.decode(item)?,
}
} else {
Expand Down Expand Up @@ -236,6 +249,51 @@ mod tests {
assert_eq!(result, expected);
}

#[test]
fn test_signed_integer_decoder_i8() {
let input = "-64";
let signed_i8: i8 = -64;
let expected = vec![signed_i8.into()];
let result = decode_calldata(input).unwrap();
assert_eq!(result, expected);
}

#[test]
fn test_signed_integer_decoder_i16() {
let input = "-12345";
let signed_i16: i16 = -12345;
let expected = vec![signed_i16.into()];
let result = decode_calldata(input).unwrap();
assert_eq!(result, expected);
}

#[test]
fn test_signed_integer_decoder_i32() {
let input = "-987654321";
let signed_i32: i32 = -987654321;
let expected = vec![signed_i32.into()];
let result = decode_calldata(input).unwrap();
assert_eq!(result, expected);
}

#[test]
fn test_signed_integer_decoder_i64() {
let input = "-1234567890123456789";
let signed_i64: i64 = -1234567890123456789;
let expected = vec![signed_i64.into()];
let result = decode_calldata(input).unwrap();
assert_eq!(result, expected);
}

#[test]
fn test_signed_integer_decoder_i128() {
let input = "-123456789012345678901234567890123456";
let signed_i128: i128 = -123456789012345678901234567890123456;
let expected = vec![signed_i128.into()];
let result = decode_calldata(input).unwrap();
assert_eq!(result, expected);
}

#[test]
fn test_combined_decoders() {
let input = "u256:0x64,str:world,987654,0x123";
Expand All @@ -259,4 +317,12 @@ mod tests {
let result = decode_calldata(input).unwrap();
assert_eq!(result, expected);
}

#[test]
fn test_invalid_signed_integer_decoder() {
let input = "-12345abc";
let decoder = SignedIntegerCalldataDecoder;
let result = decoder.decode(input);
assert!(result.is_err());
}
}
1 change: 1 addition & 0 deletions bin/sozo/src/commands/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub struct ExecuteArgs {
- u256: A 256-bit unsigned integer.
- sstr: A cairo short string.
- str: A cairo string (ByteArray).
- int: A signed integer.
- no prefix: A cairo felt or any type that fit into one felt.")]
pub calldata: Option<String>,

Expand Down
60 changes: 60 additions & 0 deletions crates/dojo-core/src/database/introspect.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,66 @@ impl Introspect_u256 of Introspect<u256> {
}
}

impl Introspect_i8 of Introspect<i8> {
fn size() -> Option<usize> {
Option::Some(1)
}
fn layout() -> Layout {
Layout::Fixed(array![251].span())
}
fn ty() -> Ty {
Ty::Primitive('i8')
}
}

impl Introspect_i16 of Introspect<i16> {
fn size() -> Option<usize> {
Option::Some(1)
}
fn layout() -> Layout {
Layout::Fixed(array![251].span())
}
fn ty() -> Ty {
Ty::Primitive('i16')
}
}

impl Introspect_i32 of Introspect<i32> {
fn size() -> Option<usize> {
Option::Some(1)
}
fn layout() -> Layout {
Layout::Fixed(array![251].span())
}
fn ty() -> Ty {
Ty::Primitive('i32')
}
}

impl Introspect_i64 of Introspect<i64> {
fn size() -> Option<usize> {
Option::Some(1)
}
fn layout() -> Layout {
Layout::Fixed(array![251].span())
}
fn ty() -> Ty {
Ty::Primitive('i64')
}
}

impl Introspect_i128 of Introspect<i128> {
fn size() -> Option<usize> {
Option::Some(1)
}
fn layout() -> Layout {
Layout::Fixed(array![251].span())
}
fn ty() -> Ty {
Ty::Primitive('i128')
}
}

impl Introspect_address of Introspect<starknet::ContractAddress> {
fn size() -> Option<usize> {
Option::Some(1)
Expand Down
1 change: 1 addition & 0 deletions crates/dojo-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use starknet::core::types::Felt;
pub mod event;
pub mod packing;
pub mod primitive;
pub mod primitive_conversion;
pub mod schema;
pub mod storage;
pub mod system;
Expand Down
Loading

0 comments on commit 19389b8

Please sign in to comment.