Skip to content

Commit 921baf2

Browse files
shekhirinyongkangc
authored andcommitted
test(static-file): StaticFileSegment string and serde roundtrips (#19561)
1 parent 0bd775b commit 921baf2

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/static-file/types/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ strum = { workspace = true, features = ["derive"] }
2121

2222
[dev-dependencies]
2323
reth-nippy-jar.workspace = true
24+
serde_json.workspace = true
2425

2526
[features]
2627
default = ["std"]
@@ -29,5 +30,6 @@ std = [
2930
"derive_more/std",
3031
"serde/std",
3132
"strum/std",
33+
"serde_json/std",
3234
]
3335
clap = ["dep:clap"]

crates/static-file/types/src/segment.rs

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use alloy_primitives::TxNumber;
77
use core::{ops::RangeInclusive, str::FromStr};
88
use derive_more::Display;
99
use serde::{Deserialize, Serialize};
10-
use strum::{AsRefStr, EnumIs, EnumString};
10+
use strum::{EnumIs, EnumString};
1111

1212
#[derive(
1313
Debug,
@@ -21,28 +21,27 @@ use strum::{AsRefStr, EnumIs, EnumString};
2121
Deserialize,
2222
Serialize,
2323
EnumString,
24-
AsRefStr,
2524
Display,
2625
EnumIs,
2726
)]
27+
#[strum(serialize_all = "kebab-case")]
2828
#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
2929
/// Segment of the data that can be moved to static files.
3030
pub enum StaticFileSegment {
31-
#[strum(serialize = "headers")]
3231
/// Static File segment responsible for the `CanonicalHeaders`, `Headers`,
3332
/// `HeaderTerminalDifficulties` tables.
3433
Headers,
35-
#[strum(serialize = "transactions")]
3634
/// Static File segment responsible for the `Transactions` table.
3735
Transactions,
38-
#[strum(serialize = "receipts")]
3936
/// Static File segment responsible for the `Receipts` table.
4037
Receipts,
4138
}
4239

4340
impl StaticFileSegment {
4441
/// Returns the segment as a string.
4542
pub const fn as_str(&self) -> &'static str {
43+
// `strum` doesn't generate a doc comment for `into_str` when using `IntoStaticStr` derive
44+
// macro, so we need to manually implement it.
4645
match self {
4746
Self::Headers => "headers",
4847
Self::Transactions => "transactions",
@@ -73,7 +72,7 @@ impl StaticFileSegment {
7372
pub fn filename(&self, block_range: &SegmentRangeInclusive) -> String {
7473
// ATTENTION: if changing the name format, be sure to reflect those changes in
7574
// [`Self::parse_filename`].
76-
format!("static_file_{}_{}_{}", self.as_ref(), block_range.start(), block_range.end())
75+
format!("static_file_{}_{}_{}", self.as_str(), block_range.start(), block_range.end())
7776
}
7877

7978
/// Returns file name for the provided segment and range, alongside filters, compression.
@@ -473,4 +472,36 @@ mod tests {
473472
);
474473
}
475474
}
475+
476+
/// Used in filename writing/parsing
477+
#[test]
478+
fn test_static_file_segment_str_roundtrip() {
479+
for segment in StaticFileSegment::iter() {
480+
let static_str = segment.as_str();
481+
assert_eq!(StaticFileSegment::from_str(static_str).unwrap(), segment);
482+
483+
let expected_str = match segment {
484+
StaticFileSegment::Headers => "headers",
485+
StaticFileSegment::Transactions => "transactions",
486+
StaticFileSegment::Receipts => "receipts",
487+
};
488+
assert_eq!(static_str, expected_str);
489+
}
490+
}
491+
492+
/// Used in segment headers serialize/deserialize
493+
#[test]
494+
fn test_static_file_segment_serde_roundtrip() {
495+
for segment in StaticFileSegment::iter() {
496+
let ser = serde_json::to_string(&segment).unwrap();
497+
assert_eq!(serde_json::from_str::<StaticFileSegment>(&ser).unwrap(), segment);
498+
499+
let expected_str = match segment {
500+
StaticFileSegment::Headers => "Headers",
501+
StaticFileSegment::Transactions => "Transactions",
502+
StaticFileSegment::Receipts => "Receipts",
503+
};
504+
assert_eq!(ser, format!("\"{expected_str}\""));
505+
}
506+
}
476507
}

0 commit comments

Comments
 (0)