1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//! Parser for file interface

pub mod parser_rrc;

use crate::data::AdditionalInfos;
use crate::data::Trace;

use crate::errors::ErrorCode;
use crate::errors::TramexError;
use crate::tramex_error;
use chrono::NaiveTime;
use chrono::Timelike;

/// Parsing error
pub struct ParsingError {
    /// Error message
    pub message: String,

    /// Line index
    pub line_idx: u64,
}

impl ParsingError {
    /// Create a new parsing error
    pub fn new(message: String, line_idx: u64) -> Self {
        Self { message, line_idx }
    }
}

/// Convert a parsing error to a tramex error
#[inline]
pub fn parsing_error_to_tramex_error(error: ParsingError, idx: u64) -> TramexError {
    let index = idx + error.line_idx;
    tramex_error!(format!("{} (line {})", error.message, index), ErrorCode::FileParsing)
}

/// Trait for file parser
pub trait FileParser {
    /// Function that parses the first line of a log
    /// # Errors
    /// Return an error if the parsing fails
    fn parse_additional_infos(line: &[String]) -> Result<AdditionalInfos, ParsingError>;

    /// Parse the lines of a file
    /// # Errors
    /// Return an error if the parsing fails
    fn parse(lines: &[String]) -> Result<Trace, ParsingError>;
}

/// Convert a time to milliseconds.
#[inline]
pub fn time_to_milliseconds(time: &NaiveTime) -> i64 {
    let hours_in_ms = time.hour() as i64 * 3_600_000;
    let minutes_in_ms = time.minute() as i64 * 60_000;
    let seconds_in_ms = time.second() as i64 * 1000;
    let milliseconds = time.nanosecond() as i64 / 1_000_000; // convert nanoseconds to milliseconds

    hours_in_ms + minutes_in_ms + seconds_in_ms + milliseconds
}

/// Build a eof_error
#[inline]
pub fn eof_error(line_idx: u64) -> TramexError {
    tramex_error!(
        format!("End of file (line {})", line_idx),
        crate::errors::ErrorCode::EndOfFile
    )
}