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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use crate::{
    files::TrackedFiles,
    version::{Operator, SetTypes, VersionFile},
    VersionError,
};
use clap::{value_parser, Args, Parser, Subcommand};
use clio::ClioPath;
use regex::Regex;

#[derive(Parser, Debug, Clone)]
#[command(arg_required_else_help(true))]
pub struct FilesCommand {
    #[clap(subcommand)]
    pub command: Files,
}

#[derive(Subcommand, Debug, Clone)]
#[command(arg_required_else_help(true))]
pub enum Files {
    /// Add a file to add the version number
    Track(TrackFile),
    /// Remove a file from tracking the version number
    Rm(File),
    /// Set the version number from a file
    Update(File),
    /// Update all files
    UpdateAll,
}

#[derive(Args, Debug, Clone)]
#[command(arg_required_else_help(true))]
pub struct TrackFile {
    /// The path to the file to track
    #[arg(value_parser = value_parser!(ClioPath).exists().is_file())]
    pub path: ClioPath,
    /// The expression to match the version number
    ///
    /// This expression should be a regex with a single capture group that matches the version number
    #[arg(value_parser = value_parser!(Regex))]
    pub expr: Regex,
}

#[derive(Args, Debug, Clone)]
#[command(arg_required_else_help(true))]
pub struct File {
    /// The path to the file
    #[arg(value_parser = clap::value_parser!(ClioPath).exists().is_file())]
    pub path: ClioPath,
}

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

impl TrackFile {
    pub fn run(&self, version: &mut VersionFile) -> Result<(), VersionError> {
        let track_file =
            TrackedFiles::new_from_path_and_regex(self.path.to_path_buf(), self.expr.clone());
        version.value = Some(SetTypes::NewFile(track_file));
        version.run()
    }
}

impl File {
    pub fn run(&self, version: &mut VersionFile) -> Result<(), VersionError> {
        version.value = Some(SetTypes::String(self.path.to_string()));
        version.run()
    }
}

impl Files {
    pub fn run(&self, version: &mut VersionFile) -> Result<(), VersionError> {
        match self {
            Files::Track(track) => {
                version.operator = Some(Operator::AddFile);
                track.run(version)
            }
            Files::Rm(file) => {
                version.operator = Some(Operator::RmFile);
                file.run(version)
            }
            Files::Update(file) => {
                version.operator = Some(Operator::Update);
                file.run(version)
            }
            Files::UpdateAll => {
                version.operator = Some(Operator::UpdateAll);
                version.run()
            }
        }
    }
}