Skip to content

Commit ec254b9

Browse files
committed
Polished version
1 parent 8ccdb2a commit ec254b9

13 files changed

+97
-114
lines changed

Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ std = [
1414
"serde_json/std",
1515
"indexmap/std",
1616
"starknet-crypto/std",
17-
"thiserror"
17+
"thiserror-no-std/std"
1818
]
1919
testing = ["std"]
2020

@@ -37,9 +37,7 @@ starknet-crypto = { version = "0.5.1", default-features = false, features = [
3737
] }
3838
strum = "0.24.1"
3939
strum_macros = "0.24.3"
40-
thiserror = { version = "1.0.31", optional = true}
4140
thiserror-no-std = { version = "2.0.2", default-features = false }
42-
cfg-if = "1.0.0"
4341

4442
[dev-dependencies]
4543
assert_matches = "1.5.0"

src/block.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22
#[path = "block_test.rs"]
33
mod block_test;
44

5-
#[cfg(not(feature = "std"))]
6-
use alloc::vec::Vec;
7-
85
use derive_more::Display;
96
use serde::{Deserialize, Serialize};
107

118
use crate::core::{ContractAddress, GlobalRoot};
129
use crate::hash::StarkHash;
1310
use crate::serde_utils::{BytesAsHex, PrefixedBytesAsHex};
11+
use crate::stdlib::vec::Vec;
1412
use crate::transaction::{Transaction, TransactionHash, TransactionOutput};
1513

1614
/// A block.

src/core.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,6 @@
22
#[path = "core_test.rs"]
33
mod core_test;
44

5-
cfg_if::cfg_if! {
6-
if #[cfg(not(feature = "std"))] {
7-
use alloc::format;
8-
use alloc::fmt;
9-
use alloc::fmt::Debug;
10-
use alloc::string::String;
11-
use alloc::string::ToString;
12-
} else {
13-
use std::fmt;
14-
use std::fmt::Debug;
15-
}
16-
}
17-
185
use derive_more::Display;
196
use once_cell::sync::Lazy;
207
use primitive_types::H160;
@@ -23,6 +10,9 @@ use starknet_crypto::FieldElement;
2310

2411
use crate::hash::{pedersen_hash_array, StarkFelt, StarkHash};
2512
use crate::serde_utils::{BytesAsHex, PrefixedBytesAsHex};
13+
use crate::stdlib::fmt::Debug;
14+
use crate::stdlib::string::{String, ToString};
15+
use crate::stdlib::{fmt, format, mem};
2616
use crate::transaction::{Calldata, ContractAddressSalt};
2717
use crate::{impl_from_through_intermediate, StarknetApiError};
2818

@@ -262,7 +252,7 @@ pub struct EthAddress(pub H160);
262252
impl TryFrom<StarkFelt> for EthAddress {
263253
type Error = StarknetApiError;
264254
fn try_from(felt: StarkFelt) -> Result<Self, Self::Error> {
265-
const COMPLIMENT_OF_H160: usize = core::mem::size_of::<StarkFelt>() - H160::len_bytes();
255+
const COMPLIMENT_OF_H160: usize = mem::size_of::<StarkFelt>() - H160::len_bytes();
266256

267257
let (rest, h160_bytes) = felt.bytes().split_at(COMPLIMENT_OF_H160);
268258
if rest != [0u8; COMPLIMENT_OF_H160] {

src/data_availability.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
#[cfg(not(feature = "std"))]
2-
use alloc::format;
3-
41
use serde::{Deserialize, Serialize};
52

63
use crate::hash::StarkFelt;
4+
use crate::stdlib::format;
75
use crate::StarknetApiError;
86

97
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]

src/deprecated_contract_class.rs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,14 @@
1-
cfg_if::cfg_if! {
2-
if #[cfg(feature = "std")] {
3-
use std::collections::HashMap;
4-
use std::num;
5-
} else {
6-
use alloc::format;
7-
use alloc::vec::Vec;
8-
use alloc::string::String;
9-
10-
use core::num;
11-
12-
use hashbrown::HashMap;
13-
}
14-
}
15-
161
use cairo_lang_starknet::casm_contract_class::CasmContractEntryPoint;
172
use serde::de::Error as DeserializationError;
183
use serde::{Deserialize, Deserializer, Serialize, Serializer};
194
use serde_json::Value;
205

216
use crate::core::EntryPointSelector;
227
use crate::serde_utils::deserialize_optional_contract_class_abi_entry_vector;
8+
use crate::stdlib::collections::HashMap;
9+
use crate::stdlib::string::String;
10+
use crate::stdlib::vec::Vec;
11+
use crate::stdlib::{format, num};
2312
use crate::StarknetApiError;
2413

2514
/// A deprecated contract class.

src/hash.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,16 @@
22
#[path = "hash_test.rs"]
33
mod hash_test;
44

5-
cfg_if::cfg_if! {
6-
if #[cfg(feature = "std")] {
7-
use std::fmt;
8-
use std::fmt::{Debug, Display};
9-
use std::io::Error;
10-
} else {
11-
use alloc::fmt;
12-
use alloc::fmt::{Debug, Display};
13-
use alloc::format;
14-
use alloc::string::ToString;
15-
}
16-
}
5+
#[cfg(feature = "std")]
6+
use std::io::Error;
177

