summaryrefslogtreecommitdiffstats
path: root/src/commands/quit.rs
blob: aa19d5ba37e2584a64d9a1048b4af06f2af3d6b3 (plain)
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
use crate::commands::{JoshutoCommand, JoshutoRunnable};
use crate::context::JoshutoContext;
use crate::error::JoshutoError;
use crate::window::JoshutoView;

#[derive(Clone, Debug)]
pub struct Quit;

impl Quit {
    pub fn new() -> Self {
        Self::default()
    }
    pub const fn command() -> &'static str {
        "quit"
    }

    pub fn quit(context: &mut JoshutoContext) -> Result<(), JoshutoError> {
        if !context.threads.is_empty() {
            let err = std::io::Error::new(
                std::io::ErrorKind::Other,
                "operations running in background, use force_quit to quit",
            );
            Err(JoshutoError::IO(err))
        } else {
            context.exit = true;
            Ok(())
        }
    }
}

impl JoshutoCommand for Quit {}

impl std::fmt::Display for Quit {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        f.write_str(Self::command())
    }
}

impl JoshutoRunnable for Quit {
    fn execute(&self, context: &mut JoshutoContext, _: &JoshutoView) -> Result<(), JoshutoError> {
        Self::quit(context)
    }
}

impl std::default::Default for Quit {
    fn default() -> Self {
        Quit
    }
}

#[derive(Clone, Debug)]
pub struct ForceQuit;

impl ForceQuit {
    pub fn new() -> Self {
        ForceQuit
    }
    pub const fn command() -> &'static str {
        "force_quit"
    }

    pub fn force_quit(context: &mut JoshutoContext) {
        context.exit = true;
    }
}

impl JoshutoCommand for ForceQuit {}

impl std::fmt::Display for ForceQuit {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        f.write_str(Self::command())
    }
}

impl JoshutoRunnable for ForceQuit {
    fn execute(&self, context: &mut JoshutoContext, _: &JoshutoView) -> Result<(), JoshutoError> {
        Self::force_quit(context);
        Ok(())
    }
}