github_actions/
stop_commands.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
fn start_stop_commands(token: &str) {
    println!(
        "{}",
        crate::Command {
            command: "stop-commands",
            value: token,
            properties: None
        }
    );
}

fn end_stop_commands(token: &str) {
    println!(
        "{}",
        crate::Command {
            command: token,
            value: "",
            properties: None
        }
    );
}

pub struct StopCommands<T: AsRef<str>> {
    token: T,
}

impl<T: AsRef<str>> StopCommands<T> {
    pub fn new(token: T) -> Self {
        start_stop_commands(token.as_ref());
        Self { token }
    }

    pub fn end(&self) {
        end_stop_commands(self.token.as_ref());
    }
}

impl StopCommands<String> {
    pub fn gen() -> Self {
        StopCommands::<String>::new(uuid::Uuid::new_v4().to_string())
    }
}

pub struct StopCommandsEnder<'a, T: AsRef<str>> {
    parent: &'a StopCommands<T>,
}

impl<'a, T: AsRef<str>> StopCommandsEnder<'a, T> {
    pub fn new(stop_commands: &'a StopCommands<T>) -> Self {
        Self {
            parent: stop_commands,
        }
    }
}

impl<'a, T: AsRef<str>> Drop for StopCommandsEnder<'a, T> {
    fn drop(&mut self) {
        self.parent.end();
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test() {
        {
            let stop_cmds = StopCommands::new("token1");
            let _stop_cmds_ender = StopCommandsEnder::new(&stop_cmds);
        }
        {
            let _stop_cmds = StopCommands::new("token2");
            // it doesn't end
        }
    }
}