Skip to content

Commit

Permalink
refactor: replace native implementation of this_or_that
Browse files Browse the repository at this point in the history
  • Loading branch information
njgheorghita committed Jan 18, 2024
1 parent d1af102 commit 3156eb0
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 19 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions light-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ reqwest = { version = "0.11.13", default-features = false, features = ["json", "
serde = { version = "1.0.143", features = ["derive"] }
serde_json = "1.0.85"
serde_yaml = "0.9.14"
serde-this-or-that = "0.4.2"
ssz-rs = { git = "https://github.com/ralexstokes/ssz-rs", rev = "d09f55b4f8554491e3431e01af1c32347a8781cd" }
ssz_types = "0.5.4"
strum = { version = "0.25.0", features = ["derive"] }
Expand Down
23 changes: 4 additions & 19 deletions light-client/src/config/checkpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use std::collections::HashMap;

use anyhow::anyhow;
use ethereum_types::H256;
use serde::{Deserialize, Deserializer, Serialize};
use serde::{Deserialize, Serialize};
use serde_this_or_that::as_u64;

use crate::config::networks;

Expand All @@ -21,31 +22,15 @@ pub struct RawSlotResponseData {

#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Slot {
#[serde(deserialize_with = "str_or_u64")]
#[serde(deserialize_with = "as_u64")]
pub slot: u64,
pub block_root: Option<H256>,
pub state_root: Option<H256>,
#[serde(deserialize_with = "as_u64")]
pub epoch: u64,
pub time: StartEndTime,
}

fn str_or_u64<'de, D>(deserializer: D) -> Result<u64, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
#[serde(untagged)]
enum StrOrU64<'a> {
Str(&'a str),
U64(u64),
}

Ok(match StrOrU64::deserialize(deserializer)? {
StrOrU64::Str(v) => v.parse().unwrap_or(0), // Ignoring parsing errors
StrOrU64::U64(v) => v,
})
}

#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct StartEndTime {
/// An ISO 8601 formatted UTC timestamp.
Expand Down

0 comments on commit 3156eb0

Please sign in to comment.