df_hyperliquid_core/deploy/spot/
share.rs1use std::{fmt, str::FromStr};
5
6use crate::deploy::error::BadDeploy;
7use crate::utils::Dec;
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
13pub struct FeeShare(Dec);
14
15impl FeeShare {
16 pub const ALL: FeeShare = FeeShare(Dec::lit::<0>(100));
18
19 pub fn percent(percent: Dec) -> Result<Self, BadDeploy> {
21 if percent.is_negative() || percent > Dec::from_int(100) {
22 return Err(BadDeploy::FeeShare(format!("{percent}%")));
23 }
24 Ok(FeeShare(percent))
25 }
26
27 #[must_use]
29 pub const fn value(self) -> Dec {
30 self.0
31 }
32
33 #[must_use]
35 pub fn to_wire(self) -> String {
36 format!("{}%", self.0.to_wire())
37 }
38}
39
40impl FromStr for FeeShare {
41 type Err = BadDeploy;
42
43 fn from_str(text: &str) -> Result<Self, Self::Err> {
45 let bad = || BadDeploy::FeeShare(text.trim().to_owned());
46 let number = text.trim().strip_suffix('%').ok_or_else(bad)?;
47 if number.starts_with(['+', '-']) || number.trim() != number {
48 return Err(bad());
49 }
50 let percent = Dec::parse(number).map_err(|_| bad())?;
51 FeeShare::percent(percent).map_err(|_| bad())
52 }
53}
54
55impl fmt::Display for FeeShare {
56 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57 write!(f, "{}%", self.0)
58 }
59}