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