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

feat: introduce no-cid-as-bytes feature #23

Merged
merged 4 commits into from
Feb 23, 2024
Merged
Changes from 1 commit
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
58 changes: 42 additions & 16 deletions tests/cid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,30 @@ fn test_cid_not_as_bytes() {
from_slice::<serde_bytes::ByteBuf>(&cbor_cid[2..])
.expect("should have parsed an untagged CID as a byte array");

#[cfg(feature = "no-cid-as-bytes")]
{
#[derive(Debug, Deserialize)]
struct NewType(ByteBuf);
#[derive(Debug, Deserialize, PartialEq)]
struct NewType(ByteBuf);

#[derive(Debug, Deserialize)]
#[serde(untagged)]
enum BytesInEnum {
MyCid(NewType),
}
#[derive(Debug, Deserialize, PartialEq)]
#[serde(untagged)]
enum BytesInEnum {
MyCid(NewType),
}

// With the `no-cid-as-bytes` feature enabled, we make sure that it will error, when we try to
// decode a CID as bytes.
#[cfg(feature = "no-cid-as-bytes")]
from_slice::<BytesInEnum>(&cbor_cid)
.expect_err("shouldn't have parsed a tagged CID as byte array");

from_slice::<BytesInEnum>(&cbor_cid)
.expect_err("shouldn't have parsed a tagged CID as byte array");
// With that feature disabled, then it will decode the CID (without the TAG and the zero
// prefix) as bytes.
#[cfg(not(feature = "no-cid-as-bytes"))]
{
let cid_without_tag = &cbor_cid[5..];
assert_eq!(
from_slice::<BytesInEnum>(&cbor_cid).unwrap(),
BytesInEnum::MyCid(NewType(ByteBuf::from(cid_without_tag)))
vmx marked this conversation as resolved.
Show resolved Hide resolved
);
}
}

Expand Down Expand Up @@ -234,7 +245,6 @@ fn test_cid_in_kinded_enum_with_newtype() {
assert!(decoded_random_bytes.is_err());
}

#[cfg(not(feature = "no-cid-as-bytes"))]
#[test]
fn test_cid_in_tagged_enum() {
#[derive(Debug, Deserialize, PartialEq)]
Expand Down Expand Up @@ -277,11 +287,27 @@ fn test_cid_in_tagged_enum() {
let decoded: Externally = from_slice(&cbor_map1).unwrap();
assert_eq!(decoded, Externally::Cid(cid));

let decoded: Internally = from_slice(&cbor_map2).unwrap();
assert_eq!(decoded, Internally::Cid { cid });
// With the `no-cid-as-bytes` feature enabled, it's not possible to use internally tagged or
// untaggd enums. This behaviour is *not* intentionally, but incidentally due to how Serde
// internally works.. This test is only added to see what one could expect, and to get
// notified in case it ever gets supported.
#[cfg(feature = "no-cid-as-bytes")]
{
from_slice::<Internally>(&cbor_map2)
.expect_err("shouldn't be able to decode the intanlly tagged enum");
from_slice::<Untagged>(&cbor_cid)
.expect_err("shouldn't be able to decode the untagged enum");
}

// With that feature disabled, it's the expected desired behaviour.
#[cfg(not(feature = "no-cid-as-bytes"))]
{
let decoded: Internally = from_slice(&cbor_map2).unwrap();
assert_eq!(decoded, Internally::Cid { cid });

let decoded: Untagged = from_slice(&cbor_cid).unwrap();
assert_eq!(decoded, Untagged::Cid(cid));
let decoded: Untagged = from_slice(&cbor_cid).unwrap();
assert_eq!(decoded, Untagged::Cid(cid));
}
}

#[test]
Expand Down
Loading