Skip to content

Commit 288a5b6

Browse files
authored
feat(rust/cardano-blockchain-types): Add Network variants index (#165)
1 parent 581121b commit 288a5b6

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

rust/cardano-blockchain-types/src/network.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,15 @@ pub(crate) const ENVVAR_MITHRIL_EXE_NAME: &str = "MITHRIL_EXE_NAME";
3737
strum::Display,
3838
)]
3939
#[strum(ascii_case_insensitive)]
40+
#[strum(serialize_all = "snake_case")]
41+
#[repr(usize)]
4042
pub enum Network {
4143
/// Cardano mainnet network.
42-
Mainnet,
44+
Mainnet = 0,
4345
/// Cardano pre-production network.
44-
Preprod,
46+
Preprod = 1,
4547
/// Cardano preview network.
46-
Preview,
48+
Preview = 2,
4749
}
4850

4951
// Mainnet Defaults.
@@ -224,6 +226,7 @@ mod tests {
224226

225227
use anyhow::Ok;
226228
use chrono::{TimeZone, Utc};
229+
use strum::VariantNames;
227230

228231
use super::*;
229232

@@ -248,6 +251,23 @@ mod tests {
248251
Ok(())
249252
}
250253

254+
#[test]
255+
fn test_variant_to_usize() {
256+
assert_eq!(Network::Mainnet as usize, 0);
257+
assert_eq!(Network::Preprod as usize, 1);
258+
assert_eq!(Network::Preview as usize, 2);
259+
}
260+
261+
#[test]
262+
fn test_variant_to_string() {
263+
assert_eq!(Network::Mainnet.to_string(), "mainnet");
264+
assert_eq!(Network::Preprod.to_string(), "preprod");
265+
assert_eq!(Network::Preview.to_string(), "preview");
266+
267+
let expected_names = ["mainnet", "preprod", "preview"];
268+
assert_eq!(Network::VARIANTS, expected_names);
269+
}
270+
251271
#[test]
252272
fn test_time_to_slot_before_blockchain() {
253273
let network = Network::Mainnet;

0 commit comments

Comments
 (0)