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 scheduler;
36pub mod slot_hash_cache;
37pub mod spec_trait;
38pub mod typescript;
39pub mod vm;
40pub mod vm_metrics;
41
42// Re-export slot hash cache functions
43pub use slot_hash_cache::{get_slot_hash, record_slot_hash};
44
45pub use canonical_log::{CanonicalLog, LogLevel};
46pub use metrics_context::{FieldAccessor, FieldRef, MetricsContext};
47pub use resolvers::{
48    InstructionContext, KeyResolution, ResolveContext, ReverseLookupUpdater, TokenMetadata,
49};
50pub use typescript::{write_typescript_to_file, TypeScriptCompiler, TypeScriptConfig};
51pub use vm::{
52    CapacityWarning, CleanupResult, DirtyTracker, FieldChange, PendingAccountUpdate,
53    PendingQueueStats, QueuedAccountUpdate, ResolverRequest, ResolverTarget, ScheduledCallback,
54    StateTableConfig, UpdateContext, VmMemoryStats,
55};
56
57// Re-export macros for convenient use
58// The field! macro is the new recommended way to create field references
59// The field_accessor! macro is kept for backward compatibility
60
61use serde::{Deserialize, Serialize};
62use serde_json::Value;
63
64#[derive(Debug, Clone, Serialize, Deserialize)]
65pub struct Mutation {
66    pub export: String,
67    pub key: Value,
68    pub patch: Value,
69    #[serde(skip_serializing_if = "Vec::is_empty", default)]
70    pub append: Vec<String>,
71}
72
73/// Generic wrapper for event data that includes context metadata
74/// This ensures type safety for events captured in entity specs
75///
76/// # Runtime Structure
77/// Events captured with `#[event]` are automatically wrapped in this structure:
78/// ```json
79/// {
80///   "timestamp": 1234567890,
81///   "data": { /* event-specific data */ },
82///   "slot": 381471241,
83///   "signature": "4xNEYTVL8DB28W87..."
84/// }
85/// ```
86#[derive(Debug, Clone, Serialize, Deserialize)]
87pub struct EventWrapper<T = Value> {
88    /// Unix timestamp when the event was processed
89    pub timestamp: i64,
90    /// The event-specific data
91    pub data: T,
92    /// Optional slot number from UpdateContext
93    #[serde(skip_serializing_if = "Option::is_none")]
94    pub slot: Option<u64>,
95    /// Optional transaction signature from UpdateContext
96    #[serde(skip_serializing_if = "Option::is_none")]
97    pub signature: Option<String>,
98}
99
100/// Generic wrapper for account capture data that includes context metadata
101/// This ensures type safety for accounts captured with `#[capture]` in entity specs
102///
103/// # Runtime Structure
104/// Accounts captured with `#[capture]` are automatically wrapped in this structure:
105/// ```json
106/// {
107///   "timestamp": 1234567890,
108///   "account_address": "C6P5CpJnYHgpGvCGuXYAWL6guKH5LApn3QwTAZmNUPCj",
109///   "data": { /* account-specific data (filtered, no __ fields) */ },
110///   "slot": 381471241,
111///   "signature": "4xNEYTVL8DB28W87..."
112/// }
113/// ```
114#[derive(Debug, Clone, Serialize, Deserialize)]
115pub struct CaptureWrapper<T = Value> {
116    /// Unix timestamp when the account was captured
117    pub timestamp: i64,
118    /// The account address (base58 encoded public key)
119    pub account_address: String,
120    /// The account data (already filtered to remove internal __ fields)
121    pub data: T,
122    /// Optional slot number from UpdateContext
123    #[serde(skip_serializing_if = "Option::is_none")]
124    pub slot: Option<u64>,
125    /// Optional transaction signature from UpdateContext
126    #[serde(skip_serializing_if = "Option::is_none")]
127    pub signature: Option<String>,
128}