openpine_vm/events.rs
1use num_enum::FromPrimitive;
2use serde::{Deserialize, Serialize};
3
4use crate::{bar_state::BarState, draw_event::DrawEvent};
5
6/// Logging severity used by [`LogEvent`].
7#[derive(Debug, Clone, Copy, PartialEq, Eq, FromPrimitive, Serialize, Deserialize)]
8#[repr(i32)]
9pub enum LogLevel {
10 /// Informational message.
11 #[default]
12 Info,
13 /// Warning condition.
14 Warning,
15 /// Error condition.
16 Error,
17}
18
19/// A log event emitted by script execution.
20#[derive(Debug, Serialize, Deserialize)]
21pub struct LogEvent {
22 /// Severity level.
23 pub level: LogLevel,
24 /// Human-readable message.
25 pub message: String,
26}
27
28/// An alert event emitted by `alert()`/alertcondition-like behavior.
29#[derive(Debug, Serialize, Deserialize)]
30pub struct AlertEvent {
31 /// Optional alert identifier.
32 pub id: Option<usize>,
33 /// Alert message.
34 pub message: String,
35}
36
37/// Emitted at the start of each bar's execution.
38#[derive(Debug, Clone, Serialize, Deserialize)]
39pub struct BarStartEvent {
40 /// Zero-based bar index.
41 pub bar_index: usize,
42 /// The bar's epoch-millisecond timestamp.
43 pub timestamp: i64,
44 /// The bar state (history, realtime new, realtime update, etc.).
45 pub bar_state: BarState,
46}
47
48/// An event generated during VM execution.
49///
50/// Events are yielded by the [`Stream`](futures_util::Stream) returned from
51/// [`Instance::run`](crate::Instance::run). Bar lifecycle events
52/// (`BarStart`, `BarEnd`, `HistoryEnd`) are always emitted regardless of
53/// [`OutputMode`](crate::OutputMode). `Draw` events are only emitted in
54/// [`OutputMode::Stream`](crate::OutputMode::Stream) mode.
55#[derive(Debug, Serialize, Deserialize)]
56pub enum Event {
57 /// A log message.
58 Log(LogEvent),
59 /// An alert message.
60 Alert(AlertEvent),
61 /// Signals the start of a bar's execution.
62 BarStart(BarStartEvent),
63 /// Signals the end of a bar's execution.
64 BarEnd,
65 /// Signals the boundary between historical and real-time bars.
66 HistoryEnd,
67 /// A drawing instruction (only in
68 /// [`OutputMode::Stream`](crate::OutputMode::Stream)).
69 Draw(DrawEvent),
70}