Skip to content

Commit 451d029

Browse files
author
Xavier Lau
authored
Non-Standardized Supporting (rust-ethereum#236)
* Non Standardized `errors` * Non Standardized `util` * Non Standardized `param_type` * Non Standardized `tuple_param` * More Detail Features * Non Standardized `param` * Non Standardized `signature` * Non Standardized `token` * Non Standardized `filter` * Non Standardized `state_mutability` * Non Standardized `encoder` and `decoder` * Non Standardized `function` * Non Standardized `constructor` * Non Standardized `contract` * Non Standardized `event_param` * Non Standardized `operation` * Fix Compile * Non Standardized `log` * Fix Compile * Non Standardized `event` * Fix Typo * Fix Tests * Fix Typo * Use `alloc` Globally * Add `no-default-features` Checks * Change as Suggest * Satisfy `cargo-hack` * Enable `std` by `default`
1 parent 506f6cc commit 451d029

24 files changed

+285
-87
lines changed

.github/workflows/main.yml

+10
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ jobs:
3939
command: check
4040
args: --all --all-targets --all-features
4141

42+
- uses: actions-rs/cargo@v1
43+
with:
44+
command: build
45+
args: --all --all-targets --no-default-features
46+
4247
- uses: actions-rs/cargo@v1
4348
with:
4449
command: test
@@ -48,3 +53,8 @@ jobs:
4853
with:
4954
command: clippy
5055
args: --all --all-targets --all-features -- -D warnings
56+
57+
- uses: actions-rs/cargo@v1
58+
with:
59+
command: clippy
60+
args: --all --all-targets --no-default-features -- -D warnings

ethabi/Cargo.toml

+31-8
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,38 @@ description = "Easy to use conversion of ethereum contract calls to bytecode."
1313
edition = "2018"
1414

1515
[dependencies]
16-
anyhow = "1"
17-
hex = "0.4"
18-
serde = { version = "1.0", features = ["derive"] }
19-
serde_json = "1.0"
20-
sha3 = "0.9"
21-
ethereum-types = "0.12.0"
22-
thiserror = "1"
23-
uint = "0.9.0"
16+
anyhow = { version = "1", optional = true }
17+
hex = { version = "0.4", default-features = false, features = ["alloc"]}
18+
serde = { version = "1.0", optional = true, features = ["derive"] }
19+
serde_json = { version = "1.0", optional = true }
20+
sha3 = { version = "0.9", default-features = false }
21+
ethereum-types = { version = "0.12.0", default-features = false }
22+
thiserror = { version = "1", optional = true }
23+
uint = { version = "0.9.0", optional = true }
2424

2525
[dev-dependencies]
2626
hex-literal = "0.3"
2727
paste = "1"
28+
29+
[features]
30+
default = [
31+
"std",
32+
"full-serde",
33+
]
34+
std = [
35+
"anyhow/std",
36+
"hex/std",
37+
"sha3/std",
38+
"ethereum-types/std",
39+
"thiserror",
40+
"uint/std",
41+
]
42+
43+
# To enable custom `Reader`/`Tokenizer` and `serde` features support
44+
full-serde = [
45+
"std",
46+
"anyhow",
47+
"serde",
48+
"serde_json",
49+
"uint",
50+
]

ethabi/src/constructor.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@
77
// except according to those terms.
88

99
//! Contract constructor call builder.
10-
use crate::{encode, Bytes, Error, Param, ParamType, Result, Token};
10+
11+
#[cfg(feature = "full-serde")]
1112
use serde::{Deserialize, Serialize};
1213

14+
#[cfg(not(feature = "std"))]
15+
use crate::no_std_prelude::*;
16+
use crate::{encode, Bytes, Error, Param, ParamType, Result, Token};
17+
1318
/// Contract constructor specification.
14-
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
19+
#[cfg_attr(feature = "full-serde", derive(Serialize, Deserialize))]
20+
#[derive(Debug, Clone, PartialEq)]
1521
pub struct Constructor {
1622
/// Constructor input.
1723
pub inputs: Vec<Param>,

ethabi/src/contract.rs

+39-26
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,42 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
use crate::{errors, operation::Operation, Constructor, Error, Event, Function};
9+
use alloc::collections::{btree_map::Values, BTreeMap};
10+
#[cfg(feature = "full-serde")]
11+
use core::fmt;
12+
use core::iter::Flatten;
13+
#[cfg(feature = "full-serde")]
14+
use std::io;
15+
16+
#[cfg(feature = "full-serde")]
1017
use serde::{
1118
de::{SeqAccess, Visitor},
1219
ser::SerializeSeq,
1320
Deserialize, Deserializer, Serialize, Serializer,
1421
};
15-
use std::{
16-
collections::{hash_map::Values, HashMap},
17-
fmt, io,
18-
iter::Flatten,
19-
};
22+
23+
#[cfg(not(feature = "std"))]
24+
use crate::no_std_prelude::*;
25+
#[cfg(feature = "full-serde")]
26+
use crate::operation::Operation;
27+
use crate::{errors, Constructor, Error, Event, Function};
2028

2129
/// API building calls to contracts ABI.
2230
#[derive(Clone, Debug, Default, PartialEq)]
2331
pub struct Contract {
2432
/// Contract constructor.
2533
pub constructor: Option<Constructor>,
2634
/// Contract functions.
27-
pub functions: HashMap<String, Vec<Function>>,
35+
pub functions: BTreeMap<String, Vec<Function>>,
2836
/// Contract events, maps signature to event.
29-
pub events: HashMap<String, Vec<Event>>,
37+
pub events: BTreeMap<String, Vec<Event>>,
3038
/// Contract has receive function.
3139
pub receive: bool,
3240
/// Contract has fallback function.
3341
pub fallback: bool,
3442
}
3543

44+
#[cfg(feature = "full-serde")]
3645
impl<'a> Deserialize<'a> for Contract {
3746
fn deserialize<D>(deserializer: D) -> Result<Contract, D::Error>
3847
where
@@ -42,8 +51,10 @@ impl<'a> Deserialize<'a> for Contract {
4251
}
4352
}
4453

54+
#[cfg(feature = "full-serde")]
4555
struct ContractVisitor;
4656

57+
#[cfg(feature = "full-serde")]
4758
impl<'a> Visitor<'a> for ContractVisitor {
4859
type Value = Contract;
4960

@@ -80,6 +91,7 @@ impl<'a> Visitor<'a> for ContractVisitor {
8091
}
8192
}
8293

94+
#[cfg(feature = "full-serde")]
8395
impl Serialize for Contract {
8496
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
8597
where
@@ -137,6 +149,7 @@ impl Serialize for Contract {
137149

138150
impl Contract {
139151
/// Loads contract from json.
152+
#[cfg(feature = "full-serde")]
140153
pub fn load<T: io::Read>(reader: T) -> errors::Result<Self> {
141154
serde_json::from_reader(reader).map_err(From::from)
142155
}
@@ -200,11 +213,11 @@ impl<'a> Iterator for Events<'a> {
200213
}
201214
}
202215

203-
#[cfg(test)]
216+
#[cfg(all(test, feature = "full-serde"))]
204217
#[allow(deprecated)]
205218
mod test {
206219
use crate::{tests::assert_ser_de, Constructor, Contract, Event, EventParam, Function, Param, ParamType};
207-
use std::{collections::HashMap, iter::FromIterator};
220+
use std::{collections::BTreeMap, iter::FromIterator};
208221

209222
#[test]
210223
fn empty() {
@@ -216,8 +229,8 @@ mod test {
216229
deserialized,
217230
Contract {
218231
constructor: None,
219-
functions: HashMap::new(),
220-
events: HashMap::new(),
232+
functions: BTreeMap::new(),
233+
events: BTreeMap::new(),
221234
receive: false,
222235
fallback: false,
223236
}
@@ -250,8 +263,8 @@ mod test {
250263
constructor: Some(Constructor {
251264
inputs: vec![Param { name: "a".to_string(), kind: ParamType::Address, internal_type: None }]
252265
}),
253-
functions: HashMap::new(),
254-
events: HashMap::new(),
266+
functions: BTreeMap::new(),
267+
events: BTreeMap::new(),
255268
receive: false,
256269
fallback: false,
257270
}
@@ -295,7 +308,7 @@ mod test {
295308
deserialized,
296309
Contract {
297310
constructor: None,
298-
functions: HashMap::from_iter(vec![
311+
functions: BTreeMap::from_iter(vec![
299312
(
300313
"foo".to_string(),
301314
vec![Function {
@@ -325,7 +338,7 @@ mod test {
325338
}]
326339
)
327340
]),
328-
events: HashMap::new(),
341+
events: BTreeMap::new(),
329342
receive: false,
330343
fallback: false,
331344
}
@@ -369,7 +382,7 @@ mod test {
369382
deserialized,
370383
Contract {
371384
constructor: None,
372-
functions: HashMap::from_iter(vec![(
385+
functions: BTreeMap::from_iter(vec![(
373386
"foo".to_string(),
374387
vec![
375388
Function {
@@ -396,7 +409,7 @@ mod test {
396409
}
397410
]
398411
)]),
399-
events: HashMap::new(),
412+
events: BTreeMap::new(),
400413
receive: false,
401414
fallback: false,
402415
}
@@ -441,8 +454,8 @@ mod test {
441454
deserialized,
442455
Contract {
443456
constructor: None,
444-
functions: HashMap::new(),
445-
events: HashMap::from_iter(vec![
457+
functions: BTreeMap::new(),
458+
events: BTreeMap::from_iter(vec![
446459
(
447460
"foo".to_string(),
448461
vec![Event {
@@ -508,8 +521,8 @@ mod test {
508521
deserialized,
509522
Contract {
510523
constructor: None,
511-
functions: HashMap::new(),
512-
events: HashMap::from_iter(vec![(
524+
functions: BTreeMap::new(),
525+
events: BTreeMap::from_iter(vec![(
513526
"foo".to_string(),
514527
vec![
515528
Event {
@@ -550,8 +563,8 @@ mod test {
550563
deserialized,
551564
Contract {
552565
constructor: None,
553-
functions: HashMap::new(),
554-
events: HashMap::new(),
566+
functions: BTreeMap::new(),
567+
events: BTreeMap::new(),
555568
receive: true,
556569
fallback: false,
557570
}
@@ -574,8 +587,8 @@ mod test {
574587
deserialized,
575588
Contract {
576589
constructor: None,
577-
functions: HashMap::new(),
578-
events: HashMap::new(),
590+
functions: BTreeMap::new(),
591+
events: BTreeMap::new(),
579592
receive: false,
580593
fallback: true,
581594
}

ethabi/src/decoder.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
//! ABI decoder.
1010
11+
#[cfg(not(feature = "std"))]
12+
use crate::no_std_prelude::*;
1113
use crate::{Error, ParamType, Token, Word};
1214

1315
#[derive(Debug)]
@@ -217,9 +219,12 @@ fn decode_param(param: &ParamType, data: &[u8], offset: usize) -> Result<DecodeR
217219

218220
#[cfg(test)]
219221
mod tests {
220-
use crate::{decode, ParamType, Token, Uint};
221222
use hex_literal::hex;
222223

224+
#[cfg(not(feature = "std"))]
225+
use crate::no_std_prelude::*;
226+
use crate::{decode, ParamType, Token, Uint};
227+
223228
#[test]
224229
fn decode_from_empty_byte_slice() {
225230
// these can NOT be decoded from empty byte slice

ethabi/src/encoder.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
//! ABI encoder.
1010
11+
#[cfg(not(feature = "std"))]
12+
use crate::no_std_prelude::*;
1113
use crate::{util::pad_u32, Bytes, Token, Word};
1214

1315
fn pad_bytes(bytes: &[u8]) -> Vec<Word> {
@@ -175,9 +177,12 @@ fn encode_token(token: &Token) -> Mediate {
175177

176178
#[cfg(test)]
177179
mod tests {
178-
use crate::{encode, util::pad_u32, Token};
179180
use hex_literal::hex;
180181

182+
#[cfg(not(feature = "std"))]
183+
use crate::no_std_prelude::*;
184+
use crate::{encode, util::pad_u32, Token};
185+
181186
#[test]
182187
fn encode_address() {
183188
let address = Token::Address([0x11u8; 20].into());

ethabi/src/errors.rs

+18-8
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,49 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9+
#[cfg(feature = "full-serde")]
10+
use core::num;
11+
12+
#[cfg(feature = "full-serde")]
913
use anyhow::anyhow;
10-
use std::{num, string};
14+
#[cfg(feature = "std")]
1115
use thiserror::Error;
1216

17+
#[cfg(not(feature = "std"))]
18+
use crate::no_std_prelude::*;
19+
1320
/// Ethabi result type
14-
pub type Result<T> = std::result::Result<T, Error>;
21+
pub type Result<T> = core::result::Result<T, Error>;
1522

1623
/// Ethabi errors
17-
#[derive(Debug, Error)]
24+
#[cfg_attr(feature = "std", derive(Error))]
25+
#[derive(Debug)]
1826
pub enum Error {
1927
/// Invalid entity such as a bad function name.
20-
#[error("Invalid name: {0}")]
28+
#[cfg_attr(feature = "std", error("Invalid name: {0}"))]
2129
InvalidName(String),
2230
/// Invalid data.
23-
#[error("Invalid data")]
31+
#[cfg_attr(feature = "std", error("Invalid data"))]
2432
InvalidData,
2533
/// Serialization error.
34+
#[cfg(feature = "full-serde")]
2635
#[error("Serialization error: {0}")]
2736
SerdeJson(#[from] serde_json::Error),
2837
/// Integer parsing error.
38+
#[cfg(feature = "full-serde")]
2939
#[error("Integer parsing error: {0}")]
3040
ParseInt(#[from] num::ParseIntError),
31-
/// UTF-8 parsing error.
32-
#[error("UTF-8 parsing error: {0}")]
33-
Utf8(#[from] string::FromUtf8Error),
3441
/// Hex string parsing error.
42+
#[cfg(feature = "full-serde")]
3543
#[error("Hex parsing error: {0}")]
3644
Hex(#[from] hex::FromHexError),
3745
/// Other errors.
46+
#[cfg(feature = "full-serde")]
3847
#[error("{0}")]
3948
Other(#[from] anyhow::Error),
4049
}
4150

51+
#[cfg(feature = "full-serde")]
4252
impl From<uint::FromDecStrErr> for Error {
4353
fn from(err: uint::FromDecStrErr) -> Self {
4454
use uint::FromDecStrErr::*;

0 commit comments

Comments
 (0)