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
use crate::{
    version::{Operator, SetTypes, VersionFile},
    VersionError,
};
use clap::{builder::RangedU64ValueParser, Args, Subcommand};

#[derive(Args, Debug, Clone)]
/// Set the version number
pub struct Set {
    #[arg(value_parser = RangedU64ValueParser::<u8>::new(), exclusive = true)]
    /// The value to set the version number to
    pub value: Option<u8>,
    #[command(subcommand)]
    /// Increment or decrement the version number by 1
    pub command: Option<UpDown>,
}

impl Set {
    pub fn run(&self, version: &mut VersionFile) -> Result<(), VersionError> {
        if let Some(value) = &self.value {
            version.operator = Some(Operator::Set(SetTypes::Number(*value)));
            version.run()
        } else if let Some(command) = &self.command {
            command.run(version)
        } else {
            Err(VersionError::NoValue)
        }
    }
}

#[derive(Subcommand, Debug, Clone)]
#[command(rename_all = "lower", arg_required_else_help(true))]
pub enum UpDown {
    #[command(name = "+")]
    /// Increment the version number by 1
    Up,
    #[command(name = "-")]
    /// Decrement the version number by 1
    Down,
}

impl UpDown {
    pub fn run(&self, version: &mut VersionFile) -> Result<(), VersionError> {
        match self {
            UpDown::Up => {
                version.operator = Some(Operator::Set(SetTypes::AddNumber));
                version.run()
            }
            UpDown::Down => {
                version.operator = Some(Operator::Set(SetTypes::SubNumber));
                version.run()
            }
        }
    }
}