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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//! Type mapping system for validating DataFrame structure
//!
//! This module provides type mappings that define the expected structure
//! of DataFrames loaded for specific types. Type mappings enable:
//! - Compile-time validation of data structure
//! - Custom column name mappings
//! - Extensibility for user-defined types
//!
//! NOTE: No built-in types are registered. Domain-specific types should be
//! defined in stdlib and registered at application startup.
use crate::data::DataFrame;
use shape_ast::error::{Result, ShapeError};
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
/// Maps DataFrame columns to a user-defined type
///
/// Specifies which columns are required and how they map to type fields.
///
/// # Example
///
/// ```ignore
/// // For a SensorReading type:
/// let mapping = TypeMapping::new("SensorReading".to_string())
/// .add_field("temperature", "temp_c")
/// .add_field("humidity", "humidity_pct")
/// .add_required("temp_c")
/// .add_required("humidity_pct");
/// ```
#[derive(Debug, Clone)]
pub struct TypeMapping {
/// Type name (e.g., "SensorReading", "LogEntry", "DataPoint")
pub type_name: String,
/// Field name → DataFrame column name mapping
/// Allows renaming: { "temp": "temperature_celsius" }
pub field_to_column: HashMap<String, String>,
/// List of required DataFrame columns
/// These must exist in the DataFrame for validation to pass
pub required_columns: Vec<String>,
}
impl TypeMapping {
/// Create a new type mapping
pub fn new(type_name: String) -> Self {
Self {
type_name,
field_to_column: HashMap::new(),
required_columns: Vec::new(),
}
}
// NOTE: The ohlcv() factory method has been removed.
// Finance-specific types like "Candle" should be defined in shape-stdlib,
// not in the core library. Types are registered at CLI startup from stdlib.
/// Add a field mapping
pub fn add_field(mut self, field: &str, column: &str) -> Self {
self.field_to_column
.insert(field.to_string(), column.to_string());
self
}
/// Add a required column
pub fn add_required(mut self, column: &str) -> Self {
self.required_columns.push(column.to_string());
self
}
/// Apply custom column mapping
///
/// Extends the existing mapping with custom field→column pairs.
pub fn with_mapping(mut self, custom: HashMap<String, String>) -> Self {
self.field_to_column.extend(custom);
self
}
/// Validate that DataFrame has all required columns
///
/// # Arguments
///
/// * `df` - DataFrame to validate
///
/// # Returns
///
/// Ok if all required columns exist, error otherwise
///
/// # Example
///
/// ```ignore
/// let mapping = TypeMapping::ohlcv();
/// mapping.validate(&dataframe)?; // Checks for open, high, low, close, volume
/// ```
pub fn validate(&self, df: &DataFrame) -> Result<()> {
let mut missing = Vec::new();
for col in &self.required_columns {
if !df.has_column(col) {
missing.push(col.clone());
}
}
if !missing.is_empty() {
return Err(ShapeError::RuntimeError {
message: format!(
"DataFrame missing required columns for type '{}': {}",
self.type_name,
missing.join(", ")
),
location: None,
});
}
Ok(())
}
/// Get column name for a type field
///
/// # Arguments
///
/// * `field` - Type field name
///
/// # Returns
///
/// DataFrame column name if mapping exists
pub fn get_column(&self, field: &str) -> Option<&str> {
self.field_to_column.get(field).map(|s| s.as_str())
}
}
/// Registry of type mappings
///
/// Maintains mappings for all known types (built-in and user-defined).
/// Thread-safe for concurrent access.
#[derive(Clone)]
pub struct TypeMappingRegistry {
/// Map of type name → type mapping
mappings: Arc<RwLock<HashMap<String, TypeMapping>>>,
}
impl TypeMappingRegistry {
/// Create a new empty registry
///
/// NOTE: No built-in types are registered. Finance-specific types like
/// "Candle" should be registered at CLI/application startup from stdlib.
pub fn new() -> Self {
Self {
mappings: Arc::new(RwLock::new(HashMap::new())),
}
}
/// Register a type mapping
///
/// # Arguments
///
/// * `type_name` - Name of the type
/// * `mapping` - TypeMapping specifying required columns
pub fn register(&self, type_name: &str, mapping: TypeMapping) {
let mut mappings = self.mappings.write().unwrap();
mappings.insert(type_name.to_string(), mapping);
}
/// Get type mapping by name
///
/// # Arguments
///
/// * `type_name` - Type name to lookup
///
/// # Returns
///
/// TypeMapping if found, None otherwise
pub fn get(&self, type_name: &str) -> Option<TypeMapping> {
let mappings = self.mappings.read().unwrap();
mappings.get(type_name).cloned()
}
/// Check if a type mapping exists
pub fn has(&self, type_name: &str) -> bool {
let mappings = self.mappings.read().unwrap();
mappings.contains_key(type_name)
}
/// List all registered type names
pub fn list_types(&self) -> Vec<String> {
let mappings = self.mappings.read().unwrap();
mappings.keys().cloned().collect()
}
/// Unregister a type mapping
pub fn unregister(&self, type_name: &str) -> bool {
let mut mappings = self.mappings.write().unwrap();
mappings.remove(type_name).is_some()
}
/// Clear all type mappings
pub fn clear(&self) {
let mut mappings = self.mappings.write().unwrap();
mappings.clear();
}
}
impl Default for TypeMappingRegistry {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::data::Timeframe;
#[test]
fn test_generic_mapping() {
// Test creating a generic type mapping
let mapping = TypeMapping::new("SensorData".to_string())
.add_field("temp", "temperature")
.add_field("humid", "humidity")
.add_required("temperature")
.add_required("humidity");
assert_eq!(mapping.type_name, "SensorData");
assert_eq!(mapping.required_columns.len(), 2);
assert!(
mapping
.required_columns
.contains(&"temperature".to_string())
);
assert!(mapping.required_columns.contains(&"humidity".to_string()));
assert_eq!(mapping.get_column("temp"), Some("temperature"));
}
#[test]
fn test_validate_success() {
// Create a generic type mapping
let mapping = TypeMapping::new("Metrics".to_string())
.add_required("value")
.add_required("count");
let mut df = DataFrame::new("TEST", Timeframe::d1());
df.add_column("value", vec![100.0, 101.0]);
df.add_column("count", vec![5.0, 6.0]);
assert!(mapping.validate(&df).is_ok());
}
#[test]
fn test_validate_missing_column() {
// Create a generic type mapping with required columns
let mapping = TypeMapping::new("DataPoint".to_string())
.add_required("value")
.add_required("count");
let mut df = DataFrame::new("TEST", Timeframe::d1());
df.add_column("value", vec![100.0]);
// Missing: count
assert!(mapping.validate(&df).is_err());
}
#[test]
fn test_custom_mapping() {
let mapping = TypeMapping::new("CustomType".to_string())
.add_field("price", "close")
.add_field("size", "volume")
.add_required("close")
.add_required("volume");
assert_eq!(mapping.type_name, "CustomType");
assert_eq!(mapping.get_column("price"), Some("close"));
assert_eq!(mapping.get_column("size"), Some("volume"));
assert_eq!(mapping.required_columns.len(), 2);
}
#[test]
fn test_registry_operations() {
let registry = TypeMappingRegistry::new();
// Registry starts empty - no built-in types
assert!(!registry.has("Candle"));
assert_eq!(registry.list_types().len(), 0);
// Register a type
let custom = TypeMapping::new("DataPoint".to_string());
registry.register("DataPoint", custom);
assert!(registry.has("DataPoint"));
assert_eq!(registry.list_types().len(), 1);
// Unregister
assert!(registry.unregister("DataPoint"));
assert!(!registry.has("DataPoint"));
}
#[test]
fn test_registry_clear() {
let registry = TypeMappingRegistry::new();
registry.clear();
assert_eq!(registry.list_types().len(), 0);
}
}