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
use std::collections::BTreeMap;

use crate::prelude::ForensicError;

#[derive(Default)]
pub struct Bitacora<T : Default> {
    pub data : T,
    pub errors : BTreeMap<String, Vec<(String, ForensicError)>>
}

impl<T : Default> Bitacora <T> {
    pub fn new( data : T) -> Self {
        Self { data, errors: BTreeMap::default() }
    }

    pub fn error(parser : &str, element : String, err: ForensicError) -> Self {
        let mut errors = BTreeMap::default();
        errors.insert(parser.to_string(), vec![(element, err)]);
        Self {
            data : T::default(),
            errors
        }
    }

    pub fn copy_errors(&mut self, errors : &mut BTreeMap<String, Vec<(String, ForensicError)>>) {
        self.errors.append(errors);
    }

    pub fn add_error(&mut self, parser : &str, element : String, err : ForensicError) {
        let mut error_list = match self.errors.remove(parser) {
            Some(v) => v,
            None => vec![]
        };
        error_list.push((element, err));
        self.errors.insert(parser.to_string(), error_list);
    }
    pub fn add_errors(&mut self, parser : &str, mut errors : Vec<(String, ForensicError)>) {
        let mut error_list = match self.errors.remove(parser) {
            Some(v) => v,
            None => vec![]
        };
        error_list.append(&mut errors);
        self.errors.insert(parser.to_string(), error_list);
    }

    pub fn copy(&mut self, other : &Self) {
        for (parser_name, error_list) in &other.errors {
            if !self.errors.contains_key(parser_name) {
                self.errors.insert(parser_name.to_string(), error_list.clone());
            }else{
                let mut data = self.errors.remove(parser_name).unwrap_or_else(|| vec![]);
                for error in error_list {
                    data.push(error.clone());
                }
                self.errors.insert(parser_name.to_string(), data);
            }
        }
    }
    pub fn join(&mut self, other : Self) {
        for (parser_name, error_list) in other.errors {
            if !self.errors.contains_key(&parser_name) {
                self.errors.insert(parser_name, error_list);
            }else{
                let mut data = self.errors.remove(&parser_name).unwrap_or_else(|| vec![]);
                for error in error_list {
                    data.push(error);
                }
                self.errors.insert(parser_name, data);
            }
        }
    }
}