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