2021-12-05 04:11:19 +01:00
|
|
|
use nu_plugin::LabeledError;
|
|
|
|
use nu_protocol::{Span, Value};
|
2019-12-29 06:17:24 +01:00
|
|
|
|
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
|
|
|
pub enum Action {
|
|
|
|
SemVerAction(SemVerAction),
|
|
|
|
Default,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
|
|
|
pub enum SemVerAction {
|
|
|
|
Major,
|
|
|
|
Minor,
|
|
|
|
Patch,
|
|
|
|
}
|
|
|
|
|
2019-12-31 08:36:08 +01:00
|
|
|
#[derive(Default)]
|
2019-12-29 06:17:24 +01:00
|
|
|
pub struct Inc {
|
|
|
|
pub error: Option<String>,
|
|
|
|
pub action: Option<Action>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Inc {
|
2019-12-31 08:36:08 +01:00
|
|
|
pub fn new() -> Self {
|
|
|
|
Default::default()
|
2019-12-29 06:17:24 +01:00
|
|
|
}
|
|
|
|
|
2021-12-19 08:46:13 +01:00
|
|
|
fn apply(&self, input: &str, head: Span) -> Value {
|
2021-02-12 11:13:14 +01:00
|
|
|
match &self.action {
|
2019-12-29 06:17:24 +01:00
|
|
|
Some(Action::SemVerAction(act_on)) => {
|
2021-06-09 21:08:12 +02:00
|
|
|
let mut ver = match semver::Version::parse(input) {
|
2019-12-29 06:17:24 +01:00
|
|
|
Ok(parsed_ver) => parsed_ver,
|
2021-11-04 23:04:21 +01:00
|
|
|
Err(_) => {
|
|
|
|
return Value::String {
|
|
|
|
val: input.to_string(),
|
2021-12-19 08:46:13 +01:00
|
|
|
span: head,
|
2021-11-04 23:04:21 +01:00
|
|
|
}
|
|
|
|
}
|
2019-12-29 06:17:24 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
match act_on {
|
|
|
|
SemVerAction::Major => ver.increment_major(),
|
|
|
|
SemVerAction::Minor => ver.increment_minor(),
|
|
|
|
SemVerAction::Patch => ver.increment_patch(),
|
|
|
|
}
|
|
|
|
|
2021-11-04 23:04:21 +01:00
|
|
|
Value::String {
|
|
|
|
val: ver.to_string(),
|
2021-12-19 08:46:13 +01:00
|
|
|
span: head,
|
2021-11-04 23:04:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(Action::Default) | None => match input.parse::<u64>() {
|
|
|
|
Ok(v) => Value::String {
|
|
|
|
val: (v + 1).to_string(),
|
2021-12-19 08:46:13 +01:00
|
|
|
span: head,
|
2021-11-04 23:04:21 +01:00
|
|
|
},
|
|
|
|
Err(_) => Value::String {
|
|
|
|
val: input.to_string(),
|
2021-12-19 08:46:13 +01:00
|
|
|
span: head,
|
2021-11-04 23:04:21 +01:00
|
|
|
},
|
2019-12-29 06:17:24 +01:00
|
|
|
},
|
2021-02-12 11:13:14 +01:00
|
|
|
}
|
2019-12-29 06:17:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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]"
|
|
|
|
}
|
|
|
|
|
2021-12-05 04:11:19 +01:00
|
|
|
pub fn inc(&self, head: Span, value: &Value) -> Result<Value, LabeledError> {
|
2021-11-04 23:04:21 +01:00
|
|
|
match value {
|
|
|
|
Value::Int { val, span } => Ok(Value::Int {
|
|
|
|
val: val + 1,
|
2021-11-05 04:59:12 +01:00
|
|
|
span: *span,
|
2021-11-04 23:04:21 +01:00
|
|
|
}),
|
2021-12-19 08:46:13 +01:00
|
|
|
Value::String { val, .. } => Ok(self.apply(val, head)),
|
2021-12-03 00:11:25 +01:00
|
|
|
x => {
|
2021-12-05 04:11:19 +01:00
|
|
|
let msg = x.as_string().map_err(|e| LabeledError {
|
|
|
|
label: "Unable to extract string".into(),
|
2022-01-13 20:40:25 +01:00
|
|
|
msg: format!("value cannot be converted to string {:?} - {}", x, e),
|
2021-12-05 04:11:19 +01:00
|
|
|
span: Some(head),
|
|
|
|
})?;
|
|
|
|
|
|
|
|
Err(LabeledError {
|
|
|
|
label: "Incorrect value".into(),
|
|
|
|
msg,
|
|
|
|
span: Some(head),
|
|
|
|
})
|
2021-12-03 00:11:25 +01:00
|
|
|
}
|
2019-12-29 06:17:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
mod semver {
|
2021-11-04 23:04:21 +01:00
|
|
|
use nu_protocol::{Span, Value};
|
|
|
|
|
|
|
|
use crate::inc::SemVerAction;
|
|
|
|
use crate::Inc;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn major() {
|
|
|
|
let expected = Value::String {
|
|
|
|
val: "1.0.0".to_string(),
|
2021-12-19 08:46:13 +01:00
|
|
|
span: Span::test_data(),
|
2021-11-04 23:04:21 +01:00
|
|
|
};
|
|
|
|
let mut inc = Inc::new();
|
|
|
|
inc.for_semver(SemVerAction::Major);
|
2021-12-19 08:46:13 +01:00
|
|
|
assert_eq!(inc.apply("0.1.3", Span::test_data()), expected)
|
2019-12-29 06:17:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-12 11:13:14 +01:00
|
|
|
fn minor() {
|
2021-11-04 23:04:21 +01:00
|
|
|
let expected = Value::String {
|
|
|
|
val: "0.2.0".to_string(),
|
2021-12-19 08:46:13 +01:00
|
|
|
span: Span::test_data(),
|
2021-11-04 23:04:21 +01:00
|
|
|
};
|
|
|
|
let mut inc = Inc::new();
|
|
|
|
inc.for_semver(SemVerAction::Minor);
|
2021-12-19 08:46:13 +01:00
|
|
|
assert_eq!(inc.apply("0.1.3", Span::test_data()), expected)
|
2019-12-29 06:17:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-12 11:13:14 +01:00
|
|
|
fn patch() {
|
2021-11-04 23:04:21 +01:00
|
|
|
let expected = Value::String {
|
|
|
|
val: "0.1.4".to_string(),
|
2021-12-19 08:46:13 +01:00
|
|
|
span: Span::test_data(),
|
2021-11-04 23:04:21 +01:00
|
|
|
};
|
|
|
|
let mut inc = Inc::new();
|
|
|
|
inc.for_semver(SemVerAction::Patch);
|
2021-12-19 08:46:13 +01:00
|
|
|
assert_eq!(inc.apply("0.1.3", Span::test_data()), expected)
|
2019-12-29 06:17:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|