Skip to content

Commit

Permalink
fix(derive): support vec<string>
Browse files Browse the repository at this point in the history
  • Loading branch information
52 committed Mar 7, 2024
1 parent bd6e9e9 commit 0ee2ad7
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ resolver = "2"
members = ["stacks", "stacks_derive", "tests"]

[workspace.package]
version = "0.3.2"
version = "0.3.3"
edition = "2021"
license = "MIT OR Apache-2.0"
authors = ["Max Karou <maxkarou@protonmail.com>"]
Expand Down
2 changes: 1 addition & 1 deletion stacks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ version = "0.28.2"
features = ["recovery"]

[dependencies.stacks_derive]
version = "0.3.2"
version = "0.3.3"
optional = true
path = "../stacks_derive"

Expand Down
13 changes: 13 additions & 0 deletions stacks_derive/src/from_tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,19 @@ pub(crate) fn __impl(input: proc_macro::TokenStream) -> Result<TokenStream> {
break 'stream;
}

// handle Vec<String>
if ident.eq("Vec") && args.to_string().eq("String") {
let ty = quote!(List);
__internal_extract_and_cast(&mut stream, &key, &ty_name);
let err_cast = __Error::Cast(key, ty, ident.to_token_stream());
let tokens = quote!(
.and_then(|x| x.cast::<::stacks_rs::clarity::List>().map_err(|_| #err_cast))
);
stream.extend(tokens);
stream.extend(quote!(.map(|list| list.into_iter().map(|any| any.to_string()).collect())?));
break 'stream;
}

// handle Option<i128>
if ident.eq("Option") && args.to_string().eq("i128") {
__internal_extract_and_cast(&mut stream, &key, &ty_name);
Expand Down
2 changes: 1 addition & 1 deletion tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ repository.workspace = true
publish = false

[dependencies.stacks-rs]
version = "0.3.2"
version = "0.3.3"
features = ["derive", "wallet-sdk"]
path = "../stacks"
35 changes: 27 additions & 8 deletions tests/src/test_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use stacks_rs::clarity;
use stacks_rs::clarity::Any;
use stacks_rs::clarity::Cast;
use stacks_rs::clarity::Int;
use stacks_rs::clarity::List;
use stacks_rs::clarity::OptionalSome;
use stacks_rs::clarity::ResponseErr;
use stacks_rs::clarity::ResponseOk;
Expand All @@ -29,6 +30,8 @@ fn test_derive_from_tuple() {
response: Response,
#[stacks(key = "options")]
options: Options,
#[stacks(key = "vector")]
vector: Vector,
}

#[derive(FromTuple)]
Expand Down Expand Up @@ -75,6 +78,12 @@ fn test_derive_from_tuple() {
n: i128,
}

#[derive(FromTuple)]
struct Vector {
#[stacks(key = "o")]
o: Vec<String>,
}

let data = clarity!(
Tuple,
("a", clarity!(Int, 1)),
Expand Down Expand Up @@ -107,12 +116,25 @@ fn test_derive_from_tuple() {
("n", clarity!(ResponseOk, clarity!(Int, 1)))
);

let vector = clarity!(
Tuple,
(
"o",
clarity!(
List,
clarity!(PrincipalStandard, "STX0001"),
clarity!(PrincipalStandard, "STX0002")
)
)
);

let tuple = clarity!(
Tuple,
("data", data),
("meta", meta),
("response", response),
("options", optionals)
("options", optionals),
("vector", vector)
);

let parsed = Payload::try_from(tuple).unwrap();
Expand All @@ -130,13 +152,10 @@ fn test_derive_from_tuple() {
assert_eq!(parsed.options.l, None);
assert_eq!(parsed.response.m, 1);
assert_eq!(parsed.response.n, 1);

// assert_eq!(parsed.meta.g, Some(1));
// assert_eq!(parsed.meta.h, None);
// assert_eq!(parsed.meta.i, Some(1));
// assert_eq!(parsed.meta.j, None);
// assert_eq!(parsed.response.k, 1);
// assert_eq!(parsed.response.l, 1);
assert_eq!(
parsed.vector.o,
vec!["STX0001".to_string(), "STX0002".to_string()]
);
}

#[test]
Expand Down

0 comments on commit 0ee2ad7

Please sign in to comment.