df_hyperliquid_core/deploy/outcomes/actions/
bounded.rs1use std::{fmt, str::FromStr};
5
6use crate::deploy::outcomes::actions::error::DeployError;
7use crate::utils::Dec;
8
9fn bounded(text: &str, most: Dec) -> Option<Dec> {
12 let text = text.trim();
13 if text.starts_with(['+', '-']) {
14 return None;
15 }
16 let value = Dec::parse(text).ok()?;
17 (!value.is_negative() && value <= most).then_some(value)
18}
19
20#[derive(Debug, Clone, Copy, PartialEq, Eq)]
25pub struct FeeScale(Dec);
26
27impl FeeScale {
28 pub const ZERO: FeeScale = FeeScale(Dec::ZERO);
30 pub const ONE: FeeScale = FeeScale(Dec::ONE);
32
33 #[must_use]
35 pub const fn value(self) -> Dec {
36 self.0
37 }
38
39 #[must_use]
41 pub fn to_wire(self) -> String {
42 self.0.to_wire()
43 }
44}
45
46impl FromStr for FeeScale {
47 type Err = DeployError;
48
49 fn from_str(text: &str) -> Result<Self, Self::Err> {
52 bounded(text, Dec::from_int(10)).map(FeeScale).ok_or_else(|| DeployError::FeeScale(text.trim().to_owned()))
53 }
54}
55
56impl fmt::Display for FeeScale {
57 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58 write!(f, "{}", self.0)
59 }
60}
61
62#[derive(Debug, Clone, Copy, PartialEq, Eq)]
66pub struct Fraction(Dec);
67
68impl Fraction {
69 pub const ZERO: Fraction = Fraction(Dec::ZERO);
71 pub const ONE: Fraction = Fraction(Dec::ONE);
73
74 #[must_use]
76 pub const fn value(self) -> Dec {
77 self.0
78 }
79
80 #[must_use]
82 pub fn to_wire(self) -> String {
83 self.0.to_wire()
84 }
85
86 #[must_use]
88 pub fn is_whole(self) -> bool {
89 self == Fraction::ZERO || self == Fraction::ONE
90 }
91}
92
93impl FromStr for Fraction {
94 type Err = DeployError;
95
96 fn from_str(text: &str) -> Result<Self, Self::Err> {
98 bounded(text, Dec::ONE).map(Fraction).ok_or_else(|| DeployError::Fraction(text.trim().to_owned()))
99 }
100}
101
102impl fmt::Display for Fraction {
103 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
104 write!(f, "{}", self.0)
105 }
106}
107
108#[cfg(test)]
109#[path = "tests/bounded.rs"]
110mod tests;