tramex_tools/interface/
types.rs

1//! This module contains the types used in the websocket module.
2
3use std::str::FromStr;
4
5use crate::interface::onelog::OneLog;
6
7// deserialize the message
8#[derive(serde::Deserialize, Debug)]
9/// LogGet struct
10pub struct WebSocketLog {
11    /// Same as request
12    pub message: String,
13
14    ///Any type, force as string // Same as in request.
15    pub message_id: Option<u64>,
16
17    /// Number representing time in seconds since start of the process. // Useful to send command with absolute time.
18    pub time: f64,
19
20    ///Number representing UTC seconds.
21    pub utc: f64,
22
23    /// headers
24    pub headers: Option<Vec<String>>,
25
26    /// Logs vectors
27    pub logs: Vec<OneLog>,
28}
29
30/// LogGet struct
31#[derive(serde::Deserialize, Debug)]
32pub struct BaseMessage {
33    /// Message
34    pub message: String,
35
36    /// Message ID
37    pub name: String,
38
39    /// Time
40    pub time: f64,
41
42    /// UTC
43    pub utc: f64,
44
45    /// Version
46    pub version: String,
47}
48
49#[derive(Debug, PartialEq)]
50/// LogLevel struct
51pub enum LogLevel {
52    /// Error log level
53    ERROR = 1,
54
55    /// Warning log level
56    WARN = 2,
57
58    /// Info log level
59    INFO = 3,
60
61    /// Debug log level
62    DEBUG = 4,
63}
64
65#[derive(serde::Deserialize, Debug, PartialEq)]
66/// SourceLog enum
67pub enum SourceLog {
68    /// ENB source
69    ENB,
70
71    /// MME source
72    MME,
73}
74
75#[derive(serde::Deserialize, Debug, PartialEq, Default, Clone)]
76/// Direction enum
77pub enum Direction {
78    #[default]
79    /// Uplink direction
80    UL,
81
82    /// Downlink direction
83    DL,
84
85    /// From direction
86    FROM,
87
88    /// To direction
89    TO,
90
91    /// Not available direction
92    NA,
93}
94
95impl FromStr for Direction {
96    type Err = ();
97
98    fn from_str(input_string: &str) -> Result<Self, Self::Err> {
99        match input_string.trim() {
100            "UL" => Ok(Direction::UL),
101            "DL" => Ok(Direction::DL),
102            "FROM" => Ok(Direction::FROM),
103            "TO" => Ok(Direction::TO),
104            "" | "-" => Ok(Direction::NA),
105            _ => Err(()),
106        }
107    }
108}
109
110impl<'de> serde::Deserialize<'de> for LogLevel {
111    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
112    where
113        D: serde::Deserializer<'de>,
114    {
115        let deserialized_int = u8::deserialize(deserializer)?;
116        match deserialized_int {
117            1 => Ok(LogLevel::ERROR),
118            2 => Ok(LogLevel::WARN),
119            3 => Ok(LogLevel::INFO),
120            4 => Ok(LogLevel::DEBUG),
121            _ => Ok(LogLevel::INFO), // default
122        }
123    }
124}