1#[cfg(feature = "python")]
2use pyo3::prelude::*;
3
4use measurements::Power;
5use serde::{Deserialize, Serialize};
6use std::{
7 fmt::{Display, Formatter},
8 ops::Div,
9};
10
11#[cfg_attr(feature = "python", pyclass(str, module = "asic_rs"))]
12#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
13pub enum HashRateUnit {
14 Hash,
15 KiloHash,
16 MegaHash,
17 GigaHash,
18 #[default]
19 TeraHash,
20 PetaHash,
21 ExaHash,
22 ZettaHash,
23 YottaHash,
24}
25
26impl HashRateUnit {
27 fn to_multiplier(&self) -> f64 {
28 match self {
29 HashRateUnit::Hash => 1e0,
30 HashRateUnit::KiloHash => 1e3,
31 HashRateUnit::MegaHash => 1e6,
32 HashRateUnit::GigaHash => 1e9,
33 HashRateUnit::TeraHash => 1e12,
34 HashRateUnit::PetaHash => 1e15,
35 HashRateUnit::ExaHash => 1e18,
36 HashRateUnit::ZettaHash => 1e21,
37 HashRateUnit::YottaHash => 1e24,
38 }
39 }
40}
41
42impl Display for HashRateUnit {
43 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
44 match self {
45 HashRateUnit::Hash => write!(f, "H/s"),
46 HashRateUnit::KiloHash => write!(f, "KH/s"),
47 HashRateUnit::MegaHash => write!(f, "MH/s"),
48 HashRateUnit::GigaHash => write!(f, "GH/s"),
49 HashRateUnit::TeraHash => write!(f, "TH/s"),
50 HashRateUnit::PetaHash => write!(f, "PH/s"),
51 HashRateUnit::ExaHash => write!(f, "EH/s"),
52 HashRateUnit::ZettaHash => write!(f, "ZH/s"),
53 HashRateUnit::YottaHash => write!(f, "YH/s"),
54 }
55 }
56}
57
58#[cfg_attr(feature = "python", pyclass(get_all, module = "asic_rs"))]
59#[derive(Debug, Clone, Serialize, Deserialize)]
60pub struct HashRate {
61 pub value: f64,
63 pub unit: HashRateUnit,
65 pub algo: String,
67}
68
69impl HashRate {
70 pub fn as_unit(self, unit: HashRateUnit) -> Self {
71 let base = self.value * self.unit.to_multiplier(); Self {
74 value: base / unit.clone().to_multiplier(),
75 unit,
76 algo: self.algo,
77 }
78 }
79}
80
81impl Display for HashRate {
82 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
83 let precision = f.precision();
84
85 match precision {
86 Some(precision) => {
87 write!(f, "{:.*} {}", precision, self.value, self.unit)
88 }
89 None => {
90 write!(f, "{} {}", self.value, self.unit)
91 }
92 }
93 }
94}
95
96impl PartialEq for HashRate {
97 fn eq(&self, other: &Self) -> bool {
98 other.clone().as_unit(self.unit.clone()).value == self.value
99 }
100}
101
102impl Eq for HashRate {}
103
104impl Div<HashRate> for Power {
105 type Output = f64;
106
107 fn div(self, hash_rate: HashRate) -> Self::Output {
108 self.as_watts() / hash_rate.value
109 }
110}