glory_cli/compile/
change.rs

1use std::vec;
2
3use crate::service::notify::Watched;
4
5#[derive(Debug, Clone, PartialEq, Eq)]
6pub enum Change {
7    /// sent when a bin target source file is changed
8    BinSource,
9    /// sent when a lib target source file is changed
10    LibSource,
11    /// sent when an asset file changed
12    Asset(Watched),
13    /// sent when a style file changed
14    Style,
15    /// Cargo.toml changed
16    Conf,
17}
18
19#[derive(Debug, Default, Clone)]
20pub struct ChangeSet(Vec<Change>);
21
22impl ChangeSet {
23    pub fn all_changes() -> Self {
24        Self(vec![
25            Change::BinSource,
26            Change::LibSource,
27            Change::Style,
28            Change::Conf,
29            Change::Asset(Watched::Rescan),
30        ])
31    }
32
33    pub fn is_empty(&self) -> bool {
34        self.0.is_empty()
35    }
36
37    pub fn clear(&mut self) {
38        self.0.clear()
39    }
40
41    pub fn need_server_build(&self) -> bool {
42        self.0.contains(&Change::BinSource) || self.0.contains(&Change::Conf)
43    }
44
45    pub fn need_front_build(&self) -> bool {
46        self.0.contains(&Change::LibSource) || self.0.contains(&Change::Conf)
47    }
48
49    pub fn asset_iter(&self) -> impl Iterator<Item = &Watched> {
50        self.0.iter().filter_map(|change| match change {
51            Change::Asset(a) => Some(a),
52            _ => None,
53        })
54    }
55
56    pub fn need_style_build(&self, css_files: bool, css_in_source: bool) -> bool {
57        (css_files && self.0.contains(&Change::Style)) || (css_in_source && self.0.contains(&Change::LibSource))
58    }
59
60    pub fn add(&mut self, change: Change) -> bool {
61        if !self.0.contains(&change) {
62            self.0.push(change);
63            true
64        } else {
65            false
66        }
67    }
68}