From 90a7aa2242a9bb5cfffa581d1a7b8f85f0535764 Mon Sep 17 00:00:00 2001 From: Michael Jeffrey Date: Tue, 7 May 2024 14:01:56 -0700 Subject: [PATCH] use BoostedHexDeviceType proto enum There was not enough custom functionality to warrant owning a BoostedHexDeviceType enum of our own. --- mobile_config/src/boosted_hex_info.rs | 78 +++++---------------------- 1 file changed, 13 insertions(+), 65 deletions(-) diff --git a/mobile_config/src/boosted_hex_info.rs b/mobile_config/src/boosted_hex_info.rs index 85b00ab8b..0e31bb548 100644 --- a/mobile_config/src/boosted_hex_info.rs +++ b/mobile_config/src/boosted_hex_info.rs @@ -3,13 +3,12 @@ use chrono::{DateTime, Duration, Utc}; use file_store::traits::TimestampDecode; use futures::stream::{BoxStream, StreamExt}; use helium_proto::services::poc_mobile::BoostedHex as BoostedHexProto; -use helium_proto::BoostedHexDeviceTypeV1 as BoostedHexDeviceTypeProto; use helium_proto::BoostedHexInfoV1 as BoostedHexInfoProto; use hextree::Cell; use solana_sdk::pubkey::Pubkey; -use std::str::FromStr; use std::{collections::HashMap, convert::TryFrom, num::NonZeroU32}; +pub use helium_proto::BoostedHexDeviceTypeV1 as BoostedHexDeviceType; pub type BoostedHexInfoStream = BoxStream<'static, BoostedHexInfo>; lazy_static::lazy_static! { @@ -54,7 +53,7 @@ impl TryFrom for BoostedHexInfo { boosted_hex_pubkey, boost_config_pubkey, version: v.version, - device_type: device_type.into(), + device_type, }) } } @@ -113,64 +112,13 @@ impl BoostedHexInfo { } } -#[derive(Debug, Clone, PartialEq, Eq)] -pub enum BoostedHexDeviceType { - All, - CbrsIndoor, - CbrsOutdoor, - WifiIndoor, - WifiOutdoor, -} - -impl From for BoostedHexDeviceType { - fn from(device_type_proto: BoostedHexDeviceTypeProto) -> Self { - match device_type_proto { - BoostedHexDeviceTypeProto::All => Self::All, - BoostedHexDeviceTypeProto::CbrsIndoor => Self::CbrsIndoor, - BoostedHexDeviceTypeProto::CbrsOutdoor => Self::CbrsOutdoor, - BoostedHexDeviceTypeProto::WifiIndoor => Self::WifiIndoor, - BoostedHexDeviceTypeProto::WifiOutdoor => Self::WifiOutdoor, - } - } -} - -impl From for BoostedHexDeviceTypeProto { - fn from(device_type: BoostedHexDeviceType) -> Self { - match device_type { - BoostedHexDeviceType::All => Self::All, - BoostedHexDeviceType::CbrsIndoor => Self::CbrsIndoor, - BoostedHexDeviceType::CbrsOutdoor => Self::CbrsOutdoor, - BoostedHexDeviceType::WifiIndoor => Self::WifiIndoor, - BoostedHexDeviceType::WifiOutdoor => Self::WifiOutdoor, - } - } -} - -impl From for i32 { - fn from(device_type: BoostedHexDeviceType) -> Self { - BoostedHexDeviceTypeProto::from(device_type) as i32 - } -} - -impl TryFrom for BoostedHexDeviceType { - type Error = helium_proto::DecodeError; - - fn try_from(db_value: i32) -> Result { - Ok(BoostedHexDeviceTypeProto::try_from(db_value)?.into()) - } -} - -impl FromStr for BoostedHexDeviceType { - type Err = anyhow::Error; - - fn from_str(s: &str) -> Result { - match s { - "cbrsIndoor" => Ok(Self::CbrsIndoor), - "cbrsOutdoor" => Ok(Self::CbrsOutdoor), - "wifiIndoor" => Ok(Self::WifiIndoor), - "wifiOutdoor" => Ok(Self::WifiOutdoor), - unknown => anyhow::bail!("unknown device type value: {unknown}"), - } +fn device_type_from_str(s: &str) -> anyhow::Result { + match s { + "cbrsIndoor" => Ok(BoostedHexDeviceType::CbrsIndoor), + "cbrsOutdoor" => Ok(BoostedHexDeviceType::CbrsOutdoor), + "wifiIndoor" => Ok(BoostedHexDeviceType::WifiIndoor), + "wifiOutdoor" => Ok(BoostedHexDeviceType::WifiOutdoor), + unknown => anyhow::bail!("unknown device type value: {unknown}"), } } @@ -367,7 +315,7 @@ pub(crate) mod db { let device_type_jsonb = row.get::("device_type"); let device_type = match device_type_jsonb { None => super::BoostedHexDeviceType::All, - Some(val) => super::BoostedHexDeviceType::from_str(val.deref()) + Some(val) => super::device_type_from_str(val.deref()) .map_err(|e| sqlx::Error::Decode(e.into()))?, }; @@ -490,7 +438,7 @@ mod tests { .to_bytes() .to_vec(), version: 1, - device_type: BoostedHexDeviceTypeProto::All.into(), + device_type: BoostedHexDeviceType::All.into(), }; let msg = BoostedHexInfo::try_from(proto)?; @@ -532,7 +480,7 @@ mod tests { .to_bytes() .to_vec(), version: 1, - device_type: BoostedHexDeviceTypeProto::All.into(), + device_type: BoostedHexDeviceType::All.into(), }; let msg = BoostedHexInfo::try_from(proto)?; @@ -568,7 +516,7 @@ mod tests { boosted_hex_pubkey: BOOST_HEX_PUBKEY.as_bytes().to_vec(), boost_config_pubkey: BOOST_HEX_CONFIG_PUBKEY.as_bytes().to_vec(), version: 1, - device_type: BoostedHexDeviceTypeProto::All.into(), + device_type: BoostedHexDeviceType::All.into(), }; assert_eq!( "multipliers cannot contain values of 0",