Skip to main content

nautilus_common/
enums.rs

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