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
use derive_more::Display;
use itertools::Itertools;
use std::{collections::HashSet, fmt};
use tokio::sync::broadcast;

lazy_static::lazy_static! {
  static ref SERVER_RESTART_CHANNEL: broadcast::Sender::<()> = broadcast::channel::<()>(1).0;
}

#[derive(Debug, PartialEq, Eq, Hash)]
pub enum Outcome<T> {
    Success(T),
    Stopped,
    Failed,
}

impl<T> Outcome<T> {
    pub fn is_success(&self) -> bool {
        matches!(self, Outcome::Success(_))
    }
}

#[derive(Debug, Display, Clone, PartialEq, Eq, Hash)]
pub enum Product {
    Server,
    Front,
    Style(String),
    Assets,
    None,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProductSet(HashSet<Product>);

impl ProductSet {
    pub fn empty() -> Self {
        Self(HashSet::new())
    }

    pub fn from(vec: Vec<Outcome<Product>>) -> Self {
        Self(HashSet::from_iter(vec.into_iter().filter_map(|entry| match entry {
            Outcome::Success(Product::None) => None,
            Outcome::Success(v) => Some(v),
            _ => None,
        })))
    }

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

    pub fn only_style(&self) -> bool {
        self.0.len() == 1 && self.0.iter().any(|p| matches!(p, Product::Style(_)))
    }

    pub fn contains(&self, product: &Product) -> bool {
        self.0.contains(product)
    }

    pub fn contains_any(&self, of: &[Product]) -> bool {
        of.iter().any(|p| self.0.contains(p))
    }
}

impl fmt::Display for ProductSet {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0.iter().map(|f| f.to_string()).collect_vec().join(", "))
    }
}

pub struct ServerRestart {}

impl ServerRestart {
    pub fn subscribe() -> broadcast::Receiver<()> {
        SERVER_RESTART_CHANNEL.subscribe()
    }

    pub fn send() {
        log::trace!("Server restart sent");
        if let Err(e) = SERVER_RESTART_CHANNEL.send(()) {
            log::error!("Error could not send product changes due to {e}")
        }
    }
}