use serde::Serializer;
use uuid::Uuid;
pub fn parse_price(px: f64) -> String {
let px = format!("{px:.6}");
let px = if px.starts_with("0.") {
px
} else {
let px: Vec<&str> = px.split('.').collect();
let whole = px[0];
let decimals = px[1];
let diff = 5 - whole.len(); let sep = if diff > 0 { "." } else { "" };
format!("{whole}{sep}{decimals:.0$}", diff)
};
let px = remove_trailing_zeros(&px);
positive(px)
}
pub fn parse_size(sz: f64, sz_decimals: u32) -> String {
let sz = format!("{sz:.0$}", sz_decimals as usize);
let px = remove_trailing_zeros(&sz);
positive(px)
}
fn remove_trailing_zeros(s: &str) -> String {
let mut s = s.to_string();
while s.ends_with('0') && s.contains('.') {
s.pop();
}
if s.ends_with('.') {
s.pop();
}
s
}
fn positive(value: String) -> String {
if value.starts_with('-') {
"0".to_string()
} else {
value
}
}
pub fn as_hex_option<S>(cloid: &Option<Uuid>, s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
if let Some(cloid) = cloid {
s.serialize_str(&format!("0x{}", cloid.simple()))
} else {
s.serialize_none()
}
}
pub fn as_hex<S>(cloid: &Uuid, s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
s.serialize_str(&format!("0x{}", cloid.simple()))
}