From 30c4d39e399c9485e5b09bf04f86c0812225af83 Mon Sep 17 00:00:00 2001 From: glihm Date: Thu, 16 May 2024 23:06:29 +0200 Subject: [PATCH] feat: import serde 1 (#35) * feat: add temp serde on all types * fix: add missing derive on all cainome types * fix: fix docs + add script + add to CI * fix: fix missing docs * docs: remove empty docs * fix: fix empty docs and add rust-toolchain --- .github/workflows/lint.yml | 5 +++++ Cargo.lock | 1 + crates/cairo-serde/Cargo.toml | 1 + crates/cairo-serde/src/lib.rs | 2 +- crates/cairo-serde/src/types/array_legacy.rs | 2 +- crates/cairo-serde/src/types/byte_array.rs | 6 +++--- crates/cairo-serde/src/types/non_zero.rs | 2 +- crates/cairo-serde/src/types/option.rs | 2 +- crates/cairo-serde/src/types/result.rs | 2 +- crates/cairo-serde/src/types/starknet.rs | 6 +++--- crates/cairo-serde/src/types/u256.rs | 2 +- crates/parser/src/abi/parser.rs | 13 +++++++++++++ crates/parser/src/abi/parser_legacy.rs | 12 ++++++++++++ crates/parser/src/tokens/composite.rs | 4 ++-- crates/rs-macro/src/macro_inputs.rs | 1 - crates/rs-macro/src/macro_inputs_legacy.rs | 1 - crates/rs/src/expand/enum.rs | 7 +++++-- crates/rs/src/expand/function.rs | 6 ------ crates/rs/src/expand/struct.rs | 7 +++++-- crates/rs/src/expand/utils.rs | 4 ---- rust-toolchain.toml | 2 ++ scripts/docs.sh | 3 +++ 22 files changed, 61 insertions(+), 30 deletions(-) create mode 100644 rust-toolchain.toml create mode 100644 scripts/docs.sh diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 66f0f43..30137b9 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -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 diff --git a/Cargo.lock b/Cargo.lock index aff5d2c..e6a55b0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -322,6 +322,7 @@ dependencies = [ name = "cainome-cairo-serde" version = "0.1.0" dependencies = [ + "serde", "starknet", "thiserror", ] diff --git a/crates/cairo-serde/Cargo.toml b/crates/cairo-serde/Cargo.toml index 4bf3297..7fb9f3f 100644 --- a/crates/cairo-serde/Cargo.toml +++ b/crates/cairo-serde/Cargo.toml @@ -8,3 +8,4 @@ edition = "2021" [dependencies] starknet.workspace = true thiserror.workspace = true +serde.workspace = true diff --git a/crates/cairo-serde/src/lib.rs b/crates/cairo-serde/src/lib.rs index ffb3d99..73de809 100644 --- a/crates/cairo-serde/src/lib.rs +++ b/crates/cairo-serde/src/lib.rs @@ -59,7 +59,7 @@ pub trait CairoSerde { /// Serializes the given type into a FieldElement sequence. fn cairo_serialize(rust: &Self::RustType) -> Vec; - /// TODO: add serialize_to(rust: &Self::RustType, out: &mut Vec) + /// TODO: add `serialize_to(rust: &Self::RustType, out: &mut Vec)`. /// for large buffers optimization. /// Deserializes an array of felts into the given type. diff --git a/crates/cairo-serde/src/types/array_legacy.rs b/crates/cairo-serde/src/types/array_legacy.rs index 8227185..db46fce 100644 --- a/crates/cairo-serde/src/types/array_legacy.rs +++ b/crates/cairo-serde/src/types/array_legacy.rs @@ -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(pub Vec); impl CairoArrayLegacy { diff --git a/crates/cairo-serde/src/types/byte_array.rs b/crates/cairo-serde/src/types/byte_array.rs index 61deeb0..efe0a37 100644 --- a/crates/cairo-serde/src/types/byte_array.rs +++ b/crates/cairo-serde/src/types/byte_array.rs @@ -1,5 +1,5 @@ //! Support for string compatibility with Cairo `ByteArray`. -//! 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. @@ -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 { @@ -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, pub pending_word: FieldElement, diff --git a/crates/cairo-serde/src/types/non_zero.rs b/crates/cairo-serde/src/types/non_zero.rs index c3ee4f6..6b34523 100644 --- a/crates/cairo-serde/src/types/non_zero.rs +++ b/crates/cairo-serde/src/types/non_zero.rs @@ -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 +//! use crate::{CairoSerde, ContractAddress, Result, U256}; use starknet::core::types::FieldElement; diff --git a/crates/cairo-serde/src/types/option.rs b/crates/cairo-serde/src/types/option.rs index 79486cf..b28b3f2 100644 --- a/crates/cairo-serde/src/types/option.rs +++ b/crates/cairo-serde/src/types/option.rs @@ -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 +//! use crate::{CairoSerde, Error, Result}; use starknet::core::types::FieldElement; diff --git a/crates/cairo-serde/src/types/result.rs b/crates/cairo-serde/src/types/result.rs index c5a8c07..90ae464 100644 --- a/crates/cairo-serde/src/types/result.rs +++ b/crates/cairo-serde/src/types/result.rs @@ -1,6 +1,6 @@ //! CairoSerde implementation for Result. //! -//! 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; diff --git a/crates/cairo-serde/src/types/starknet.rs b/crates/cairo-serde/src/types/starknet.rs index 6303a6b..aab2882 100644 --- a/crates/cairo-serde/src/types/starknet.rs +++ b/crates/cairo-serde/src/types/starknet.rs @@ -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 for ContractAddress { @@ -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 for ClassHash { @@ -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 for EthAddress { diff --git a/crates/cairo-serde/src/types/u256.rs b/crates/cairo-serde/src/types/u256.rs index f84956d..ab2ce35 100644 --- a/crates/cairo-serde/src/types/u256.rs +++ b/crates/cairo-serde/src/types/u256.rs @@ -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, diff --git a/crates/parser/src/abi/parser.rs b/crates/parser/src/abi/parser.rs index e0369af..31b4100 100644 --- a/crates/parser/src/abi/parser.rs +++ b/crates/parser/src/abi/parser.rs @@ -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, @@ -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>, diff --git a/crates/parser/src/abi/parser_legacy.rs b/crates/parser/src/abi/parser_legacy.rs index 920f19c..8d4465f 100644 --- a/crates/parser/src/abi/parser_legacy.rs +++ b/crates/parser/src/abi/parser_legacy.rs @@ -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, @@ -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, diff --git a/crates/parser/src/tokens/composite.rs b/crates/parser/src/tokens/composite.rs index 2705982..a707d77 100644 --- a/crates/parser/src/tokens/composite.rs +++ b/crates/parser/src/tokens/composite.rs @@ -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| { @@ -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"]; diff --git a/crates/rs-macro/src/macro_inputs.rs b/crates/rs-macro/src/macro_inputs.rs index 0f9b602..db28c55 100644 --- a/crates/rs-macro/src/macro_inputs.rs +++ b/crates/rs-macro/src/macro_inputs.rs @@ -167,7 +167,6 @@ fn open_json_file(file_path: &str) -> Result { }) } -/// pub fn str_to_litstr(str_in: &str) -> LitStr { LitStr::new(str_in, proc_macro::Span::call_site().into()) } diff --git a/crates/rs-macro/src/macro_inputs_legacy.rs b/crates/rs-macro/src/macro_inputs_legacy.rs index 728dded..e099a6b 100644 --- a/crates/rs-macro/src/macro_inputs_legacy.rs +++ b/crates/rs-macro/src/macro_inputs_legacy.rs @@ -154,7 +154,6 @@ fn open_json_file(file_path: &str) -> Result { }) } -/// pub fn str_to_litstr(str_in: &str) -> LitStr { LitStr::new(str_in, proc_macro::Span::call_site().into()) } diff --git a/crates/rs/src/expand/enum.rs b/crates/rs/src/expand/enum.rs index 526ce14..2b63d61 100644 --- a/crates/rs/src/expand/enum.rs +++ b/crates/rs/src/expand/enum.rs @@ -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),* } diff --git a/crates/rs/src/expand/function.rs b/crates/rs/src/expand/function.rs index 821a888..88f3658 100644 --- a/crates/rs/src/expand/function.rs +++ b/crates/rs/src/expand/function.rs @@ -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; diff --git a/crates/rs/src/expand/struct.rs b/crates/rs/src/expand/struct.rs index c057217..748021c 100644 --- a/crates/rs/src/expand/struct.rs +++ b/crates/rs/src/expand/struct.rs @@ -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),* } diff --git a/crates/rs/src/expand/utils.rs b/crates/rs/src/expand/utils.rs index 111cae3..a38951a 100644 --- a/crates/rs/src/expand/utils.rs +++ b/crates/rs/src/expand/utils.rs @@ -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()) } diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 0000000..624eb0e --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,2 @@ +[toolchain] +channel = "1.76.0" diff --git a/scripts/docs.sh b/scripts/docs.sh new file mode 100644 index 0000000..1ee0622 --- /dev/null +++ b/scripts/docs.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +RUSTDOCFLAGS="-Dwarnings" cargo doc --document-private-items --no-deps --all-features --workspace