Skip to main content

optionstratlib_core/model/
positive_ext.rs

1/******************************************************************************
2   Author: Joaquín Béjar García
3   Email: jb@taunais.com
4   Date: 25/12/24
5******************************************************************************/
6
7//! Extensions for the Positive type specific to OptionStratLib.
8//!
9//! This module provides additional conversions and trait implementations
10//! for the `Positive` type that are specific to the options trading domain.
11
12use crate::model::utils::ToRound;
13use positive::Positive;
14use rust_decimal::Decimal;
15
16impl ToRound for Positive {
17    fn round(&self) -> Decimal {
18        Positive::round(self).to_dec()
19    }
20
21    fn round_to(&self, decimal_places: u32) -> Decimal {
22        Positive::round_to(self, decimal_places).to_dec()
23    }
24}
25
26#[cfg(test)]
27mod tests {
28    use super::*;
29    use positive::pos_or_panic;
30
31    #[test]
32    fn test_to_round_trait() {
33        let p = pos_or_panic!(1.567);
34        assert_eq!(p.round(), Decimal::from(2));
35        assert_eq!(p.round_to(1), Decimal::new(16, 1));
36    }
37}