Skip to content

Commit

Permalink
BoundedInt builtin support (#76)
Browse files Browse the repository at this point in the history
* BoundedInt builtin translates as felt

* cargo fmt

* refactor: use variable for starknet rs types

---------

Co-authored-by: glihm <[email protected]>
  • Loading branch information
piniom and glihm authored Jan 1, 2025
1 parent 139f7c8 commit 129a2ec
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
3 changes: 2 additions & 1 deletion crates/parser/src/tokens/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ pub const CAIRO_CORE_BASIC: [&str; 17] = [
// to match array pattern.
pub const CAIRO_CORE_SPAN_ARRAY: [&str; 2] = ["core::array::Span", "core::array::Array"];

pub const CAIRO_GENERIC_BUILTINS: [&str; 3] = [
pub const CAIRO_GENERIC_BUILTINS: [&str; 4] = [
"core::option::Option",
"core::result::Result",
"core::zeroable::NonZero",
"core::internal::bounded_int::BoundedInt",
];

pub const CAIRO_COMPOSITE_BUILTINS: [&str; 3] = [
Expand Down
8 changes: 6 additions & 2 deletions crates/rs/src/expand/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,14 @@ impl CairoToRust for Token {

fn basic_types_to_rust(type_name: &str) -> String {
let ccsp = utils::cainome_cairo_serde_path();
let snrs_types = utils::starknet_rs_types_path();

match type_name {
"ClassHash" => format!("{ccsp}::ClassHash"),
"ContractAddress" => format!("{ccsp}::ContractAddress"),
"EthAddress" => format!("{ccsp}::EthAddress"),
"felt252" => "starknet::core::types::Felt".to_string(),
"felt" => "starknet::core::types::Felt".to_string(),
"felt252" => format!("{snrs_types}::Felt"),
"felt" => format!("{snrs_types}::Felt"),
"bytes31" => format!("{ccsp}::Bytes31"),
"ByteArray" => format!("{ccsp}::ByteArray"),
"NonZero" => format!("{ccsp}::NonZero"),
Expand All @@ -133,12 +134,15 @@ fn basic_types_to_rust(type_name: &str) -> String {

fn builtin_composite_to_rust(type_name: &str) -> (String, bool) {
let ccsp = utils::cainome_cairo_serde_path();
let snrs_types = utils::starknet_rs_types_path();

match type_name {
"EthAddress" => (format!("{ccsp}::EthAddress"), true),
"ByteArray" => (format!("{ccsp}::ByteArray"), true),
"NonZero" => (format!("{ccsp}::NonZero"), true),
"U256" => (format!("{ccsp}::U256"), true),
// <https://github.com/starkware-libs/cairo/blob/35b299291fd7819f75409fb303ece7d30e4adb19/corelib/src/internal/bounded_int.cairo#L5>
"BoundedInt" => (format!("{snrs_types}::Felt"), true),
_ => (type_name.to_string(), false),
}
}
6 changes: 5 additions & 1 deletion crates/rs/src/expand/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,14 @@ pub fn cainome_cairo_serde() -> Type {

#[inline]
pub fn cainome_cairo_serde_path() -> String {
//String::from("cainome_cairo_serde")
String::from("cainome::cairo_serde")
}

#[inline]
pub fn starknet_rs_types_path() -> String {
String::from("starknet::core::types")
}

/// Expands the implementation line with generic types.
pub fn impl_with_gen_args(entity_name: &Ident, gen_args: &Vec<Ident>) -> TokenStream2 {
let gen_args_rust: Vec<Ident> = gen_args
Expand Down

0 comments on commit 129a2ec

Please sign in to comment.