shape_runtime/ast_extensions.rs
1//! Extensions to the AST for flexible query execution
2//!
3//! This module proposes enhanced query structures that support
4//! Shape-defined rules for analysis and backtesting.
5
6use serde::{Deserialize, Serialize};
7use shape_ast::ast::{Block, Expr, TimeWindow, Timeframe};
8
9/// Process statement for unified execution
10/// This is the new primary way to execute analysis and backtesting
11#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct ProcessStatement {
13 /// The points to process (variable name or expression)
14 pub target: ProcessTarget,
15
16 /// Execution rules defined in Shape
17 pub rules: ProcessRules,
18}
19
20#[derive(Debug, Clone, Serialize, Deserialize)]
21pub enum ProcessTarget {
22 /// Process a variable containing points
23 Variable(String),
24 /// Process results of a find expression
25 FindExpr(Box<Expr>),
26 /// Process all rows in the data
27 AllRows,
28}
29
30#[derive(Debug, Clone, Serialize, Deserialize)]
31pub struct ProcessRules {
32 /// Initial state setup
33 pub state: Option<StateBlock>,
34
35 /// Rules evaluated at each point
36 pub on_point: Option<Block>,
37
38 /// Rules evaluated on each subsequent row
39 pub on_bar: Option<Block>,
40
41 /// Final aggregation/results
42 pub finalize: Option<Block>,
43}
44
45#[derive(Debug, Clone, Serialize, Deserialize)]
46pub struct StateBlock {
47 /// State variable declarations
48 pub declarations: Vec<StateDeclaration>,
49}
50
51#[derive(Debug, Clone, Serialize, Deserialize)]
52pub struct StateDeclaration {
53 pub name: String,
54 pub value: Expr,
55}
56
57/// Pattern reference for AST extensions
58#[derive(Debug, Clone, Serialize, Deserialize)]
59pub enum PatternReference {
60 /// Reference to a named pattern
61 Named(String),
62}
63
64/// Find clause that identifies pattern matches
65#[derive(Debug, Clone, Serialize, Deserialize)]
66pub struct FindClause {
67 pub pattern: PatternReference,
68 pub window: Option<TimeWindow>,
69 pub where_conditions: Vec<Expr>,
70 pub timeframe: Option<Timeframe>,
71 pub as_name: Option<String>, // Store matches as variable
72}
73
74/// Analyze clause with Shape-defined rules
75#[derive(Debug, Clone, Serialize, Deserialize)]
76pub struct AnalyzeClause {
77 /// The target to analyze (usually the find results)
78 pub target: Option<String>, // Variable name from find clause
79
80 /// Analysis rules as a block of statements
81 pub with_rules: Block,
82}
83
84/// Output clause for formatting results
85#[derive(Debug, Clone, Serialize, Deserialize)]
86pub struct OutputClause {
87 /// Output specification as object literal or block
88 pub spec: Expr,
89}
90
91// DEPRECATED: AnalyzeQuery type removed - use method chaining instead
92// /// Alternative: Extend existing queries with rule blocks
93// #[derive(Debug, Clone, Serialize, Deserialize)]
94// pub struct EnhancedAnalyzeQuery {
95// /// Target of analysis (pattern matches or expression)
96// pub target: AnalysisTarget,
97//
98// /// Analysis rules defined in Shape
99// pub rules: Option<Block>,
100//
101// /// Predefined metrics (for backward compatibility)
102// pub metrics: Vec<String>,
103// }
104//
105// #[derive(Debug, Clone, Serialize, Deserialize)]
106// pub enum AnalysisTarget {
107// /// Analyze pattern matches
108// Pattern(PatternReference),
109// /// Analyze a time window
110// Window(TimeWindow),
111// /// Analyze expression results
112// Expression(Expr),
113// /// Analyze a variable (from previous find)
114// Variable(String),
115// }