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

introduce Slot newtype for beacon api types #3359

Open
benluelo opened this issue Dec 10, 2024 · 0 comments
Open

introduce Slot newtype for beacon api types #3359

benluelo opened this issue Dec 10, 2024 · 0 comments
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

Comments

@benluelo
Copy link
Contributor

benluelo commented Dec 10, 2024

this has bitten me too many times

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, ssz::Ssz)]
struct Slot(u64);

to match the beacon api specification for slot json ser/de, this type should serialize via string (see

pub mod string {
use alloc::string::String;
use core::{fmt::Display, str::FromStr};
use serde::{de::Deserialize, Deserializer, Serialize, Serializer};
pub fn serialize<S, T>(data: T, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
T: Display + Serialize,
{
if serializer.is_human_readable() {
serializer.collect_str(&data)
} else {
data.serialize(serializer)
}
}
pub fn deserialize<'de, D, T>(deserializer: D) -> Result<T, D::Error>
where
D: Deserializer<'de>,
T: FromStr + Deserialize<'de>,
{
if deserializer.is_human_readable() {
String::deserialize(deserializer).and_then(|s| {
s.parse()
// TODO fix error situation
// FromStr::Err has no bounds
.map_err(|_| serde::de::Error::custom("failure to parse string data"))
})
} else {
T::deserialize(deserializer)
}
}
}
for an example of how, note that it only serializes via string if the de/serializer is human readable).

this type should be added in this file: https://github.com/unionlabs/union/blob/e652dd1905e77d0138fb68b351cb17248d830689/lib/beacon-api-types/src/lib.rs

all fields that represent a slot should be changed to this type.

@benluelo benluelo added C-enhancement Category: An issue proposing an enhancement or a PR with one. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. labels Dec 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
Projects
None yet
Development

No branches or pull requests

1 participant