qust/idct/
pms.rs

1use crate::{
2    idct::{
3        dcon::Convert::{self, *},
4        part::Part,
5        ta::{ForeTa, Ta},
6    },
7    trade::di::Di,
8};
9use qust_ds::prelude::*;
10use qust_derive::*;
11
12// #[ta_derive]
13#[derive(Clone, Serialize, Deserialize, AsRef, PartialEq, Eq, Hash)]
14pub struct PmsType<T, N> {
15    pub dcon: T,
16    pub part: Part,
17    pub fore: N,
18}
19pub type Pms = PmsType<Convert, Box<dyn Ta>>;
20pub type PmsVert = PmsType<(Convert, Convert), Box<dyn Ta>>;
21impl std::fmt::Debug for Pms {
22    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23        write!(f, "{:?} + {:?} + {:?}", self.dcon, self.part, self.fore,)
24    }
25}
26impl std::fmt::Debug for PmsVert {
27    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28        write!(
29            f,
30            "{:?} -> {:?} + {:?} + {:?}",
31            self.dcon.0, self.dcon.1, self.part, self.fore,
32        )
33    }
34}
35
36use std::ops::Add;
37
38use super::{fore::ForeTaCalc, prelude::TaBox};
39
40impl Add for Convert {
41    type Output = Convert;
42    fn add(self, rhs: Self) -> Self::Output {
43        PreNow(Box::new(self), Box::new(rhs))
44    }
45}
46
47impl Add<Part> for Convert {
48    type Output = Pre;
49    fn add(self, rhs: Part) -> Self::Output {
50        Pre(self, rhs)
51    }
52}
53
54#[ta_derive]
55pub struct Pre(pub Convert, pub Part);
56
57impl<T: Ta + Clone> Add<T> for Pre {
58    type Output = Pms;
59    fn add(self, rhs: T) -> Self::Output {
60        PmsType {
61            dcon: self.0,
62            part: self.1,
63            fore: Box::new(rhs),
64        }
65    }
66}
67
68impl Add<TaBox> for Pre {
69    type Output = Pms;
70    fn add(self, rhs: TaBox) -> Self::Output {
71        PmsType {
72            dcon: self.0,
73            part: self.1,
74            fore: rhs
75        }
76    }
77}
78
79impl<T: ForeTaCalc> Add<T> for Pms {
80    type Output = Pms;
81    fn add(self, rhs: T) -> Self::Output {
82        let box_t: Box<dyn ForeTaCalc> = Box::new(rhs);
83        let t = ForeTa(self.fore, box_t);
84        PmsType {
85            dcon: self.dcon,
86            part: self.part,
87            fore: Box::new(t),
88        }
89    }
90}
91
92/* #region GetPmsFromTa */
93pub trait GetPmsFromTa: Send + Sync + std::fmt::Debug + Clone + 'static {
94    fn get_pms_from_ta(&self, di: &Di) -> Pms;
95}
96
97impl<T: Ta + Clone> GetPmsFromTa for T {
98    fn get_pms_from_ta(&self, di: &Di) -> Pms {
99        let ta_box: Box<dyn Ta> = Box::new(self.clone());
100        ta_box.get_pms_from_ta(di)
101    }
102}
103
104impl GetPmsFromTa for Box<dyn Ta> {
105    fn get_pms_from_ta(&self, di: &Di) -> Pms {
106        PmsType {
107            dcon: di.last_dcon(),
108            part: di.last_part(),
109            fore: self.clone(),
110        }
111    }
112}
113
114impl<T: Ta + Clone> GetPmsFromTa for (Part, T) {
115    fn get_pms_from_ta(&self, di: &Di) -> Pms {
116        let ta_box: Box<dyn Ta> = Box::new(self.1.clone());
117        (self.0.clone(), ta_box).get_pms_from_ta(di)
118    }
119}
120
121impl GetPmsFromTa for (Part, Box<dyn Ta>) {
122    fn get_pms_from_ta(&self, di: &Di) -> Pms {
123        PmsType {
124            dcon: di.last_dcon(),
125            part: self.0.clone(),
126            fore: self.1.clone(),
127        }
128    }
129}
130/* #endregion */