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

feat: import serde 1 #35

Merged
merged 7 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ jobs:
- name: Check Rust format
run: |
cargo fmt --all -- --check

- name: Run Clippy lints
run: |
cargo clippy --all --all-targets --all-features -- -D warnings

- name: check Rust docs
run: |
RUSTDOCFLAGS="-Dwarnings" cargo doc --document-private-items --no-deps --all-features --workspace
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/cairo-serde/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ edition = "2021"
[dependencies]
starknet.workspace = true
thiserror.workspace = true
serde.workspace = true
2 changes: 1 addition & 1 deletion crates/cairo-serde/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub trait CairoSerde {
/// Serializes the given type into a FieldElement sequence.
fn cairo_serialize(rust: &Self::RustType) -> Vec<FieldElement>;

/// TODO: add serialize_to(rust: &Self::RustType, out: &mut Vec<FieldElement>)
/// TODO: add `serialize_to(rust: &Self::RustType, out: &mut Vec<FieldElement>)`.
/// for large buffers optimization.

/// Deserializes an array of felts into the given type.
Expand Down
2 changes: 1 addition & 1 deletion crates/cairo-serde/src/types/array_legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use crate::{CairoSerde, Error, Result};
use starknet::core::types::FieldElement;

#[derive(Debug, Clone, PartialEq, PartialOrd)]
#[derive(Debug, Clone, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize)]
pub struct CairoArrayLegacy<T>(pub Vec<T>);

impl<T: std::clone::Clone> CairoArrayLegacy<T> {
Expand Down
6 changes: 3 additions & 3 deletions crates/cairo-serde/src/types/byte_array.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Support for string compatibility with Cairo `ByteArray`.
//! https://github.com/starkware-libs/cairo/blob/a4de08fbd75fa1d58c69d054d6b3d99aaf318f90/corelib/src/byte_array.cairo
//! <https://github.com/starkware-libs/cairo/blob/a4de08fbd75fa1d58c69d054d6b3d99aaf318f90/corelib/src/byte_array.cairo>
//!
//! The basic concept of this `ByteArray` is relying on a string being
//! represented as an array of bytes packed by 31 bytes in a felt.
Expand Down Expand Up @@ -28,7 +28,7 @@ pub const BYTES31_MAX: FieldElement = FieldElement::from_mont([
576460566199927480,
]);

#[derive(Debug, Clone, Eq, PartialEq, Default)]
#[derive(Debug, Clone, Eq, PartialEq, Default, serde::Serialize, serde::Deserialize)]
pub struct Bytes31(FieldElement);

impl Bytes31 {
Expand Down Expand Up @@ -71,7 +71,7 @@ impl CairoSerde for Bytes31 {
}
}

#[derive(Debug, Clone, Eq, PartialEq, Default)]
#[derive(Debug, Clone, Eq, PartialEq, Default, serde::Serialize, serde::Deserialize)]
pub struct ByteArray {
pub data: Vec<Bytes31>,
pub pending_word: FieldElement,
Expand Down
2 changes: 1 addition & 1 deletion crates/cairo-serde/src/types/non_zero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!
//! NonZero serializes with zero ( hehe :) ) overhead as the inner value
//!
//! https://github.com/starkware-libs/cairo/blob/main/corelib/src/zeroable.cairo#L38
//! <https://github.com/starkware-libs/cairo/blob/main/corelib/src/zeroable.cairo#L38>
use crate::{CairoSerde, ContractAddress, Result, U256};
use starknet::core::types::FieldElement;

Expand Down
2 changes: 1 addition & 1 deletion crates/cairo-serde/src/types/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! In cairo, `Some` is the first field and `None` the second one.
//! To follow the serialization rule, `Some` has index 0, and `None` index 1.
//!
//! https://github.com/starkware-libs/cairo/blob/main/corelib/src/option.cairo#L6
//! <https://github.com/starkware-libs/cairo/blob/main/corelib/src/option.cairo#L6>
use crate::{CairoSerde, Error, Result};
use starknet::core::types::FieldElement;

Expand Down
2 changes: 1 addition & 1 deletion crates/cairo-serde/src/types/result.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! CairoSerde implementation for Result.
//!
//! https://github.com/starkware-libs/cairo/blob/main/corelib/src/result.cairo#L6
//! <https://github.com/starkware-libs/cairo/blob/main/corelib/src/result.cairo#L6>
use crate::{CairoSerde, Error as CairoError, Result as CairoResult};
use starknet::core::types::FieldElement;

Expand Down
6 changes: 3 additions & 3 deletions crates/cairo-serde/src/types/starknet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{CairoSerde, Error, Result};
use starknet::core::types::FieldElement;

/// ContractAddress.
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize)]
pub struct ContractAddress(pub FieldElement);

impl From<FieldElement> for ContractAddress {
Expand Down Expand Up @@ -42,7 +42,7 @@ impl CairoSerde for ContractAddress {
}

/// ClassHash.
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize)]
pub struct ClassHash(pub FieldElement);

