nushell/crates/nu_plugin_inc/src/inc.rs
Ian Manske 1c49ca503a
Name the Value conversion functions more clearly (#11851)
# Description
This PR renames the conversion functions on `Value` to be more consistent.
It follows the Rust [API guidelines](https://rust-lang.github.io/api-guidelines/naming.html#ad-hoc-conversions-follow-as_-to_-into_-conventions-c-conv) for ad-hoc conversions.
The conversion functions on `Value` now come in a few forms:
- `coerce_{type}` takes a `&Value` and attempts to convert the value to
`type` (e.g., `i64` are converted to `f64`). This is the old behavior of
some of the `as_{type}` functions -- these functions have simply been
renamed to better reflect what they do.
- The new `as_{type}` functions take a `&Value` and returns an `Ok`
result only if the value is of `type` (no conversion is attempted). The
returned value will be borrowed if `type` is non-`Copy`, otherwise an
owned value is returned.
- `into_{type}` exists for non-`Copy` types, but otherwise does not
attempt conversion just like `as_type`. It takes an owned `Value` and
always returns an owned result.
- `coerce_into_{type}` has the same relationship with `coerce_{type}` as
`into_{type}` does with `as_{type}`.
- `to_{kind}_string`: conversion to different string formats (debug,
abbreviated, etc.). Only two of the old string conversion functions were
removed, the rest have been renamed only.
- `to_{type}`: other conversion functions. Currently, only `to_path`
exists. (And `to_string` through `Display`.)

This table summaries the above:
| Form | Cost | Input Ownership | Output Ownership | Converts `Value`
case/`type` |
| ---------------------------- | ----- | --------------- |
---------------- | -------- |
| `as_{type}` | Cheap | Borrowed | Borrowed/Owned | No |
| `into_{type}` | Cheap | Owned | Owned | No |
| `coerce_{type}` | Cheap | Borrowed | Borrowed/Owned | Yes |
| `coerce_into_{type}` | Cheap | Owned | Owned | Yes |
| `to_{kind}_string` | Expensive | Borrowed | Owned | Yes |
| `to_{type}` | Expensive | Borrowed | Owned | Yes |

# User-Facing Changes
Breaking API change for `Value` in `nu-protocol` which is exposed as
part of the plugin API.
2024-02-17 18:14:16 +00:00

171 lines
4.8 KiB
Rust

use nu_plugin::LabeledError;
use nu_protocol::{ast::CellPath, Span, Value};
use semver::{BuildMetadata, Prerelease, Version};
#[derive(Debug, Eq, PartialEq)]
pub enum Action {
SemVerAction(SemVerAction),
Default,
}
#[derive(Debug, Eq, PartialEq)]
pub enum SemVerAction {
Major,
Minor,
Patch,
}
#[derive(Default)]
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)
.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> {
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 {
label: "Unable to extract string".into(),
msg: format!("value cannot be converted to string {x:?} - {e}"),
span: Some(head),
})?;
Err(LabeledError {
label: "Incorrect value".into(),
msg,
span: Some(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)
}
}
}