Skip to main content

arete_interpreter/
lib.rs

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