Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delegate newtype EOC #163

Merged
merged 3 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions macros/src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,18 @@ pub fn derive_struct_impl(
}
} else {
quote! {
<#ty as #crate_root::Decode>::decode_with_tag_and_constraints(
decoder,
tag,
<#ty as #crate_root::AsnType>::CONSTRAINTS.override_constraints(constraints),
).map(Self)
match tag {
#crate_root::Tag::EOC => {
Ok(Self(<#ty>::decode(decoder)?))
}
_ => {
<#ty as #crate_root::Decode>::decode_with_tag_and_constraints(
decoder,
tag,
<#ty as #crate_root::AsnType>::CONSTRAINTS.override_constraints(constraints),
).map(Self)
}
}
}
}
} else if config.set {
Expand Down
19 changes: 13 additions & 6 deletions macros/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,19 @@ pub fn derive_struct_impl(
}
} else {
quote!(
<#ty as #crate_root::Encode>::encode_with_tag_and_constraints(
&self.0,
encoder,
tag,
<#ty as #crate_root::AsnType>::CONSTRAINTS.override_constraints(constraints)
)
match tag {
#crate_root::Tag::EOC => {
self.0.encode(encoder)
}
_ => {
<#ty as #crate_root::Encode>::encode_with_tag_and_constraints(
&self.0,
encoder,
tag,
<#ty as #crate_root::AsnType>::CONSTRAINTS.override_constraints(constraints)
)
}
}
)
}
} else {
Expand Down
27 changes: 27 additions & 0 deletions tests/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,33 @@ pub struct BasicConstraints {
pub path_len_constraint: Option<Integer>,
}

// This test makes sure that Newtype3(Newtype2(Newtype1(T))) results in serializing
// as T when using the #[rasn(delegate)] attribute when T is a non-universal type.
#[test]
fn delegated_newtype_wrapping() {
#[derive(AsnType, Debug, Decode, Encode, PartialEq)]
#[rasn(choice)]
enum Hash {
#[rasn(tag(explicit(0)))]
Sha256(String),
}

#[derive(AsnType, Debug, Decode, Encode, PartialEq)]
#[rasn(delegate)]
struct TransactionID(Hash);

#[derive(AsnType, Debug, Decode, Encode, PartialEq)]
#[rasn(delegate)]
struct PolicyID(TransactionID);

let policy_id1 = PolicyID(TransactionID(Hash::Sha256("abcdef".into())));

let ser = rasn::der::encode(&policy_id1).unwrap();
assert_eq!(&ser[..], &[160, 8, 12, 6, 97, 98, 99, 100, 101, 102]);
let policy_id2: PolicyID = rasn::der::decode(&ser[..]).unwrap();
assert_eq!(policy_id1, policy_id2);
}

// This test will fail to compile if `Result` is used in the derive/proc macros instead of
// `core::result::Result`
#[test]
Expand Down
Loading