tramex_tools/interface/
onelog.rs

1//! This module contains the definition of the OneLog struct.
2
3use std::str::FromStr;
4
5use crate::data::{AdditionalInfos, Trace};
6use crate::errors::TramexError;
7use crate::interface::functions::extract_hexe;
8
9use crate::interface::{layer::Layer, types::SourceLog};
10use crate::tramex_error;
11
12use super::parser::parser_rrc::RRCInfos;
13use super::types::Direction; // to use the FileParser trait and implementations
14
15#[derive(serde::Deserialize, Debug)]
16/// Data structure to store the log.
17pub struct OneLog {
18    /// Each item is a string representing a line of log.
19    pub data: Vec<String>,
20
21    /// Milliseconds since January 1st 1970.
22    pub timestamp: u64,
23
24    /// log layer
25    pub layer: Layer,
26
27    /// Source of the log.
28    pub src: SourceLog,
29
30    /// index
31    pub idx: u64,
32
33    /// index
34    pub dir: Option<String>,
35}
36
37impl OneLog {
38    /// Extract the hexadecimal representation of the log.
39    /// # Errors
40    /// Returns a TramexError if the hexe representation could not be extracted.
41    pub fn extract_hexe(&self) -> Result<Vec<u8>, TramexError> {
42        extract_hexe(&self.data)
43    }
44
45    /// Extract the canal message of the log.
46    pub fn extract_canal_msg(&self) -> Option<String> {
47        // TODO implement this function correctly
48        if let Some(data_line) = self.data.first() {
49            log::debug!("{data_line:?}");
50            return Some(data_line.to_owned());
51        }
52        None
53    }
54
55    /// Extract the data of the log.
56    /// # Errors
57    /// Returns a TramexError if the data could not be extracted.
58    pub fn extract_data(&self) -> Result<Trace, TramexError> {
59        match self.layer {
60            Layer::RRC => {
61                let dir = match &self.dir {
62                    Some(opt_dir) => match Direction::from_str(opt_dir) {
63                        Ok(d) => d,
64                        Err(_) => {
65                            return Err(tramex_error!(
66                                format!("Can't format direction {}", opt_dir),
67                                crate::errors::ErrorCode::WebSocketErrorDecodingMessage
68                            ));
69                        }
70                    },
71                    None => {
72                        return Err(tramex_error!(
73                            "Direction not found".to_owned(),
74                            crate::errors::ErrorCode::WebSocketErrorDecodingMessage
75                        ));
76                    }
77                };
78                let firs_line = self.data[0].split(':').collect::<Vec<&str>>();
79                if firs_line.len() < 2 {
80                    return Err(tramex_error!(
81                        format!("Invalid first line {}", self.data[0]),
82                        crate::errors::ErrorCode::WebSocketErrorDecodingMessage
83                    ));
84                }
85                let rrc: RRCInfos = RRCInfos {
86                    direction: dir,
87                    canal: firs_line[0].to_owned(),
88                    canal_msg: firs_line[1][1..].to_owned(),
89                };
90                let infos = AdditionalInfos::RRCInfos(rrc);
91                let trace = Trace {
92                    timestamp: self.timestamp.to_owned(),
93                    layer: Layer::RRC,
94                    additional_infos: infos,
95                    hexa: self.extract_hexe().unwrap_or_default(),
96                    text: Some(self.data[1..].iter().map(|x| x.to_string()).collect()),
97                };
98                Ok(trace)
99            }
100            _ => Err(tramex_error!(
101                format!("Layer {:?} not implemented", self.layer),
102                crate::errors::ErrorCode::ParsingLayerNotImplemented
103            )),
104        }
105    }
106}