darklua_core/frontend/
work_item.rs

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
156
157
158
159
160
161
162
163
164
165
use std::path::{Path, PathBuf};

use crate::{nodes::Block, utils::Timer};

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Progress {
    block: Block,
    next_rule: usize,
    required: Vec<PathBuf>,
    duration: Timer,
}

impl Progress {
    pub fn new(block: Block) -> Self {
        Self {
            block,
            next_rule: 0,
            required: Vec::new(),
            duration: Timer::now(),
        }
    }

    pub fn with_content(self, content: String) -> WorkProgress {
        WorkProgress {
            content,
            work: self,
        }
    }

    pub fn at_rule(mut self, rule_index: usize) -> Self {
        self.next_rule = rule_index;
        self
    }

    pub fn with_required_content(mut self, required_content: Vec<PathBuf>) -> Self {
        self.required = required_content;
        self
    }

    pub fn next_rule(&self) -> usize {
        self.next_rule
    }

    pub fn block(&self) -> &Block {
        &self.block
    }

    pub fn mutate_block(&mut self) -> &mut Block {
        &mut self.block
    }

    pub fn duration(&mut self) -> &mut Timer {
        &mut self.duration
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct WorkProgress {
    content: String,
    work: Progress,
}

impl WorkProgress {
    pub fn new(content: String, block: Block) -> Self {
        Self {
            content,
            work: Progress::new(block),
        }
    }

    pub fn required_content(&self) -> impl Iterator<Item = &Path> {
        self.work.required.iter().map(AsRef::as_ref)
    }

    pub fn extract(self) -> (String, Progress) {
        (self.content, self.work)
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum WorkStatus {
    NotStarted,
    InProgress(Box<WorkProgress>),
}

impl Default for WorkStatus {
    fn default() -> Self {
        Self::NotStarted
    }
}

impl From<WorkProgress> for WorkStatus {
    fn from(progress: WorkProgress) -> Self {
        Self::InProgress(Box::new(progress))
    }
}

#[derive(Debug, Clone)]
pub(crate) struct WorkData {
    source: PathBuf,
    output: PathBuf,
}

impl WorkData {
    pub fn with_status(self, status: impl Into<WorkStatus>) -> WorkItem {
        WorkItem {
            data: self,
            status: status.into(),
        }
    }

    pub fn source(&self) -> &Path {
        &self.source
    }

    pub fn output(&self) -> &Path {
        &self.output
    }
}

#[derive(Debug, Clone)]
pub(crate) struct WorkItem {
    data: WorkData,
    status: WorkStatus,
}

impl WorkItem {
    pub fn new(source: impl Into<PathBuf>, output: impl Into<PathBuf>) -> Self {
        Self {
            data: WorkData {
                source: source.into(),
                output: output.into(),
            },
            status: Default::default(),
        }
    }

    pub fn new_in_place(source: impl Into<PathBuf>) -> Self {
        let source = source.into();
        Self::new(source.clone(), source)
    }

    pub fn extract(self) -> (WorkStatus, WorkData) {
        (self.status, self.data)
    }

    pub fn source(&self) -> &Path {
        &self.data.source
    }

    pub fn get_created_file_path(&self) -> Option<PathBuf> {
        if self.data.source == self.data.output {
            None
        } else {
            Some(self.data.output.to_path_buf())
        }
    }

    pub fn total_required_content(&self) -> usize {
        match &self.status {
            WorkStatus::NotStarted => 0,
            WorkStatus::InProgress(progress) => progress.work.required.len(),
        }
    }
}