rateslib/scheduling/py/
imm.rs

1use crate::json::{DeserializedObj, JSON};
2use crate::scheduling::frequency::Imm;
3
4use chrono::prelude::*;
5use pyo3::exceptions::PyValueError;
6use pyo3::prelude::*;
7
8#[pymethods]
9impl Imm {
10    /// Return the next IMM date after ``date`` under the given definition.
11    ///
12    /// Parameters
13    /// ----------
14    /// date: datetime
15    ///     The input date.
16    ///
17    /// Returns
18    /// -------
19    /// datetime
20    #[pyo3(name = "next")]
21    fn next_py(&self, date: NaiveDateTime) -> NaiveDateTime {
22        self.next(&date)
23    }
24
25    /// Check whether a date is an IMM date under the given definition.
26    ///
27    /// Parameters
28    /// ----------
29    /// date: datetime
30    ///     The input date.
31    ///
32    /// Returns
33    /// -------
34    /// bool
35    #[pyo3(name = "validate")]
36    fn validate_py(&self, date: NaiveDateTime) -> bool {
37        self.validate(&date)
38    }
39
40    /// Return an IMM date from a given year and month under the given definition.
41    ///
42    /// Parameters
43    /// ----------
44    /// year: int
45    ///     The year.
46    /// month: int
47    ///     The month.
48    ///
49    /// Returns
50    /// -------
51    /// datetime
52    #[pyo3(name = "get")]
53    fn from_ym_opt_py(&self, year: i32, month: u32) -> PyResult<NaiveDateTime> {
54        self.from_ym_opt(year, month)
55    }
56
57    // JSON
58    /// Return a JSON representation of the object.
59    ///
60    /// Returns
61    /// -------
62    /// str
63    #[pyo3(name = "to_json")]
64    fn to_json_py(&self) -> PyResult<String> {
65        match DeserializedObj::Imm(self.clone()).to_json() {
66            Ok(v) => Ok(v),
67            Err(_) => Err(PyValueError::new_err("Failed to serialize `Imm` to JSON.")),
68        }
69    }
70
71    // Pickling
72    #[new]
73    fn new_py(item: usize) -> Imm {
74        match item {
75            _ if item == Imm::Wed3 as usize => Imm::Wed3,
76            _ if item == Imm::Wed3_HMUZ as usize => Imm::Wed3_HMUZ,
77            _ if item == Imm::Fri2 as usize => Imm::Fri2,
78            _ if item == Imm::Fri2_HMUZ as usize => Imm::Fri2_HMUZ,
79            _ if item == Imm::Day20 as usize => Imm::Day20,
80            _ if item == Imm::Day20_HU as usize => Imm::Day20_HU,
81            _ if item == Imm::Day20_MZ as usize => Imm::Day20_MZ,
82            _ if item == Imm::Day20_HMUZ as usize => Imm::Day20_HMUZ,
83            _ if item == Imm::Wed1_Post9 as usize => Imm::Wed1_Post9,
84            _ if item == Imm::Wed1_Post9_HMUZ as usize => Imm::Wed1_Post9_HMUZ,
85            _ if item == Imm::Eom as usize => Imm::Eom,
86            _ if item == Imm::Leap as usize => Imm::Leap,
87            _ => panic!("Reportable issue: must map this enum variant for serialization."),
88        }
89    }
90    fn __getnewargs__<'py>(&self) -> PyResult<(usize,)> {
91        Ok((*self as usize,))
92    }
93
94    fn __repr__(&self) -> String {
95        format!("<rl.Imm.{:?} at {:p}>", self, self)
96    }
97}