plutus_ledger_api/v3/
ratio.rs

1//! Types related to rational values
2
3#[cfg(feature = "lbf")]
4use lbr_prelude::json::Json;
5use num_bigint::BigInt;
6#[cfg(feature = "serde")]
7use serde::{Deserialize, Serialize};
8
9use crate::plutus_data::{IsPlutusData, PlutusData, PlutusDataError};
10
11// TODO(chfanghr): maintain the invariants mentioned here: https://github.com/IntersectMBO/plutus/blob/master/plutus-tx/src/PlutusTx/Ratio.hs#L65-L68
12/// Represents an arbitrary-precision ratio.
13#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
14#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
15#[cfg_attr(feature = "lbf", derive(Json))]
16pub struct Rational(
17    /// numerator
18    pub BigInt,
19    /// denominator
20    pub BigInt,
21);
22
23impl IsPlutusData for Rational {
24    fn to_plutus_data(&self) -> PlutusData {
25        (self.0.clone(), self.1.clone()).to_plutus_data()
26    }
27
28    fn from_plutus_data(plutus_data: &PlutusData) -> Result<Self, PlutusDataError>
29    where
30        Self: Sized,
31    {
32        let (n, d) = IsPlutusData::from_plutus_data(plutus_data)?;
33
34        Ok(Self(n, d))
35    }
36}