#![cfg(feature = "decimal")]
use serde::{Deserialize, Serialize};
use crate::tag::{Tag, Tagged};
const TYPE_NAME: &str = "decimal";
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(try_from = "DecimalTypst", into = "DecimalTypst")]
pub struct Decimal(rust_decimal::Decimal);
impl Decimal {
pub fn new(value: &str) -> Result<Self, rust_decimal::Error> {
rust_decimal::Decimal::from_str_exact(value).map(Self)
}
}
impl std::ops::Deref for Decimal {
type Target = rust_decimal::Decimal;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
struct DecimalTypst {
typwire_type: Tag<Decimal>,
decimal: String,
}
impl Tagged for Decimal {
const TYPE_NAME: &'static str = TYPE_NAME;
}
impl From<Decimal> for DecimalTypst {
fn from(value: Decimal) -> Self {
Self {
typwire_type: Tag::new(),
decimal: value.0.to_string(),
}
}
}
impl TryFrom<DecimalTypst> for Decimal {
type Error = String;
fn try_from(value: DecimalTypst) -> Result<Self, Self::Error> {
Decimal::new(&value.decimal).map_err(|error| format!("invalid decimal: {error}"))
}
}
#[cfg(test)]
mod tests {
use crate::decode;
use super::*;
#[test]
fn invalid_decimal_fails_to_decode() {
let bytes = cbor4ii::serde::to_vec(
Vec::new(),
&DecimalTypst {
typwire_type: Tag::new(),
decimal: "not-a-number".to_string(),
},
)
.unwrap();
assert!(decode::<Decimal>(&bytes).is_err());
}
}