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

#[derive(Parser, Debug, Clone)]
/// Get or set the build version
#[command(arg_required_else_help(true))]
pub struct GetSetBuild {
    #[command(subcommand)]
    pub command: GetSetBuildCommand,
}

impl GetSetBuild {
    pub fn run(&self, version: &mut VersionFile) -> Result<(), VersionError> {
        self.command.run(version)
    }
}

#[derive(Subcommand, Debug, Clone)]
/// Get or set the build version
pub enum GetSetBuildCommand {
    Get,
    Set(SetBuild),
    Rm,
}

impl GetSetBuildCommand {
    pub fn run(&self, version: &mut VersionFile) -> Result<(), VersionError> {
        match self {
            GetSetBuildCommand::Get => {
                version.operator = Some(Operator::Get);
                version.run()
            }
            GetSetBuildCommand::Set(set) => set.run(version),
            GetSetBuildCommand::Rm => {
                version.operator = Some(Operator::Rm);
                version.run()
            }
        }
    }
}

#[derive(Args, Debug, Clone)]
/// Set the build version
pub struct SetBuild {
    #[arg(value_parser = NonEmptyStringValueParser::new())]
    pub value: String,
}

impl SetBuild {
    pub fn run(&self, version: &mut VersionFile) -> Result<(), VersionError> {
        version.operator = Some(Operator::Set(SetTypes::String(self.value.clone())));
        version.run()
    }
}