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

use crate::service::notify::Watched;

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Change {
    /// sent when a bin target source file is changed
    BinSource,
    /// sent when a lib target source file is changed
    LibSource,
    /// sent when an asset file changed
    Asset(Watched),
    /// sent when a style file changed
    Style,
    /// Cargo.toml changed
    Conf,
}

#[derive(Debug, Default, Clone)]
pub struct ChangeSet(Vec<Change>);

impl ChangeSet {
    pub fn all_changes() -> Self {
        Self(vec![
            Change::BinSource,
            Change::LibSource,
            Change::Style,
            Change::Conf,
            Change::Asset(Watched::Rescan),
        ])
    }

    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    pub fn clear(&mut self) {
        self.0.clear()
    }

    pub fn need_server_build(&self) -> bool {
        self.0.contains(&Change::BinSource) || self.0.contains(&Change::Conf)
    }

    pub fn need_front_build(&self) -> bool {
        self.0.contains(&Change::LibSource) || self.0.contains(&Change::Conf)
    }

    pub fn asset_iter(&self) -> impl Iterator<Item = &Watched> {
        self.0.iter().filter_map(|change| match change {
            Change::Asset(a) => Some(a),
            _ => None,
        })
    }

    pub fn need_style_build(&self, css_files: bool, css_in_source: bool) -> bool {
        (css_files && self.0.contains(&Change::Style)) || (css_in_source && self.0.contains(&Change::LibSource))
    }

    pub fn add(&mut self, change: Change) -> bool {
        if !self.0.contains(&change) {
            self.0.push(change);
            true
        } else {
            false
        }
    }
}