use std::{fmt, str::FromStr};
use crate::deploy::error::BadDeploy;
use crate::utils::Dec;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct FeeShare(Dec);
impl FeeShare {
pub const ALL: FeeShare = FeeShare(Dec::lit::<0>(100));
pub fn percent(percent: Dec) -> Result<Self, BadDeploy> {
if percent.is_negative() || percent > Dec::from_int(100) {
return Err(BadDeploy::FeeShare(format!("{percent}%")));
}
Ok(FeeShare(percent))
}
#[must_use]
pub const fn value(self) -> Dec {
self.0
}
#[must_use]
pub fn to_wire(self) -> String {
format!("{}%", self.0.to_wire())
}
}
impl FromStr for FeeShare {
type Err = BadDeploy;
fn from_str(text: &str) -> Result<Self, Self::Err> {
let bad = || BadDeploy::FeeShare(text.trim().to_owned());
let number = text.trim().strip_suffix('%').ok_or_else(bad)?;
if number.starts_with(['+', '-']) || number.trim() != number {
return Err(bad());
}
let percent = Dec::parse(number).map_err(|_| bad())?;
FeeShare::percent(percent).map_err(|_| bad())
}
}
impl fmt::Display for FeeShare {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}%", self.0)
}
}