Skip to content

Commit

Permalink
feat(stacks_derive): migrate to 'std::convert::TryFrom'
Browse files Browse the repository at this point in the history
  • Loading branch information
52 committed Feb 16, 2024
1 parent 4933375 commit 9b3cc05
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
7 changes: 4 additions & 3 deletions stacks_derive/src/from_tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,9 @@ pub(crate) fn __impl(input: proc_macro::TokenStream) -> Result<TokenStream> {
let (imp, ty, wher) = generics.split_for_impl();

Ok(quote!(
impl #imp #ident #ty #wher {
pub fn from_tuple(tuple: ::stacks_rs::clarity::Tuple) -> Result<Self, ::stacks_rs::Error> {
impl #imp ::std::convert::TryFrom<::stacks_rs::clarity::Tuple> for #ident #ty #wher {
type Error = ::stacks_rs::Error;
fn try_from(tuple: ::stacks_rs::clarity::Tuple) -> Result<Self, Self::Error> {
use ::stacks_rs::clarity::Cast;
Ok(Self { #(#tokens),* })
}
Expand Down Expand Up @@ -240,7 +241,7 @@ where
tuple.get(#key).ok_or_else(||#err_extract)?
.cast::<::stacks_rs::clarity::#ty>()
.map_err(|_| #err_cast)
.and_then(#ident::from_tuple)?
.and_then(#ident::try_from)?
})
}

Expand Down
4 changes: 2 additions & 2 deletions tests/src/test_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn test_derive_from_tuple() {
let meta = clarity!(Tuple, ("e", clarity!(True)), ("f", clarity!(False)));
let tuple = clarity!(Tuple, ("data", data), ("meta", meta));

let parsed = Payload::from_tuple(tuple).unwrap();
let parsed = Payload::try_from(tuple).unwrap();
assert_eq!(parsed.data.a, 1);
assert_eq!(parsed.data.b, 1);
assert_eq!(parsed.data.c, vec![0x01, 0x02, 0x03]);
Expand Down Expand Up @@ -117,7 +117,7 @@ fn test_derive_from_tuple_to_string() {
("string_utf8", clarity!(StringUtf8, "hello \u{1234}"))
);

let parsed = Payload::from_tuple(data).unwrap();
let parsed = Payload::try_from(data).unwrap();

assert_eq!(parsed.int, "1");
assert_eq!(parsed.uint, "u1");
Expand Down

0 comments on commit 9b3cc05

Please sign in to comment.