188
use serde::{Deserialize, Serialize};
199
use starknet_crypto::{pedersen_hash as starknet_crypto_pedersen_hash, FieldElement};
2010

2111
use crate::serde_utils::{bytes_from_hex_str, hex_str_from_bytes, BytesAsHex, PrefixedBytesAsHex};
12+
use crate::stdlib::fmt::{Debug, Display};
13+
use crate::stdlib::string::ToString;
14+
use crate::stdlib::{fmt, format, mem};
2215
use crate::{impl_from_through_intermediate, StarknetApiError};
2316

2417
/// Genesis state hash.
@@ -215,8 +208,7 @@ impl From<StarkFelt> for PrefixedBytesAsHex<32_usize> {
215208
impl TryFrom<StarkFelt> for usize {
216209
type Error = StarknetApiError;
217210
fn try_from(felt: StarkFelt) -> Result<Self, Self::Error> {
218-
const COMPLIMENT_OF_USIZE: usize =
219-
core::mem::size_of::<StarkFelt>() - core::mem::size_of::<usize>();
211+
const COMPLIMENT_OF_USIZE: usize = mem::size_of::<StarkFelt>() - mem::size_of::<usize>();
220212

221213
let (rest, usize_bytes) = felt.bytes().split_at(COMPLIMENT_OF_USIZE);
222214
if rest != [0u8; COMPLIMENT_OF_USIZE] {

src/lib.rs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
55
#![cfg_attr(not(feature = "std"), no_std)]
66

7-
#[cfg(not(features = "std"))]
8-
extern crate alloc;
7+
#[cfg(feature = "std")]
8+
include!("./with_std.rs");
99

10-
#[cfg(not(features = "std"))]
11-
extern crate thiserror_no_std as thiserror;
10+
#[cfg(not(feature = "std"))]
11+
include!("./without_std.rs");
1212

1313
pub mod block;
1414
pub mod core;
@@ -20,21 +20,29 @@ pub mod state;
2020
pub mod transaction;
2121
pub mod type_utils;
2222

23-
mod api_error {
24-
25-
cfg_if::cfg_if! {
26-
if #[cfg(feature = "std")] {
27-
use std::num;
28-
} else {
29-
use core::num;
30-
use alloc::string::String;
31-
}
23+
pub mod stdlib {
24+
pub mod collections {
25+
#[cfg(feature = "std")]
26+
pub use crate::with_std::collections::*;
27+
#[cfg(not(feature = "std"))]
28+
pub use crate::without_std::collections::*;
3229
}
3330

31+
#[cfg(feature = "std")]
32+
pub use crate::with_std::*;
33+
#[cfg(not(feature = "std"))]
34+
pub use crate::without_std::*;
35+
}
36+
37+
mod api_error {
38+
use thiserror_no_std::Error;
39+
3440
use crate::serde_utils::InnerDeserializationError;
41+
use crate::stdlib::num;
42+
use crate::stdlib::string::String;
3543

3644
/// The error type returned by StarknetApi.
37-
#[derive(thiserror::Error, Clone, Debug)]
45+
#[derive(Error, Clone, Debug)]
3846
pub enum StarknetApiError {
3947
/// Error in the inner deserialization of the node.
4048
#[error(transparent)]

src/serde_utils.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,16 @@
33
#[path = "serde_utils_test.rs"]
44
mod serde_utils_test;
55

6-
cfg_if::cfg_if! {
7-
if #[cfg(not(feature = "std"))] {
8-
use alloc::borrow::ToOwned;
9-
use alloc::fmt;
10-
use alloc::format;
11-
use alloc::string::String;
12-
use alloc::string::ToString;
13-
use alloc::vec;
14-
use alloc::vec::Vec;
15-
} else {
16-
use std::fmt;
17-
}
18-
}
19-
206
use serde::de::{Deserialize, Visitor};
217
use serde::ser::{Serialize, SerializeTuple};
228
use serde::Deserializer;
9+
use thiserror_no_std::Error;
2310

2411
use crate::deprecated_contract_class::ContractClassAbiEntry;
12+
use crate::stdlib::borrow::ToOwned;
13+
use crate::stdlib::string::{String, ToString};
14+
use crate::stdlib::vec::Vec;
15+
use crate::stdlib::{fmt, format, vec};
2516

2617
/// A [BytesAsHex](`crate::serde_utils::BytesAsHex`) prefixed with '0x'.
2718
pub type PrefixedBytesAsHex<const N: usize> = BytesAsHex<N, true>;
@@ -90,7 +81,7 @@ impl<const N: usize, const PREFIXED: bool> Serialize for BytesAsHex<N, PREFIXED>
9081
}
9182

9283
/// The error type returned by the inner deserialization.
93-
#[derive(thiserror::Error, Clone, Debug)]
84+
#[derive(Error, Clone, Debug)]
9485
pub enum InnerDeserializationError {
9586
/// Error parsing the hex string.
9687
#[error(transparent)]

src/state.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,6 @@
22
#[path = "state_test.rs"]
33
mod state_test;
44

5-
cfg_if::cfg_if! {
6-
if #[cfg(features = "std")] {
7-
use std::collections::HashMap;
8-
} else {
9-
use alloc::fmt::Debug;
10-
use alloc::string::String;
11-
use alloc::vec::Vec;
12-
13-
use hashbrown::HashMap;
14-
}
15-
}
16-
175
use indexmap::IndexMap;
186
use serde::{Deserialize, Serialize};
197

@@ -24,6 +12,10 @@ use crate::core::{
2412
};
2513
use crate::deprecated_contract_class::ContractClass as DeprecatedContractClass;
2614
use crate::hash::{StarkFelt, StarkHash};
15+
use crate::stdlib::collections::HashMap;
16+
use crate::stdlib::fmt::Debug;
17+
use crate::stdlib::string::String;
18+
use crate::stdlib::vec::Vec;
2719
use crate::{impl_from_through_intermediate, StarknetApiError};
2820

2921
pub type DeclaredClasses = IndexMap<ClassHash, ContractClass>;

src/state_test.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
use std::collections::HashMap;
2-
31
use serde_json::json;
42

53
use crate::deprecated_contract_class::EntryPointOffset;
4+
use crate::stdlib::collections::HashMap;
65

76
#[test]
87
fn entry_point_offset_from_json_str() {

src/transaction.rs

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,3 @@
1-
cfg_if::cfg_if! {
2-
if #[cfg(features = "std")] {
3-
use std::collections::{BTreeMap, HashMap, HashSet};
4-
use std::fmt;
5-
use std::fmt::Display;
6-
use std::sync::Arc;
7-
} else {
8-
use alloc::collections::BTreeMap;
9-
use alloc::fmt;
10-
use alloc::fmt::Display;
11-
use alloc::format;
12-
use alloc::string::String;
13-
use alloc::sync::Arc;
14-
use alloc::vec::Vec;
15-
16-
use hashbrown::{HashMap, HashSet};
17-
}
18-
}
19-
201
use derive_more::From;
212
use serde::{Deserialize, Deserializer, Serialize, Serializer};
223
use strum::IntoEnumIterator;
@@ -29,6 +10,12 @@ use crate::core::{
2910
use crate::data_availability::DataAvailabilityMode;
3011
use crate::hash::{StarkFelt, StarkHash};
3112
use crate::serde_utils::PrefixedBytesAsHex;
13+
use crate::stdlib::collections::{BTreeMap, HashMap, HashSet};
14+
use crate::stdlib::fmt;
15+
use crate::stdlib::fmt::Display;
16+
use crate::stdlib::string::String;
17+
use crate::stdlib::sync::Arc;
18+
use crate::stdlib::vec::Vec;
3219
use crate::StarknetApiError;
3320

3421
/// A transaction.

src/with_std.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
pub mod with_std {
2+
pub use std::borrow;
3+
pub use std::fmt;
4+
pub use std::format;
5+
pub use std::mem;
6+
pub use std::num;
7+
8+
pub use std::string;
9+
pub use std::sync;
10+
pub use std::vec;
11+
12+
pub mod collections {
13+
pub use std::collections::{BTreeMap, HashMap, HashSet};
14+
}
15+
}

src/without_std.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#[macro_use]
2+
pub extern crate alloc;
3+
4+
pub mod without_std {
5+
pub use core::fmt;
6+
pub use core::hash;
7+
pub use core::mem;
8+
pub use core::num;
9+
10+
pub use alloc::boxed;
11+
pub use alloc::format;
12+
pub use alloc::rc;
13+
pub use alloc::string;
14+
pub use alloc::sync;
15+
pub use alloc::vec;
16+
17+
pub mod collections {
18+
pub use alloc::collections::BTreeMap;
19+
pub use hashbrown::{HashMap, HashSet};
20+
}
21+
22+
pub mod borrow {
23+
pub use alloc::borrow::*;
24+
pub use core::borrow::*;
25+
}
26+
}

0 commit comments

Comments
 (0)