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