tramex_tools/interface/
onelog.rs1use 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; #[derive(serde::Deserialize, Debug)]
16pub struct OneLog {
18 pub data: Vec<String>,
20
21 pub timestamp: u64,
23
24 pub layer: Layer,
26
27 pub src: SourceLog,
29
30 pub idx: u64,
32
33 pub dir: Option<String>,
35}
36
37impl OneLog {
38 pub fn extract_hexe(&self) -> Result<Vec<u8>, TramexError> {
42 extract_hexe(&self.data)
43 }
44
45 pub fn extract_canal_msg(&self) -> Option<String> {
47 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 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}