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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//! utils functions for file interface

use std::str::FromStr;

use crate::errors::ErrorCode;
use crate::interface::parser::{parsing_error_to_tramex_error, FileParser};
use crate::tramex_error;
// to use the FileParser trait and implementations
use crate::{
    data::Trace,
    errors::TramexError,
    interface::{
        layer::Layer,
        parser::{eof_error, parser_rrc::RRCParser, time_to_milliseconds},
    },
};

/// Function that parses one log
/// # Errors
/// Return an error if the parsing fails
pub fn parse_one_block(lines: &[String], ix: &mut usize) -> Result<Trace, TramexError> {
    // no more lines to read
    if lines.is_empty() {
        return Err(eof_error(*ix as u64));
    }
    let mut start_line = 0;
    let mut end_line = 0;
    let mut should_stop = false;
    for one_line in lines.iter() {
        end_line += 1;
        if one_line.starts_with('#') {
            start_line += 1;
            continue;
        } else if one_line.starts_with(' ') || one_line.starts_with('\t') || one_line.trim().is_empty() {
            continue;
        } else {
            if should_stop {
                end_line -= 1;
                break;
            }
            should_stop = true;
        }
    }
    if end_line == 1 && (lines[0].starts_with(' ') || lines[0].starts_with('\t') || lines[0].trim().is_empty()) {
        return Err(eof_error(*ix as u64));
    }
    let lines_to_parse = &lines[start_line..end_line];
    let copy_ix = *ix + start_line;
    *ix += end_line;
    match lines_to_parse.first() {
        Some(first_line) => {
            let parts: Vec<&str> = first_line.split_whitespace().collect();
            if parts.is_empty() {
                return Err(tramex_error!(
                    format!("Not enough parts in the line {:?} (line {})", first_line, copy_ix as u64 + 1),
                    ErrorCode::FileParsing
                ));
            }
            let date = match chrono::NaiveTime::parse_from_str(parts[0], "%H:%M:%S%.3f") {
                Ok(rdate) => rdate,
                Err(_) => {
                    return Err(tramex_error!(
                        format!(
                            "Error while parsing date {:?} in {:?} (line {})",
                            parts[0],
                            first_line,
                            copy_ix + 1
                        ),
                        ErrorCode::FileParsing
                    ));
                }
            };
            let res_layer = Layer::from_str(parts[1].trim_start_matches('[').trim_end_matches(']'));
            let res_parse = match res_layer {
                Ok(Layer::RRC) => RRCParser::parse(lines_to_parse),
                _ => {
                    return Err(tramex_error!(
                        format!(
                            "Unknown message type {:?} in {:?} (line {})",
                            parts[1],
                            first_line,
                            copy_ix + 1
                        ),
                        ErrorCode::ParsingLayerNotImplemented
                    ));
                }
            };
            match res_parse {
                Ok(mut trace) => {
                    trace.timestamp = time_to_milliseconds(&date) as u64;
                    Ok(trace)
                }
                Err(err) => Err(parsing_error_to_tramex_error(err, copy_ix as u64)),
            }
        }
        None => Err(eof_error(copy_ix as u64)),
    }
}