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
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use crate::data::ast::Interval;

use lazy_static::*;
use std::collections::*;
use std::sync::*;
use std::thread::*;

////////////////////////////////////////////////////////////////////////////////
// DATA STRUCTURES
////////////////////////////////////////////////////////////////////////////////

lazy_static! {
    static ref HASHMAP: Mutex<HashMap<ThreadId, Linter>> = Mutex::new(HashMap::default());
}

#[derive(Debug, Clone)]
pub struct Goto {
    pub src_flow: String,
    pub src_step: String,
    pub dst_flow: String,
    pub dst_step: String,
    pub interval: Interval,
}

#[derive(Debug, Clone)]
pub struct Linter {
    pub flow: HashMap<String, HashMap<String, Vec<Interval>>>, // can be change ?
    pub goto: Vec<Goto>,
}

////////////////////////////////////////////////////////////////////////////////
// TRAIT FUNCTIONS
////////////////////////////////////////////////////////////////////////////////

impl Default for Linter {
    fn default() -> Self {
        Self {
            flow: HashMap::default(),
            goto: Vec::default(),
        }
    }
}

////////////////////////////////////////////////////////////////////////////////
// PRIVATE FUNCTIONS
////////////////////////////////////////////////////////////////////////////////

impl Goto {
    fn new(
        src_flow: &str,
        src_step: &str,
        dst_flow: &str,
        dst_step: &str,
        interval: Interval,
    ) -> Self {
        Self {
            src_flow: src_flow.to_owned(),
            src_step: src_step.to_owned(),
            dst_flow: dst_flow.to_owned(),
            dst_step: dst_step.to_owned(),
            interval,
        }
    }
}

////////////////////////////////////////////////////////////////////////////////
// PUBLIC FUNCTIONS
////////////////////////////////////////////////////////////////////////////////

impl Linter {
    pub fn add_flow(flow: &str) {
        let thread_id = current().id();
        let mut hashmap = HASHMAP.lock().unwrap();

        hashmap.entry(thread_id).or_insert_with(Linter::default);

        if let Some(linter) = hashmap.get_mut(&thread_id) {
            linter
                .flow
                .entry(flow.to_owned())
                .or_insert_with(HashMap::default);
        }
    }

    pub fn add_step(flow: &str, step: &str, interval: Interval) {
        let thread_id = current().id();
        let mut hashmap = HASHMAP.lock().unwrap();

        hashmap.entry(thread_id).or_insert_with(Linter::default);

        if let Some(linter) = hashmap.get_mut(&thread_id) {
            linter
                .flow
                .entry(flow.to_owned())
                .or_insert_with(HashMap::default);

            if let Some(hashmap_step) = linter.flow.get_mut(flow) {
                match hashmap_step.get_mut(step) {
                    Some(vector_step) => {
                        vector_step.push(interval);
                    }
                    None => {
                        hashmap_step.insert(step.to_owned(), vec![interval]);
                    }
                }
            }
        }
    }

    pub fn add_goto(
        src_flow: &str,
        src_step: &str,
        dst_flow: &str,
        dst_step: &str,
        interval: Interval,
    ) {
        let thread_id = current().id();
        let mut hashmap = HASHMAP.lock().unwrap();

        hashmap.entry(thread_id).or_insert_with(Linter::default);

        if let Some(linter) = hashmap.get_mut(&thread_id) {
            linter
                .goto
                .push(Goto::new(src_flow, src_step, dst_flow, dst_step, interval));
        }
    }

    pub fn clear() {
        let thread_id = current().id();
        let mut hashmap = HASHMAP.lock().unwrap();

        hashmap.entry(thread_id).or_insert_with(Linter::default);

        if let Some(linter) = hashmap.get_mut(&thread_id) {
            linter.flow.clear();
            linter.goto.clear();
        }
    }
}

impl Linter {
    pub fn get_linter() -> Linter {
        let thread_id = current().id();
        let mut hashmap = HASHMAP.lock().unwrap();

        hashmap.entry(thread_id).or_insert_with(Linter::default);

        if let Some(linter) = hashmap.get(&thread_id) {
            (*linter).to_owned()
        } else {
            unreachable!();
        }
    }
}