1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// ! Source definitions and routing operations for signal computation.
//!
//! This module provides structures for defining data sources and routing operations
//! that transform source data before it is processed. It enables the flexible
//! combination and transformation of data from various sources.
//!
//! The module provides:
//!
//! - The [`Operation`] enum which defines basic arithmetic operations
//! - The [`OperationRoute`] struct which pairs operations with dependent signals
//! - The [`SourceQuery`] struct which specifies where to obtain input data
//!
//! # Source Routing
//!
//! Sources can be configured with routes that apply transformations using values
//! from other signals. This enables complex relationships between signals, such as:
//!
//! - Converting between different units (e.g., USDT to USD)
//! - Adjusting values using scaling factors
//! - Applying corrections based on other market data
//!
//! Routing operations are applied sequentially, with each operation using the
//! result of the previous operation as its first operand.
use ;
use ;
use ;
/// Arithmetic operations that can be applied in routing transformations.
///
/// The `Operation` enum represents the four basic arithmetic operations that
/// can be used to transform values during the routing process. Each operation
/// is designed to safely handle potential numerical errors using checked operations.
///
/// # Variants
///
/// * `Add` - Addition operation (+)
/// * `Subtract` - Subtraction operation (-)
/// * `Multiply` - Multiplication operation (*)
/// * `Divide` - Division operation (/)
///
/// # Examples
///
/// ```
/// use bothan_lib::registry::source::Operation;
/// use rust_decimal::Decimal;
///
/// // Create a multiplication operation
/// let operation = Operation::Multiply;
///
/// // Apply the operation to two values
/// let a = Decimal::new(10, 0); // 10.0
/// let b = Decimal::new(15, 0); // 15.0
/// let result = operation.execute(a, b).unwrap();
///
/// assert_eq!(result, Decimal::new(150, 0)); // 150.0
/// ```
/// A transformation that applies an operation using a value from another signal.
///
/// The `OperationRoute` struct defines a step in a routing transformation chain.
/// It specifies a signal whose value should be used as the second operand in
/// an operation, with the first operand being either the original source value
/// or the result of previous routing operations.
///
/// Routes are applied sequentially, allowing for complex transformations to be
/// built from simple arithmetic operations.
///
/// # Examples
///
/// ```
/// use bothan_lib::registry::source::{OperationRoute, Operation};
///
/// // Create a route that multiplies by the "USDT-USD" signal's value
/// let route = OperationRoute::new("USDT-USD", Operation::Multiply);
/// ```
/// A specification for retrieving and transforming data from a source.
///
/// The `SourceQuery` struct defines where to obtain data and how to transform it
/// before processing. It specifies a data source, an identifier to query within
/// that source, and optionally a series of routing operations to apply to the
/// retrieved value.
///
/// This structure is a key component of signal definitions, allowing each signal
/// to combine and transform data from multiple sources.
///
/// # Examples
///
/// ```
/// use bothan_lib::registry::source::{SourceQuery, OperationRoute, Operation};
///
/// // Create a query for BTC/USDT from Binance, converted to USD
/// let query = SourceQuery::new(
/// "binance",
/// "btcusdt",
/// vec![
/// // Convert from USDT to USD using the USDT-USD signal
/// OperationRoute::new("USDT-USD", Operation::Multiply),
/// ]
/// );
/// ```