Skip to main content

hyperstack_interpreter/
lib.rs

1//! # hyperstack-interpreter
2//!
3//! AST transformation runtime and VM for HyperStack streaming pipelines.
4//!
5//! This crate provides the core components for processing Solana blockchain
6//! events into typed state projections:
7//!
8//! - **AST Definition** - Type-safe schemas for state and event handlers
9//! - **Bytecode Compiler** - Compiles specs into optimized bytecode  
10//! - **Virtual Machine** - Executes bytecode to process events
11//! - **TypeScript Generation** - Generate client SDKs automatically
12//!
13//! ## Example
14//!
15//! ```rust,ignore
16//! use hyperstack_interpreter::{TypeScriptCompiler, TypeScriptConfig};
17//!
18//! let config = TypeScriptConfig::default();
19//! let compiler = TypeScriptCompiler::new(config);
20//! let typescript = compiler.compile(&spec)?;
21//! ```
22//!
23//! ## Feature Flags
24//!
25//! - `otel` - OpenTelemetry integration for distributed tracing and metrics
26
27pub mod ast;
28pub mod canonical_log;
29pub mod compiler;
30pub mod event_type_helpers;
31pub mod metrics_context;
32pub mod proto_router;
33pub mod resolvers;
34pub mod rust;
35pub mod spec_trait;
36pub mod typescript;
37pub mod vm;
38pub mod vm_metrics;
39
40pub use canonical_log::{CanonicalLog, LogLevel};
41pub use metrics_context::{FieldAccessor, FieldRef, MetricsContext};
42pub use resolvers::{InstructionContext, KeyResolution, ResolveContext, ReverseLookupUpdater};
43pub use typescript::{write_typescript_to_file, TypeScriptCompiler, TypeScriptConfig};
44pub use vm::{
45    CapacityWarning, CleanupResult, DirtyTracker, FieldChange, PendingAccountUpdate,
46    PendingQueueStats, QueuedAccountUpdate, StateTableConfig, UpdateContext, VmMemoryStats,
47};
48
49// Re-export macros for convenient use
50// The field! macro is the new recommended way to create field references
51// The field_accessor! macro is kept for backward compatibility
52
53use serde::{Deserialize, Serialize};
54use serde_json::Value;
55
56#[derive(Debug, Clone, Serialize, Deserialize)]
57pub struct Mutation {
58    pub export: String,
59    pub key: Value,
60    pub patch: Value,
61    #[serde(skip_serializing_if = "Vec::is_empty", default)]
62    pub append: Vec<String>,
63}
64
65/// Generic wrapper for event data that includes context metadata
66/// This ensures type safety for events captured in entity specs
67///
68/// # Runtime Structure
69/// Events captured with `#[event]` are automatically wrapped in this structure:
70/// ```json
71/// {
72///   "timestamp": 1234567890,
73///   "data": { /* event-specific data */ },
74///   "slot": 381471241,
75///   "signature": "4xNEYTVL8DB28W87..."
76/// }
77/// ```
78#[derive(Debug, Clone, Serialize, Deserialize)]
79pub struct EventWrapper<T = Value> {
80    /// Unix timestamp when the event was processed
81    pub timestamp: i64,
82    /// The event-specific data
83    pub data: T,
84    /// Optional slot number from UpdateContext
85    #[serde(skip_serializing_if = "Option::is_none")]
86    pub slot: Option<u64>,
87    /// Optional transaction signature from UpdateContext
88    #[serde(skip_serializing_if = "Option::is_none")]
89    pub signature: Option<String>,
90}
91
92/// Generic wrapper for account capture data that includes context metadata
93/// This ensures type safety for accounts captured with `#[capture]` in entity specs
94///
95/// # Runtime Structure
96/// Accounts captured with `#[capture]` are automatically wrapped in this structure:
97/// ```json
98/// {
99///   "timestamp": 1234567890,
100///   "account_address": "C6P5CpJnYHgpGvCGuXYAWL6guKH5LApn3QwTAZmNUPCj",
101///   "data": { /* account-specific data (filtered, no __ fields) */ },
102///   "slot": 381471241,
103///   "signature": "4xNEYTVL8DB28W87..."
104/// }
105/// ```
106#[derive(Debug, Clone, Serialize, Deserialize)]
107pub struct CaptureWrapper<T = Value> {
108    /// Unix timestamp when the account was captured
109    pub timestamp: i64,
110    /// The account address (base58 encoded public key)
111    pub account_address: String,
112    /// The account data (already filtered to remove internal __ fields)
113    pub data: T,
114    /// Optional slot number from UpdateContext
115    #[serde(skip_serializing_if = "Option::is_none")]
116    pub slot: Option<u64>,
117    /// Optional transaction signature from UpdateContext
118    #[serde(skip_serializing_if = "Option::is_none")]
119    pub signature: Option<String>,
120}