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

#[derive(Clone)]
#[cfg_attr(debug_assertions, derive(Debug))]
pub struct Command {
    pub id: i64,
    pub category: String,
    pub alias: Option<String>,
    pub cmd: String,
    pub description: String,
    pub usage: u64,
}

impl Command {
    pub fn new(category: impl Into<String>, command: impl Into<String>, description: impl Into<String>) -> Self {
        Self {
            id: 0,
            category: category.into(),
            alias: None,
            cmd: command.into(),
            description: description.into(),
            usage: 0,
        }
    }

    pub fn increment_usage(&mut self) {
        self.usage += 1;
    }

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

impl Display for Command {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.cmd)
    }
}