impl From<FieldElement> for ClassHash {
Expand Down Expand Up @@ -77,7 +77,7 @@ impl CairoSerde for ClassHash {
}

/// EthAddress.
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize)]
pub struct EthAddress(pub FieldElement);

impl From<FieldElement> for EthAddress {
Expand Down
2 changes: 1 addition & 1 deletion crates/cairo-serde/src/types/u256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::CairoSerde;
use starknet::core::types::{FieldElement, ValueOutOfRangeError};
use std::cmp::Ordering;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct U256 {
pub low: u128,
pub high: u128,
Expand Down
13 changes: 13 additions & 0 deletions crates/parser/src/abi/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,15 @@ impl AbiParser {
})
}

/// Collects the function from the ABI entry.
///
/// # Arguments
///
/// * `entry` - The ABI entry to collect functions from.
/// * `all_composites` - All known composites tokens.
/// * `functions` - The list of functions already collected.
/// * `interfaces` - The list of interfaces already collected.
/// * `interface_name` - The name of the interface (if any).
fn collect_entry_function(
entry: &AbiEntry,
all_composites: &HashMap<String, Composite>,
Expand Down Expand Up @@ -189,7 +197,12 @@ impl AbiParser {
Ok(())
}

/// Collects the token from the ABI entry.
///
/// # Arguments
///
/// * `entry` - The ABI entry to collect tokens from.
/// * `tokens` - The list of tokens already collected.
fn collect_entry_token(
entry: &AbiEntry,
tokens: &mut HashMap<String, Vec<Token>>,
Expand Down
12 changes: 12 additions & 0 deletions crates/parser/src/abi/parser_legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,12 @@ impl AbiParserLegacy {
})
}

