version_manager/cli/
version.rs

1use crate::{
2    VersionError,
3    cli::{
4        files::FilesCommand,
5        getset::{GetSet, GetSetBuild, GetSetRm, SetVer},
6        package::PackageCommand,
7    },
8    version::Scope,
9};
10use clap::Subcommand;
11
12#[derive(Subcommand, Debug, Clone, PartialEq)]
13/// Specify the version number to change
14#[command(rename_all = "lower", arg_required_else_help(true))]
15pub enum VersionCommand {
16    /// Change the major version number
17    Major(GetSet),
18    /// Change the minor version number
19    Minor(GetSet),
20    /// Change the patch version number
21    Patch(GetSet),
22    /// Change the alpha identifier
23    Alpha(GetSetRm),
24    /// Change the beta identifier
25    Beta(GetSetRm),
26    /// Change the release candidate identifier
27    RC(GetSetRm),
28    /// Change the build identifier
29    Build(GetSetBuild),
30    /// Get the current version number as a full SemVer string
31    Get,
32    /// Set the version number to a specific version
33    Set(SetVer),
34    /// Get just the version number as a string with no revision or build identifiers
35    Version,
36    /// Get just the revision number as a string with no build identifiers
37    Revision,
38    /// Track and update the version number in a file
39    File(FilesCommand),
40    /// Track and update the version number in a file
41    Package(PackageCommand),
42}
43
44impl TryFrom<&VersionCommand> for Scope {
45    type Error = VersionError;
46
47    fn try_from(cmd: &VersionCommand) -> Result<Self, VersionError> {
48        let scope = match cmd {
49            VersionCommand::Major(getset) => Scope::Major(getset.try_into()?),
50            VersionCommand::Minor(getset) => Scope::Minor(getset.try_into()?),
51            VersionCommand::Patch(getset) => Scope::Patch(getset.try_into()?),
52            VersionCommand::Alpha(getset) => Scope::Alpha(getset.try_into()?),
53            VersionCommand::Beta(getset) => Scope::Beta(getset.try_into()?),
54            VersionCommand::RC(getset) => Scope::RC(getset.try_into()?),
55            VersionCommand::Build(getset) => Scope::Build(getset.try_into()?),
56            VersionCommand::Get => Scope::Get,
57            VersionCommand::Version => Scope::Version,
58            VersionCommand::Revision => Scope::Revision,
59            VersionCommand::File(file_cmd) => Scope::File(file_cmd.try_into()?),
60            VersionCommand::Package(package_cmd) => package_cmd.try_into()?,
61            VersionCommand::Set(setver) => Scope::Set(setver.try_into()?),
62        };
63        Ok(scope)
64    }
65}
66
67impl TryFrom<VersionCommand> for Scope {
68    type Error = VersionError;
69
70    fn try_from(cmd: VersionCommand) -> Result<Self, VersionError> {
71        let scope = match cmd {
72            VersionCommand::Major(getset) => Scope::Major(getset.try_into()?),
73            VersionCommand::Minor(getset) => Scope::Minor(getset.try_into()?),
74            VersionCommand::Patch(getset) => Scope::Patch(getset.try_into()?),
75            VersionCommand::Alpha(getset) => Scope::Alpha(getset.try_into()?),
76            VersionCommand::Beta(getset) => Scope::Beta(getset.try_into()?),
77            VersionCommand::RC(getset) => Scope::RC(getset.try_into()?),
78            VersionCommand::Build(getset) => Scope::Build(getset.try_into()?),
79            VersionCommand::Get => Scope::Get,
80            VersionCommand::Version => Scope::Version,
81            VersionCommand::Revision => Scope::Revision,
82            VersionCommand::File(file_cmd) => Scope::File(file_cmd.try_into()?),
83            VersionCommand::Package(package_cmd) => package_cmd.try_into()?,
84            VersionCommand::Set(setver) => Scope::Set(setver.try_into()?),
85        };
86        Ok(scope)
87    }
88}