1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use serde::{Deserialize, Serialize};

pub trait One {
	fn one() -> Self;
}

pub trait Zero {
	fn zero() -> Self;
}

impl One for u128 {
	fn one() -> Self {
		1
	}
}

impl Zero for u128 {
	fn zero() -> Self {
		0
	}
}

impl ValueFormatter for u128 {
	fn format_scalar(&self) -> String {
		crate::Dimension::fmt_scalar(*self)
	}
}

pub trait ValueFormatter {
	fn format_scalar(&self) -> String;
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, PartialOrd, Ord, Eq)]
pub struct Weight {
	pub time: u128,
	pub proof: u128,
}

impl Zero for Weight {
	fn zero() -> Self {
		Self { time: 0, proof: 0 }
	}
}

impl One for Weight {
	fn one() -> Self {
		Self { time: 1, proof: 1 }
	}
}

impl From<(u128, u128)> for Weight {
	fn from((time, proof): (u128, u128)) -> Self {
		Self { time, proof }
	}
}

impl From<Weight> for (u128, u128) {
	fn from(val: Weight) -> Self {
		(val.time, val.proof)
	}
}

impl From<u128> for Weight {
	fn from(time: u128) -> Self {
		Self { time, proof: 0 }
	}
}

impl ValueFormatter for Weight {
	fn format_scalar(&self) -> String {
		format!("({}, {})", self.time, self.proof)
	}
}

impl core::fmt::Display for Weight {
	fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
		write!(f, "({}, {})", self.time, self.proof)
	}
}

impl Weight {
	pub fn mul_scalar(&self, other: u128) -> Self {
		Self { time: self.time * other, proof: self.proof * other }
	}
}