/// Collects the token from the ABI entry.
///
/// # Arguments
///
/// * `entry` - The ABI entry to collect tokens from.
/// * `tokens` - The list of tokens already collected.
fn collect_entry_token(
entry: &RawLegacyAbiEntry,
tokens: &mut HashMap<String, Token>,
Expand All @@ -114,7 +119,14 @@ impl AbiParserLegacy {
Ok(())
}

/// Collects the function from the ABI entry.
///
/// # Arguments
///
/// * `entry` - The ABI entry to collect functions from.
/// * `all_composites` - All known composites tokens.
/// * `structs` - The list of structs already collected.
/// * `functions` - The list of functions already collected.
fn collect_entry_function(
entry: &RawLegacyAbiEntry,
all_composites: &mut HashMap<String, Composite>,
Expand Down
4 changes: 2 additions & 2 deletions crates/parser/src/tokens/composite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl Composite {
}
}

///
/// Converts a snake case string to pascal case.
pub fn snake_to_pascal_case(s: &str) -> String {
s.split('_')
.map(|word| {
Expand All @@ -147,7 +147,7 @@ pub fn snake_to_pascal_case(s: &str) -> String {
.collect()
}

///
/// Escapes Rust keywords that may be found into cairo code.
pub fn escape_rust_keywords(s: &str) -> String {
let keywords = ["move", "type", "final"];

Expand Down
1 change: 0 additions & 1 deletion crates/rs-macro/src/macro_inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ fn open_json_file(file_path: &str) -> Result<File> {
})
}

///
pub fn str_to_litstr(str_in: &str) -> LitStr {
LitStr::new(str_in, proc_macro::Span::call_site().into())
}
1 change: 0 additions & 1 deletion crates/rs-macro/src/macro_inputs_legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ fn open_json_file(file_path: &str) -> Result<File> {
})
}

///
pub fn str_to_litstr(str_in: &str) -> LitStr {
LitStr::new(str_in, proc_macro::Span::call_site().into())
}
7 changes: 5 additions & 2 deletions crates/rs/src/expand/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,18 @@ impl CairoEnum {
// Add one phantom for each generic type.
// Those phantom fields are ignored by serde.

// TODO: as for struct, we need to have a better way for the user to specify the
// traits to derive.

quote! {
#[derive(Debug, PartialEq, PartialOrd, Clone)]
#[derive(Debug, PartialEq, PartialOrd, Clone, serde::Serialize, serde::Deserialize)]
pub enum #enum_name<#(#gen_args),*> {
#(#variants),*
}
}
} else {
quote! {
#[derive(Debug, PartialEq, PartialOrd, Clone)]
#[derive(Debug, PartialEq, PartialOrd, Clone, serde::Serialize, serde::Deserialize)]
pub enum #enum_name {
#(#variants),*
}
Expand Down
6 changes: 0 additions & 6 deletions crates/rs/src/expand/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@
//!
//! * `FCall` - Struct for readonly functions.
//! * `Execution` - Struct from starknet-rs for transaction based functions.
//!
//! ## Examples
//!
//! ```ignore (pseudo-code)
//! // TODO
//! ```
use cainome_parser::tokens::{Function, FunctionOutputKind, StateMutability, Token};
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
Expand Down
7 changes: 5 additions & 2 deletions crates/rs/src/expand/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,18 @@ impl CairoStruct {
// Add one phantom for each generic type.
// Those phantom fields are ignored by serde.

// TODO: add a way for the user to specify which trait must be derived for the
// generated structs. For now Serde is used to ensure easy serialization.

quote! {
#[derive(Debug, PartialEq, PartialOrd, Clone)]
#[derive(Debug, PartialEq, PartialOrd, Clone, serde::Serialize, serde::Deserialize)]
pub struct #struct_name<#(#gen_args),*> {
#(pub #members),*
}
}
} else {
quote! {
#[derive(Debug, PartialEq, PartialOrd, Clone)]
#[derive(Debug, PartialEq, PartialOrd, Clone, serde::Serialize, serde::Deserialize)]
pub struct #struct_name {
#(pub #members),*
}
Expand Down
4 changes: 0 additions & 4 deletions crates/rs/src/expand/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,18 @@ use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use syn::{Ident, LitInt, LitStr, Type};

///
pub fn str_to_ident(str_in: &str) -> Ident {
Ident::new(str_in, proc_macro2::Span::call_site())
}

///
pub fn str_to_type(str_in: &str) -> Type {
syn::parse_str(str_in).unwrap_or_else(|_| panic!("Can't convert {} to syn::Type", str_in))
}

///
pub fn str_to_litstr(str_in: &str) -> LitStr {
LitStr::new(str_in, proc_macro2::Span::call_site())
}

///
pub fn str_to_litint(str_in: &str) -> LitInt {
LitInt::new(str_in, proc_macro2::Span::call_site())
}
Expand Down
2 changes: 2 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[toolchain]
channel = "1.76.0"
3 changes: 3 additions & 0 deletions scripts/docs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

RUSTDOCFLAGS="-Dwarnings" cargo doc --document-private-items --no-deps --all-features --workspace
Loading