2021-12-05 04:11:19 +01:00
|
|
|
use nu_plugin::LabeledError;
|
2022-03-22 19:32:03 +01:00
|
|
|
use nu_protocol::{ast::CellPath, Span, Value};
|
2023-01-16 03:39:27 +01:00
|
|
|
use semver::{BuildMetadata, Prerelease, Version};
|
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>,
|
2022-03-22 19:32:03 +01:00
|
|
|
pub cell_path: Option<CellPath>,
|
2019-12-29 06:17:24 +01:00
|
|
|
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,
|
Reduced LOC by replacing several instances of `Value::Int {}`, `Value::Float{}`, `Value::Bool {}`, and `Value::String {}` with `Value::int()`, `Value::float()`, `Value::boolean()` and `Value::string()` (#7412)
# Description
While perusing Value.rs, I noticed the `Value::int()`, `Value::float()`,
`Value::boolean()` and `Value::string()` constructors, which seem
designed to make it easier to construct various Values, but which aren't
used often at all in the codebase. So, using a few find-replaces
regexes, I increased their usage. This reduces overall LOC because
structures like this:
```
Value::Int {
val: a,
span: head
}
```
are changed into
```
Value::int(a, head)
```
and are respected as such by the project's formatter.
There are little readability concerns because the second argument to all
of these is `span`, and it's almost always extremely obvious which is
the span at every callsite.
# User-Facing Changes
None.
# Tests + Formatting
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# After Submitting
If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2022-12-09 17:37:51 +01:00
|
|
|
Err(_) => return Value::string(input, head),
|
2019-12-29 06:17:24 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
match act_on {
|
2023-01-16 03:39:27 +01:00
|
|
|
SemVerAction::Major => Self::increment_major(&mut ver),
|
|
|
|
SemVerAction::Minor => Self::increment_minor(&mut ver),
|
|
|
|
SemVerAction::Patch => Self::increment_patch(&mut ver),
|
2019-12-29 06:17:24 +01:00
|
|
|
}
|
|
|
|
|
Reduced LOC by replacing several instances of `Value::Int {}`, `Value::Float{}`, `Value::Bool {}`, and `Value::String {}` with `Value::int()`, `Value::float()`, `Value::boolean()` and `Value::string()` (#7412)
# Description
While perusing Value.rs, I noticed the `Value::int()`, `Value::float()`,
`Value::boolean()` and `Value::string()` constructors, which seem
designed to make it easier to construct various Values, but which aren't
used often at all in the codebase. So, using a few find-replaces
regexes, I increased their usage. This reduces overall LOC because
structures like this:
```
Value::Int {
val: a,
span: head
}
```
are changed into
```
Value::int(a, head)
```
and are respected as such by the project's formatter.
There are little readability concerns because the second argument to all
of these is `span`, and it's almost always extremely obvious which is
the span at every callsite.
# User-Facing Changes
None.
# Tests + Formatting
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# After Submitting
If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2022-12-09 17:37:51 +01:00
|
|
|
Value::string(ver.to_string(), head)
|
2021-11-04 23:04:21 +01:00
|
|
|
}
|
2023-01-24 12:23:42 +01:00
|
|
|
Some(Action::Default) | None => {
|
|
|
|
if let Ok(v) = input.parse::<u64>() {
|
|
|
|
Value::string((v + 1).to_string(), head)
|
|
|
|
} else {
|
|
|
|
Value::string(input, head)
|
|
|
|
}
|
|
|
|
}
|
2021-02-12 11:13:14 +01:00
|
|
|
}
|
2019-12-29 06:17:24 +01:00
|
|
|
}
|
|
|
|
|
2023-01-16 03:39:27 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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> {
|
2022-03-22 19:32:03 +01:00
|
|
|
if let Some(cell_path) = &self.cell_path {
|
|
|
|
let working_value = value.clone();
|
2023-03-16 04:50:58 +01:00
|
|
|
let cell_value = working_value.follow_cell_path(&cell_path.members, false)?;
|
2022-03-22 19:32:03 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
.map_err(|x| {
|
|
|
|
let error: LabeledError = x.into();
|
|
|
|
error
|
|
|
|
})?;
|
|
|
|
Ok(value)
|
|
|
|
} else {
|
|
|
|
self.inc_value(head, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn inc_value(&self, head: Span, value: &Value) -> Result<Value, LabeledError> {
|
2021-11-04 23:04:21 +01:00
|
|
|
match value {
|
Reduced LOC by replacing several instances of `Value::Int {}`, `Value::Float{}`, `Value::Bool {}`, and `Value::String {}` with `Value::int()`, `Value::float()`, `Value::boolean()` and `Value::string()` (#7412)
# Description
While perusing Value.rs, I noticed the `Value::int()`, `Value::float()`,
`Value::boolean()` and `Value::string()` constructors, which seem
designed to make it easier to construct various Values, but which aren't
used often at all in the codebase. So, using a few find-replaces
regexes, I increased their usage. This reduces overall LOC because
structures like this:
```
Value::Int {
val: a,
span: head
}
```
are changed into
```
Value::int(a, head)
```
and are respected as such by the project's formatter.
There are little readability concerns because the second argument to all
of these is `span`, and it's almost always extremely obvious which is
the span at every callsite.
# User-Facing Changes
None.
# Tests + Formatting
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# After Submitting
If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2022-12-09 17:37:51 +01:00
|
|
|
Value::Int { val, span } => Ok(Value::int(val + 1, *span)),
|
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(),
|
2023-01-30 02:37:54 +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() {
|
2022-12-24 10:25:38 +01:00
|
|
|
let expected = Value::test_string("1.0.0");
|
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() {
|
2022-12-24 10:25:38 +01:00
|
|
|
let expected = Value::test_string("0.2.0");
|
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() {
|
2022-12-24 10:25:38 +01:00
|
|
|
let expected = Value::test_string("0.1.4");
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|