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