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
use std::sync::{Arc, Mutex};
use crate::data::{File, FilePath};

pub type SharedJob = Arc<Job>;

static JOB_COUNTER: Mutex<usize> = Mutex::new(0);

fn new_job_counter_id() -> usize {
    let mut counter = JOB_COUNTER.lock().expect("Failed to lock job counter");
    *counter += 1;
    (*counter).clone()
}

#[derive(Debug, Clone, PartialEq, Copy)]
pub enum JobState {
    NotProcessed,
    Analyzed,
}

#[derive(Debug)]
pub struct Job {
    id: usize,
    pub parent: Option<SharedJob>,
    pub finished_children: Mutex<Vec<File>>,
    pub target_path: FilePath,
    pub state: JobState,
}

impl Job {
    pub fn new(parent: Option<SharedJob>, target_path: FilePath) -> Self {
        Job {
            id: new_job_counter_id(),
            parent,
            target_path,
            state: JobState::NotProcessed,
            finished_children: Mutex::new(Vec::new()),
        }
    }
    
    pub fn job_id(&self) -> usize {
        self.id
    }

    pub(crate) fn new_job_id(mut self) -> Self {
        self.id = new_job_counter_id();
        self
    }
}

impl JobTrait for Job {
    fn job_id(&self) -> usize {
        Job::job_id(self)
    }
}


pub trait JobTrait<T: std::marker::Send = Self> {
    fn job_id(&self) -> usize;
}

pub trait ResultTrait<T: std::marker::Send = Self> {}