Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions asic-rs-core/src/data/hashrate.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{
fmt::{Display, Formatter},
ops::Div,
str::FromStr,
};

use measurements::Power;
Expand Down Expand Up @@ -39,6 +40,27 @@ impl HashRateUnit {
}
}

impl FromStr for HashRateUnit {
type Err = ();

fn from_str(s: &str) -> Result<Self, Self::Err> {
let normalized = s.trim().to_ascii_uppercase().replace([' ', '_'], "");

match normalized.as_str() {
"HASH" | "H" | "HS" | "H/S" => Ok(HashRateUnit::Hash),
"KILOHASH" | "KH" | "KHS" | "KH/S" => Ok(HashRateUnit::KiloHash),
"MEGAHASH" | "MH" | "MHS" | "MH/S" => Ok(HashRateUnit::MegaHash),
"GIGAHASH" | "GH" | "GHS" | "GH/S" => Ok(HashRateUnit::GigaHash),
"TERAHASH" | "TH" | "THS" | "TH/S" => Ok(HashRateUnit::TeraHash),
"PETAHASH" | "PH" | "PHS" | "PH/S" => Ok(HashRateUnit::PetaHash),
"EXAHASH" | "EH" | "EHS" | "EH/S" => Ok(HashRateUnit::ExaHash),
"ZETTAHASH" | "ZH" | "ZHS" | "ZH/S" => Ok(HashRateUnit::ZettaHash),
"YOTTAHASH" | "YH" | "YHS" | "YH/S" => Ok(HashRateUnit::YottaHash),
_ => Err(()),
}
}
}

impl Display for HashRateUnit {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Expand Down
Loading