version_manager/cli/getset/
set.rs

1use crate::{VersionError, version::SetTypes};
2use clap::{Args, Subcommand, builder::RangedU64ValueParser};
3
4#[derive(Args, Debug, Clone, PartialEq)]
5/// Set the version number
6pub struct Set {
7    #[arg(value_parser = RangedU64ValueParser::<u64>::new(), exclusive = true)]
8    /// The value to set the version number to
9    pub value: Option<u64>,
10    #[command(subcommand)]
11    /// Increment or decrement the version number by 1
12    pub command: Option<UpDown>,
13}
14
15impl TryFrom<&Set> for SetTypes {
16    type Error = VersionError;
17
18    fn try_from(set: &Set) -> Result<Self, VersionError> {
19        if let Some(value) = &set.value {
20            return Ok(SetTypes::Number(*value));
21        } else if let Some(command) = &set.command {
22            return Ok(command.try_into()?);
23        } else {
24            Err(VersionError::NoValue)
25        }
26    }
27}
28
29impl TryFrom<Set> for SetTypes {
30    type Error = VersionError;
31
32    fn try_from(set: Set) -> Result<Self, VersionError> {
33        if let Some(value) = &set.value {
34            return Ok(SetTypes::Number(*value));
35        } else if let Some(command) = &set.command {
36            return Ok(command.try_into()?);
37        } else {
38            Err(VersionError::NoValue)
39        }
40    }
41}
42
43#[derive(Subcommand, Debug, Clone, PartialEq)]
44#[command(rename_all = "lower", arg_required_else_help(true))]
45pub enum UpDown {
46    #[command(name = "+")]
47    /// Increment the version number by 1
48    Plus,
49    #[command(name = "-")]
50    /// Decrement the version number by 1
51    Minus,
52    /// Increment the version number by 1
53    Up,
54    /// Decrement the version number by 1
55    Down,
56}
57
58impl TryFrom<&UpDown> for SetTypes {
59    type Error = VersionError;
60
61    fn try_from(updown: &UpDown) -> Result<Self, VersionError> {
62        match updown {
63            UpDown::Up => Ok(SetTypes::AddNumber),
64            UpDown::Down => Ok(SetTypes::SubNumber),
65            UpDown::Plus => Ok(SetTypes::AddNumber),
66            UpDown::Minus => Ok(SetTypes::SubNumber),
67        }
68    }
69}
70
71impl TryFrom<UpDown> for SetTypes {
72    type Error = VersionError;
73
74    fn try_from(updown: UpDown) -> Result<Self, VersionError> {
75        match updown {
76            UpDown::Up => Ok(SetTypes::AddNumber),
77            UpDown::Down => Ok(SetTypes::SubNumber),
78            UpDown::Plus => Ok(SetTypes::AddNumber),
79            UpDown::Minus => Ok(SetTypes::SubNumber),
80        }
81    }
82}