1use std::any::TypeId;
10use std::sync::Arc;
11use std::time::Duration;
12
13use dex_protocol::dex::Value as ProtoValue;
14
15use crate::SubFlowOptions;
16
17pub struct Wait {
35 pub(crate) kind: WaitKind,
36}
37
38pub(crate) enum WaitKind {
39 SkipImmediately,
40 AllOf(Vec<Condition>),
41 AnyOf(Vec<Condition>),
42 AnyCombinationOf(Vec<ConditionCombination>),
43}
44
45impl Wait {
46 pub fn skip_immediately() -> Self {
48 Self {
49 kind: WaitKind::SkipImmediately,
50 }
51 }
52
53 pub fn until(condition: Condition) -> Self {
55 Self::all_of([condition])
56 }
57
58 pub fn all_of(conditions: impl IntoIterator<Item = Condition>) -> Self {
60 Self {
61 kind: WaitKind::AllOf(conditions.into_iter().collect()),
62 }
63 }
64
65 pub fn any_of(conditions: impl IntoIterator<Item = Condition>) -> Self {
67 Self {
68 kind: WaitKind::AnyOf(conditions.into_iter().collect()),
69 }
70 }
71
72 pub fn any_combination_of(
77 combinations: impl IntoIterator<Item = ConditionCombination>,
78 ) -> Self {
79 Self {
80 kind: WaitKind::AnyCombinationOf(combinations.into_iter().collect()),
81 }
82 }
83}
84
85#[derive(Clone)]
90pub struct Condition {
91 pub(crate) identity: Arc<ConditionIdentity>,
92 pub(crate) id: Option<String>,
93 pub(crate) kind: ConditionKind,
94}
95
96pub(crate) struct ConditionIdentity;
97
98#[derive(Clone)]
99pub(crate) enum ConditionKind {
100 Timer(Duration),
101 Channel {
102 name: String,
103 instance: Option<String>,
104 at_least: Option<usize>,
105 at_most: Option<usize>,
106 },
107 SubFlow(Box<SubFlowDefinition>),
108}
109
110#[derive(Clone)]
111pub(crate) struct SubFlowDefinition {
112 pub(crate) flow_type: &'static str,
113 pub(crate) type_id: TypeId,
114 pub(crate) input: ProtoValue,
115 pub(crate) options: SubFlowOptions,
116}
117
118impl Condition {
119 pub(crate) fn timer(duration: Duration) -> Self {
120 Self {
121 identity: Arc::new(ConditionIdentity),
122 id: None,
123 kind: ConditionKind::Timer(duration),
124 }
125 }
126
127 pub(crate) fn channel(
128 name: String,
129 instance: Option<String>,
130 at_least: Option<usize>,
131 at_most: Option<usize>,
132 ) -> Self {
133 Self {
134 identity: Arc::new(ConditionIdentity),
135 id: None,
136 kind: ConditionKind::Channel {
137 name,
138 instance,
139 at_least,
140 at_most,
141 },
142 }
143 }
144
145 pub(crate) fn sub_flow(definition: SubFlowDefinition) -> Self {
146 let id = definition.options.condition_id.clone();
147 Self {
148 identity: Arc::new(ConditionIdentity),
149 id,
150 kind: ConditionKind::SubFlow(Box::new(definition)),
151 }
152 }
153
154 pub fn with_id(mut self, id: impl Into<String>) -> Self {
156 self.id = Some(id.into());
157 self
158 }
159}
160
161pub struct ConditionCombination {
163 pub(crate) conditions: Vec<Condition>,
164}
165
166impl ConditionCombination {
167 pub fn all_of(conditions: impl IntoIterator<Item = Condition>) -> Self {
169 Self {
170 conditions: conditions.into_iter().collect(),
171 }
172 }
173}