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

Ciborium and Cleanups #2

Merged
merged 9 commits into from
Mar 16, 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
12 changes: 8 additions & 4 deletions .github/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,20 @@ repository:
labels:
- name: github_actions
color: '#000000'
description: Changes to github actions or dependencies
description: Pull requests that update github actions or dependencies
- name: rust
color: '#f74c00'
description: Changes to rust code or dependencies
description: Pull requests that update rust code or dependencies
- name: python
color: '#4584b6'
description: Changes to python code or dependencies
description: Pull requests that update python code or dependencies
- name: go
color: '#29beb0'
description: Changes to golang code or dependencies
description: Pull requests that update golang code or dependencies

- name: dependencies
color: '#0366d6'
description: Pull requests that update a dependency file

- name: size/XS
color: '#00ed01'
Expand Down
23 changes: 17 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ rust-version = "1.67.1"
[profile.release]
lto = "thin"

[metadata.release]
[package.metadata.release]
shared-version = true
dev-version-ext = "beta.0"
consolidate-commits = true
Expand All @@ -32,10 +32,24 @@ pre-release-replacements = [
]

[features]
std = ["serde/std", "serde_cbor/std", "serde_with"]
test_utils = ["protobuf"]
std = ["serde/std", "ciborium/std"]
test_utils = ["dep:protobuf"]
jsonu64 = ["std", "dep:serde_with"]

[dependencies]
# These dependencies locked because the interaction between cibiorum and ciborium-io is garbage.
#
# cibiorum::ser::into_writer(&Vec<u8>) is infallible, but only because of how cibiorum-io is
# implemented, in version 0.2.0, when the alloc feature (which is not exposed by ciborium) is
# enabled.
#
# If you are considering using serde for any reason, consider the choices you have made in your
# life, and then use protobufs (via prost) instead.
ciborium = { version = "=0.2.0", default-features = false }
ciborium-io = { version = "=0.2.0", default-features = false, features = [
"alloc",
] }

prost = { version = "0.11", default-features = false, features = [
"prost-derive",
] }
Expand All @@ -44,9 +58,6 @@ serde = { version = "1.0", default-features = false, features = [
"alloc",
"derive",
] }
serde_cbor = { version = "0.11", default-features = false, features = [
"alloc",
] }
serde_with = { version = "2.0", default-features = false, features = [
"macros",
], optional = true }
Expand Down
83 changes: 83 additions & 0 deletions src/json_u64.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright (c) 2018-2022 The MobileCoin Foundation

//! Serialization and deserialization for a U64 in JSON

use serde::{Deserialize, Serialize};
use serde_with::{serde_as, DisplayFromStr};

/// Represents u64 using string, when serializing to Json
/// Javascript integers are not 64 bit, and so it is not really proper json.
/// Using string avoids issues with some json parsers not handling large
/// numbers well.
///
/// This does not rely on the serde-json arbitrary precision feature, which
/// (we fear) might break other things (e.g. https://github.com/serde-rs/json/issues/505)
#[serde_as]
#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, PartialEq, Hash, Serialize)]
#[serde(transparent)]
pub struct JsonU64(#[serde_as(as = "DisplayFromStr")] pub u64);

impl From<&u64> for JsonU64 {
fn from(src: &u64) -> Self {
Self(*src)
}
}

impl From<&JsonU64> for u64 {
fn from(src: &JsonU64) -> u64 {
src.0
}
}

impl From<JsonU64> for u64 {
fn from(src: JsonU64) -> u64 {
src.0
}
}

impl AsRef<u64> for JsonU64 {
fn as_ref(&self) -> &u64 {
&self.0
}
}

#[cfg(test)]
mod tests {
use super::*;
use alloc::vec::Vec;
use serde::{Deserialize, Serialize};

#[derive(PartialEq, Serialize, Deserialize, Debug)]
struct TestStruct {
nums: Vec<JsonU64>,
block: JsonU64,
}

#[test]
fn serialize_jsonu64_struct() {
let the_struct = TestStruct {
nums: [0, 1, 2, u64::MAX]
.iter()
.map(Into::<JsonU64>::into)
.collect(),
block: JsonU64(u64::MAX - 1),
};
let serialized = crate::serialize(&the_struct).unwrap();
let deserialized =
crate::deserialize::<TestStruct>(&serialized).expect("Could not deserialize struct");
assert_eq!(deserialized, the_struct);

// Sanity that serde_as works as expected: it should accept and hand us back
// strings.
let expected_json =
r#"{"nums":["0","1","2","18446744073709551615"],"block":"18446744073709551614"}"#;
assert_eq!(
expected_json,
serde_json::to_string(&the_struct).expect("Could not convert struct to json string")
);
assert_eq!(
the_struct,
serde_json::from_str(expected_json).expect("Could not convert json string to struct")
);
}
}
Loading