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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use nu_protocol::{ast::CellPath, LabeledError, Span, Value};
use semver::{BuildMetadata, Prerelease, Version};

#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub enum Action {
    SemVerAction(SemVerAction),
    Default,
}

#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub enum SemVerAction {
    Major,
    Minor,
    Patch,
}

#[derive(Default, Clone)]
pub struct Inc {
    pub error: Option<String>,
    pub cell_path: Option<CellPath>,
    pub action: Option<Action>,
}

impl Inc {
    pub fn new() -> Self {
        Default::default()
    }

    fn apply(&self, input: &str, head: Span) -> Value {
        match &self.action {
            Some(Action::SemVerAction(act_on)) => {
                let mut ver = match semver::Version::parse(input) {
                    Ok(parsed_ver) => parsed_ver,
                    Err(_) => return Value::string(input, head),
                };

                match act_on {
                    SemVerAction::Major => Self::increment_major(&mut ver),
                    SemVerAction::Minor => Self::increment_minor(&mut ver),
                    SemVerAction::Patch => Self::increment_patch(&mut ver),
                }

                Value::string(ver.to_string(), head)
            }
            Some(Action::Default) | None => {
                if let Ok(v) = input.parse::<u64>() {
                    Value::string((v + 1).to_string(), head)
                } else {
                    Value::string(input, head)
                }
            }
        }
    }

    pub fn increment_patch(v: &mut Version) {
        v.patch += 1;
        v.pre = Prerelease::EMPTY;
        v.build = BuildMetadata::EMPTY;
    }

    pub fn increment_minor(v: &mut Version) {
        v.minor += 1;
        v.patch = 0;
        v.pre = Prerelease::EMPTY;
        v.build = BuildMetadata::EMPTY;
    }

    pub fn increment_major(v: &mut Version) {
        v.major += 1;
        v.minor = 0;
        v.patch = 0;
        v.pre = Prerelease::EMPTY;
        v.build = BuildMetadata::EMPTY;
    }

    pub fn for_semver(&mut self, part: SemVerAction) {
        if self.permit() {
            self.action = Some(Action::SemVerAction(part));
        } else {
            self.log_error("can only apply one");
        }
    }

    fn permit(&mut self) -> bool {
        self.action.is_none()
    }

    fn log_error(&mut self, message: &str) {
        self.error = Some(message.to_string());
    }

    pub fn usage() -> &'static str {
        "Usage: inc field [--major|--minor|--patch]"
    }

    pub fn inc(&self, head: Span, value: &Value) -> Result<Value, LabeledError> {
        if let Some(cell_path) = &self.cell_path {
            let working_value = value.clone();
            let cell_value = working_value.follow_cell_path(&cell_path.members, false)?;

            let cell_value = self.inc_value(head, &cell_value)?;

            let mut value = value.clone();
            value.update_data_at_cell_path(&cell_path.members, cell_value)?;
            Ok(value)
        } else {
            self.inc_value(head, value)
        }
    }

    pub fn inc_value(&self, head: Span, value: &Value) -> Result<Value, LabeledError> {
        match value {
            Value::Int { val, .. } => Ok(Value::int(val + 1, head)),
            Value::String { val, .. } => Ok(self.apply(val, head)),
            x => {
                let msg = x.coerce_string().map_err(|e| {
                    LabeledError::new("Unable to extract string").with_label(
                        format!("value cannot be converted to string {x:?} - {e}"),
                        head,
                    )
                })?;

                Err(LabeledError::new("Incorrect value").with_label(msg, head))
            }
        }
    }
}

#[cfg(test)]
mod tests {
    mod semver {
        use nu_protocol::{Span, Value};

        use crate::inc::SemVerAction;
        use crate::Inc;

        #[test]
        fn major() {
            let expected = Value::test_string("1.0.0");
            let mut inc = Inc::new();
            inc.for_semver(SemVerAction::Major);
            assert_eq!(inc.apply("0.1.3", Span::test_data()), expected)
        }

        #[test]
        fn minor() {
            let expected = Value::test_string("0.2.0");
            let mut inc = Inc::new();
            inc.for_semver(SemVerAction::Minor);
            assert_eq!(inc.apply("0.1.3", Span::test_data()), expected)
        }

        #[test]
        fn patch() {
            let expected = Value::test_string("0.1.4");
            let mut inc = Inc::new();
            inc.for_semver(SemVerAction::Patch);
            assert_eq!(inc.apply("0.1.3", Span::test_data()), expected)
        }
    }
}