mirror of
https://github.com/nushell/nushell.git
synced 2025-08-19 07:41:30 +02:00
new inc takes a path
This commit is contained in:
@@ -5,11 +5,56 @@ use nu::{
|
||||
};
|
||||
|
||||
struct Inc {
|
||||
inc_by: i64,
|
||||
field: Option<String>,
|
||||
}
|
||||
impl Inc {
|
||||
fn new() -> Inc {
|
||||
Inc { inc_by: 1 }
|
||||
Inc { field: None }
|
||||
}
|
||||
|
||||
fn inc(value: Spanned<Value>, field: &Option<String>) -> Result<Spanned<Value>, ShellError> {
|
||||
match value.item {
|
||||
Value::Primitive(Primitive::Int(i)) => Ok(Value::int(i + 1).spanned(value.span)),
|
||||
Value::Primitive(Primitive::Bytes(b)) => {
|
||||
Ok(Value::bytes(b + 1 as u64).spanned(value.span))
|
||||
}
|
||||
Value::Primitive(Primitive::String(s)) => {
|
||||
if let Ok(i) = s.parse::<u64>() {
|
||||
Ok(Spanned {
|
||||
item: Value::string(format!("{}", i + 1)),
|
||||
span: value.span,
|
||||
})
|
||||
} else {
|
||||
Err(ShellError::string("string could not be incremented"))
|
||||
}
|
||||
}
|
||||
Value::Object(_) => match field {
|
||||
Some(f) => {
|
||||
let replacement = match value.item.get_data_by_path(value.span, f) {
|
||||
Some(result) => Inc::inc(result.map(|x| x.clone()), &None)?,
|
||||
None => {
|
||||
return Err(ShellError::string("inc could not find field to replace"))
|
||||
}
|
||||
};
|
||||
match value
|
||||
.item
|
||||
.replace_data_at_path(value.span, f, replacement.item.clone())
|
||||
{
|
||||
Some(v) => return Ok(v),
|
||||
None => {
|
||||
return Err(ShellError::string("inc could not find field to replace"))
|
||||
}
|
||||
}
|
||||
}
|
||||
None => Err(ShellError::string(
|
||||
"inc needs a field when incrementing a value in an object",
|
||||
)),
|
||||
},
|
||||
x => Err(ShellError::string(format!(
|
||||
"Unrecognized type in stream: {:?}",
|
||||
x
|
||||
))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +62,7 @@ impl Plugin for Inc {
|
||||
fn config(&mut self) -> Result<CommandConfig, ShellError> {
|
||||
Ok(CommandConfig {
|
||||
name: "inc".to_string(),
|
||||
positional: vec![PositionalType::optional_any("Increment")],
|
||||
positional: vec![PositionalType::optional_any("Field")],
|
||||
is_filter: true,
|
||||
is_sink: false,
|
||||
named: IndexMap::new(),
|
||||
@@ -29,12 +74,17 @@ impl Plugin for Inc {
|
||||
for arg in args {
|
||||
match arg {
|
||||
Spanned {
|
||||
item: Value::Primitive(Primitive::Int(i)),
|
||||
item: Value::Primitive(Primitive::String(s)),
|
||||
..
|
||||
} => {
|
||||
self.inc_by = i;
|
||||
self.field = Some(s);
|
||||
}
|
||||
_ => {
|
||||
return Err(ShellError::string(format!(
|
||||
"Unrecognized type in params: {:?}",
|
||||
arg
|
||||
)))
|
||||
}
|
||||
_ => return Err(ShellError::string("Unrecognized type in params")),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -43,20 +93,7 @@ impl Plugin for Inc {
|
||||
}
|
||||
|
||||
fn filter(&mut self, input: Spanned<Value>) -> Result<Vec<ReturnValue>, ShellError> {
|
||||
let span = input.span;
|
||||
|
||||
match input.item {
|
||||
Value::Primitive(Primitive::Int(i)) => Ok(vec![ReturnSuccess::value(
|
||||
Value::int(i + self.inc_by).spanned(span),
|
||||
)]),
|
||||
Value::Primitive(Primitive::Bytes(b)) => Ok(vec![ReturnSuccess::value(
|
||||
Value::bytes(b + self.inc_by as u64).spanned(span),
|
||||
)]),
|
||||
x => Err(ShellError::string(format!(
|
||||
"Unrecognized type in stream: {:?}",
|
||||
x
|
||||
))),
|
||||
}
|
||||
Ok(vec![ReturnSuccess::value(Inc::inc(input, &self.field)?)])
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user