Skip to content

Commit

Permalink
add field type encode and decode.
Browse files Browse the repository at this point in the history
  • Loading branch information
YancyParker committed Dec 15, 2023
1 parent b86c688 commit 4d49530
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
11 changes: 5 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ola-lang-abi"
version = "1.0.3"
version = "1.0.4"
edition = "2021"
license = "MIT"
description = "Ola Smart Contract ABI parsing library"
Expand All @@ -11,11 +11,10 @@ documentation = "https://olang.gitbook.io/ola-lang/"
homepage = "https://github.com/Sin7Y/ola-lang-abi"

[dependencies]
anyhow = { version = "1.0", default-features = false, features = ["std"] }
nom = { version = "7.0", default-features = false, features = ["std"] }
regex = { version = "1.5", default-features = false, features = ["std"] }
serde = { version = "1.0", default-features = false, features = ["derive"] }
serde_json = { version = "1.0", default-features = false, features = ["std"] }
anyhow = { version = "1.0.75", default-features = false, features = ["std"] }
nom = { version = "7.1.3", default-features = false, features = ["std"] }
serde = { version = "1.0.193", default-features = false, features = ["derive"] }
serde_json = { version = "1.0.108", default-features = false, features = ["std"] }
tiny-keccak = { version = "2.0", default-features = false, features = ["keccak"] }

[dev-dependencies]
Expand Down
1 change: 1 addition & 0 deletions examples/book_decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ fn main() {
serde_json::from_reader(file).expect("failed to parse ABI")
};


let data = vec![60, 5, 111, 108, 97, 118, 109, 7, 120553111];

// Decode
Expand Down
30 changes: 29 additions & 1 deletion src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,14 @@ fn parse_simple_type(
move |input: &str| {
alt((
parse_tuple(components.clone()),
parse_fields,
parse_uint,
parse_field,
parse_address,
parse_hash,
parse_bool,
parse_string,
parse_fields,

))(input)
}
}
Expand All @@ -233,6 +235,10 @@ fn parse_uint(input: &str) -> TypeParseResult<&str, Type> {
map_error(verify(parse_sized("u"), check_int_size)(input).map(|(i, _)| (i, Type::U32)))
}

fn parse_field(input: &str) -> TypeParseResult<&str, Type> {
map_error(tag("field")(input).map(|(i, _)| (i, Type::Field)))
}

fn parse_address(input: &str) -> TypeParseResult<&str, Type> {
map_error(tag("address")(input).map(|(i, _)| (i, Type::Address)))
}
Expand Down Expand Up @@ -352,6 +358,28 @@ mod test {
assert_eq!(v, param_json);
}

#[test]
fn serde_field() {
let v = json!({
"name": "a",
"type": "field",
});

let param: Param = serde_json::from_value(v.clone()).expect("param deserialized");

assert_eq!(
param,
Param {
name: "a".to_string(),
type_: Type::Field,
}
);

let param_json = serde_json::to_value(param).expect("param serialized");

assert_eq!(v, param_json);
}

#[test]
fn serde_address() {
let v = json!({
Expand Down

0 comments on commit 4d49530

Please sign in to comment.