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