tramex_tools/interface/parser/
mod.rs

1//! Parser for file interface
2
3pub mod parser_rrc;
4
5use crate::data::AdditionalInfos;
6use crate::data::Trace;
7
8use crate::errors::ErrorCode;
9use crate::errors::TramexError;
10use crate::tramex_error;
11use chrono::NaiveTime;
12use chrono::Timelike;
13
14/// Parsing error
15pub struct ParsingError {
16    /// Error message
17    pub message: String,
18
19    /// Line index
20    pub line_idx: u64,
21}
22
23impl ParsingError {
24    /// Create a new parsing error
25    pub fn new(message: String, line_idx: u64) -> Self {
26        Self { message, line_idx }
27    }
28}
29
30/// Convert a parsing error to a tramex error
31#[inline]
32pub fn parsing_error_to_tramex_error(error: ParsingError, idx: u64) -> TramexError {
33    let index = idx + error.line_idx;
34    tramex_error!(format!("{} (line {})", error.message, index), ErrorCode::FileParsing)
35}
36
37/// Trait for file parser
38pub trait FileParser {
39    /// Function that parses the first line of a log
40    /// # Errors
41    /// Return an error if the parsing fails
42    fn parse_additional_infos(line: &[String]) -> Result<AdditionalInfos, ParsingError>;
43
44    /// Parse the lines of a file
45    /// # Errors
46    /// Return an error if the parsing fails
47    fn parse(lines: &[String]) -> Result<Trace, ParsingError>;
48}
49
50/// Convert a time to milliseconds.
51#[inline]
52pub fn time_to_milliseconds(time: &NaiveTime) -> i64 {
53    let hours_in_ms = time.hour() as i64 * 3_600_000;
54    let minutes_in_ms = time.minute() as i64 * 60_000;
55    let seconds_in_ms = time.second() as i64 * 1000;
56    let milliseconds = time.nanosecond() as i64 / 1_000_000; // convert nanoseconds to milliseconds
57
58    hours_in_ms + minutes_in_ms + seconds_in_ms + milliseconds
59}
60
61/// Build a eof_error
62#[inline]
63pub fn eof_error(line_idx: u64) -> TramexError {
64    tramex_error!(
65        format!("End of file (line {})", line_idx),
66        crate::errors::ErrorCode::EndOfFile
67    )
68}