From 288a5b61a9b77603145fdbb3e6ff2e58b0cbda7f Mon Sep 17 00:00:00 2001 From: Apisit Ritruengroj <38898766+apskhem@users.noreply.github.com> Date: Tue, 21 Jan 2025 17:36:59 +0700 Subject: [PATCH] feat(rust/cardano-blockchain-types): Add Network variants index (#165) --- rust/cardano-blockchain-types/src/network.rs | 26 +++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/rust/cardano-blockchain-types/src/network.rs b/rust/cardano-blockchain-types/src/network.rs index 35b687a383..536fdc0b16 100644 --- a/rust/cardano-blockchain-types/src/network.rs +++ b/rust/cardano-blockchain-types/src/network.rs @@ -37,13 +37,15 @@ pub(crate) const ENVVAR_MITHRIL_EXE_NAME: &str = "MITHRIL_EXE_NAME"; strum::Display, )] #[strum(ascii_case_insensitive)] +#[strum(serialize_all = "snake_case")] +#[repr(usize)] pub enum Network { /// Cardano mainnet network. - Mainnet, + Mainnet = 0, /// Cardano pre-production network. - Preprod, + Preprod = 1, /// Cardano preview network. - Preview, + Preview = 2, } // Mainnet Defaults. @@ -224,6 +226,7 @@ mod tests { use anyhow::Ok; use chrono::{TimeZone, Utc}; + use strum::VariantNames; use super::*; @@ -248,6 +251,23 @@ mod tests { Ok(()) } + #[test] + fn test_variant_to_usize() { + assert_eq!(Network::Mainnet as usize, 0); + assert_eq!(Network::Preprod as usize, 1); + assert_eq!(Network::Preview as usize, 2); + } + + #[test] + fn test_variant_to_string() { + assert_eq!(Network::Mainnet.to_string(), "mainnet"); + assert_eq!(Network::Preprod.to_string(), "preprod"); + assert_eq!(Network::Preview.to_string(), "preview"); + + let expected_names = ["mainnet", "preprod", "preview"]; + assert_eq!(Network::VARIANTS, expected_names); + } + #[test] fn test_time_to_slot_before_blockchain() { let network = Network::Mainnet;