nautilus_common/
enums.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! Enumerations for common components.
17
18use std::fmt::Debug;
19
20use log::Level;
21use serde::{Deserialize, Serialize};
22use strum::{Display, EnumIter, EnumString, FromRepr};
23
24/// The state of a component within the system.
25#[repr(C)]
26#[derive(
27    Copy,
28    Clone,
29    Debug,
30    Default,
31    Display,
32    Hash,
33    PartialEq,
34    Eq,
35    PartialOrd,
36    Ord,
37    FromRepr,
38    EnumIter,
39    EnumString,
40    Serialize,
41    Deserialize,
42)]
43#[strum(ascii_case_insensitive)]
44#[strum(serialize_all = "SCREAMING_SNAKE_CASE")]
45#[cfg_attr(
46    feature = "python",
47    pyo3::pyclass(eq, eq_int, module = "nautilus_trader.core.nautilus_pyo3.common.enums")
48)]
49pub enum ComponentState {
50    /// When a component is instantiated, but not yet ready to fulfill its specification.
51    #[default]
52    PreInitialized = 0,
53    /// When a component is able to be started.
54    Ready = 1,
55    /// When a component is executing its actions on `start`.
56    Starting = 2,
57    /// When a component is operating normally and can fulfill its specification.
58    Running = 3,
59    /// When a component is executing its actions on `stop`.
60    Stopping = 4,
61    /// When a component has successfully stopped.
62    Stopped = 5,
63    /// When a component is started again after its initial start.
64    Resuming = 6,
65    /// When a component is executing its actions on `reset`.
66    Resetting = 7,
67    /// When a component is executing its actions on `dispose`.
68    Disposing = 8,
69    /// When a component has successfully shut down and released all of its resources.
70    Disposed = 9,
71    /// When a component is executing its actions on `degrade`.
72    Degrading = 10,
73    /// When a component has successfully degraded and may not meet its full specification.
74    Degraded = 11,
75    /// When a component is executing its actions on `fault`.
76    Faulting = 12,
77    /// When a component has successfully shut down due to a detected fault.
78    Faulted = 13,
79}
80
81/// A trigger condition for a component within the system.
82#[repr(C)]
83#[derive(
84    Copy,
85    Clone,
86    Debug,
87    Display,
88    Hash,
89    PartialEq,
90    Eq,
91    PartialOrd,
92    Ord,
93    FromRepr,
94    EnumIter,
95    EnumString,
96    Serialize,
97    Deserialize,
98)]
99#[strum(ascii_case_insensitive)]
100#[strum(serialize_all = "SCREAMING_SNAKE_CASE")]
101#[cfg_attr(
102    feature = "python",
103    pyo3::pyclass(eq, eq_int, module = "nautilus_trader.core.nautilus_pyo3.common.enums")
104)]
105pub enum ComponentTrigger {
106    /// A trigger for the component to initialize.
107    Initialize = 1,
108    /// A trigger for the component to start.
109    Start = 2,
110    /// A trigger when the component has successfully started.
111    StartCompleted = 3,
112    /// A trigger for the component to stop.
113    Stop = 4,
114    /// A trigger when the component has successfully stopped.
115    StopCompleted = 5,
116    /// A trigger for the component to resume (after being stopped).
117    Resume = 6,
118    /// A trigger when the component has successfully resumed.
119    ResumeCompleted = 7,
120    /// A trigger for the component to reset.
121    Reset = 8,
122    /// A trigger when the component has successfully reset.
123    ResetCompleted = 9,
124    /// A trigger for the component to dispose and release resources.
125    Dispose = 10,
126    /// A trigger when the component has successfully disposed.
127    DisposeCompleted = 11,
128    /// A trigger for the component to degrade.
129    Degrade = 12,
130    /// A trigger when the component has successfully degraded.
131    DegradeCompleted = 13,
132    /// A trigger for the component to fault.
133    Fault = 14,
134    /// A trigger when the component has successfully faulted.
135    FaultCompleted = 15,
136}
137
138/// Represents the environment context for a Nautilus system.
139#[repr(C)]
140#[derive(
141    Copy,
142    Clone,
143    Debug,
144    Display,
145    Hash,
146    PartialEq,
147    Eq,
148    PartialOrd,
149    Ord,
150    FromRepr,
151    EnumIter,
152    EnumString,
153    Serialize,
154    Deserialize,
155)]
156#[strum(ascii_case_insensitive)]
157#[strum(serialize_all = "SCREAMING_SNAKE_CASE")]
158#[cfg_attr(
159    feature = "python",
160    pyo3::pyclass(eq, eq_int, module = "nautilus_trader.core.nautilus_pyo3.common.enums")
161)]
162pub enum Environment {
163    Backtest,
164    Sandbox,
165    Live,
166}
167
168/// The log level for log messages.
169#[repr(C)]
170#[derive(
171    Copy,
172    Clone,
173    Debug,
174    Display,
175    Hash,
176    PartialEq,
177    Eq,
178    PartialOrd,
179    Ord,
180    FromRepr,
181    EnumIter,
182    EnumString,
183    Serialize,
184    Deserialize,
185)]
186#[strum(ascii_case_insensitive)]
187#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
188#[cfg_attr(
189    feature = "python",
190    pyo3::pyclass(eq, eq_int, module = "nautilus_trader.core.nautilus_pyo3.common.enums")
191)]
192pub enum LogLevel {
193    /// The **OFF** log level. A level lower than all other log levels (off).
194    #[strum(serialize = "OFF")]
195    #[serde(rename = "OFF")]
196    Off = 0,
197    /// The **TRACE** log level. Only available in Rust for debug/development builds.
198    #[strum(serialize = "TRACE")]
199    #[serde(rename = "TRACE")]
200    Trace = 1,
201    /// The **DEBUG** log level.
202    #[strum(serialize = "DEBUG")]
203    #[serde(rename = "DEBUG")]
204    Debug = 2,
205    /// The **INFO** log level.
206    #[strum(serialize = "INFO")]
207    #[serde(rename = "INFO")]
208    Info = 3,
209    /// The **WARNING** log level.
210    #[strum(serialize = "WARN", serialize = "WARNING")]
211    #[serde(rename = "WARNING")]
212    Warning = 4,
213    /// The **ERROR** log level.
214    #[strum(serialize = "ERROR")]
215    #[serde(rename = "ERROR")]
216    Error = 5,
217}
218
219/// The log color for log messages.
220#[repr(C)]
221#[derive(
222    Copy,
223    Clone,
224    Debug,
225    Display,
226    Hash,
227    PartialEq,
228    Eq,
229    PartialOrd,
230    Ord,
231    FromRepr,
232    EnumIter,
233    EnumString,
234    Serialize,
235    Deserialize,
236)]
237#[strum(ascii_case_insensitive)]
238#[strum(serialize_all = "SCREAMING_SNAKE_CASE")]
239#[cfg_attr(
240    feature = "python",
241    pyo3::pyclass(eq, eq_int, module = "nautilus_trader.core.nautilus_pyo3.common.enums")
242)]
243pub enum LogColor {
244    /// The default/normal log color.
245    #[strum(serialize = "NORMAL")]
246    Normal = 0,
247    /// The green log color, typically used with [`LogLevel::Info`] log levels and associated with success events.
248    #[strum(serialize = "GREEN")]
249    Green = 1,
250    /// The blue log color, typically used with [`LogLevel::Info`] log levels and associated with user actions.
251    #[strum(serialize = "BLUE")]
252    Blue = 2,
253    /// The magenta log color, typically used with [`LogLevel::Info`] log levels.
254    #[strum(serialize = "MAGENTA")]
255    Magenta = 3,
256    /// The cyan log color, typically used with [`LogLevel::Info`] log levels.
257    #[strum(serialize = "CYAN")]
258    Cyan = 4,
259    /// The yellow log color, typically used with [`LogLevel::Warning`] log levels.
260    #[strum(serialize = "YELLOW")]
261    Yellow = 5,
262    /// The red log color, typically used with [`LogLevel::Error`] level.
263    #[strum(serialize = "RED")]
264    Red = 6,
265}
266
267impl LogColor {
268    #[must_use]
269    pub const fn as_ansi(&self) -> &str {
270        match *self {
271            Self::Normal => "",
272            Self::Green => "\x1b[92m",
273            Self::Blue => "\x1b[94m",
274            Self::Magenta => "\x1b[35m",
275            Self::Cyan => "\x1b[36m",
276            Self::Yellow => "\x1b[1;33m",
277            Self::Red => "\x1b[1;31m",
278        }
279    }
280}
281
282impl From<u8> for LogColor {
283    fn from(value: u8) -> Self {
284        match value {
285            1 => Self::Green,
286            2 => Self::Blue,
287            3 => Self::Magenta,
288            4 => Self::Cyan,
289            5 => Self::Yellow,
290            6 => Self::Red,
291            _ => Self::Normal,
292        }
293    }
294}
295
296impl From<Level> for LogColor {
297    fn from(value: Level) -> Self {
298        match value {
299            Level::Error => Self::Red,
300            Level::Warn => Self::Yellow,
301            Level::Info => Self::Normal,
302            Level::Debug => Self::Normal,
303            Level::Trace => Self::Normal,
304        }
305    }
306}
307
308/// An ANSI log line format specifier.
309/// This is used for formatting log messages with ANSI escape codes.
310#[repr(C)]
311#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, FromRepr, EnumString, Display)]
312#[strum(ascii_case_insensitive)]
313#[strum(serialize_all = "SCREAMING_SNAKE_CASE")]
314#[cfg_attr(
315    feature = "python",
316    pyo3::pyclass(eq, eq_int, module = "nautilus_trader.core.nautilus_pyo3.common.enums")
317)]
318pub enum LogFormat {
319    /// Header log format. This ANSI escape code is used for magenta text color,
320    /// often used for headers or titles in the log output.
321    #[strum(serialize = "\x1b[95m")]
322    Header,
323
324    /// Endc log format. This ANSI escape code is used to reset all format attributes
325    /// to their defaults. It should be used after applying other formats.
326    #[strum(serialize = "\x1b[0m")]
327    Endc,
328
329    /// Bold log format. This ANSI escape code is used to make the text bold in the log output.
330    #[strum(serialize = "\x1b[1m")]
331    Bold,
332
333    /// Underline log format. This ANSI escape code is used to underline the text in the log output.
334    #[strum(serialize = "\x1b[4m")]
335    Underline,
336}
337
338/// The serialization encoding.
339#[repr(C)]
340#[derive(
341    Copy,
342    Clone,
343    Debug,
344    Display,
345    Hash,
346    PartialEq,
347    Eq,
348    PartialOrd,
349    Ord,
350    FromRepr,
351    EnumIter,
352    EnumString,
353    Serialize,
354    Deserialize,
355)]
356#[strum(ascii_case_insensitive)]
357#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
358#[cfg_attr(
359    feature = "python",
360    pyo3::pyclass(eq, eq_int, module = "nautilus_trader.core.nautilus_pyo3.common.enums")
361)]
362pub enum SerializationEncoding {
363    /// The MessagePack encoding.
364    #[serde(rename = "msgpack")]
365    MsgPack = 0,
366    /// The JavaScript Object Notation (JSON) encoding.
367    #[serde(rename = "json")]
368    Json = 1,
369}