mirror of
https://github.com/nushell/nushell.git
synced 2025-08-17 01:11:16 +02:00
Move the remainder of the plugins to crates
This commit is contained in:
99
src/cli.rs
99
src/cli.rs
@ -231,22 +231,75 @@ pub async fn cli() -> Result<(), Box<dyn Error>> {
|
||||
use crate::commands::*;
|
||||
|
||||
context.add_commands(vec![
|
||||
// System/file operations
|
||||
whole_stream_command(PWD),
|
||||
whole_stream_command(LS),
|
||||
whole_stream_command(CD),
|
||||
whole_stream_command(Env),
|
||||
per_item_command(Remove),
|
||||
per_item_command(Open),
|
||||
whole_stream_command(Config),
|
||||
per_item_command(Help),
|
||||
per_item_command(History),
|
||||
whole_stream_command(Save),
|
||||
per_item_command(Cpy),
|
||||
whole_stream_command(Date),
|
||||
per_item_command(Mkdir),
|
||||
per_item_command(Move),
|
||||
whole_stream_command(Version),
|
||||
whole_stream_command(What),
|
||||
whole_stream_command(Which),
|
||||
whole_stream_command(Debug),
|
||||
// Statistics
|
||||
whole_stream_command(Size),
|
||||
whole_stream_command(Nth),
|
||||
whole_stream_command(Count),
|
||||
// Metadata
|
||||
whole_stream_command(Tags),
|
||||
// Shells
|
||||
whole_stream_command(Next),
|
||||
whole_stream_command(Previous),
|
||||
whole_stream_command(Shells),
|
||||
per_item_command(Enter),
|
||||
whole_stream_command(Exit),
|
||||
// Viewers
|
||||
whole_stream_command(Autoview),
|
||||
whole_stream_command(Table),
|
||||
// Text manipulation
|
||||
whole_stream_command(SplitColumn),
|
||||
whole_stream_command(SplitRow),
|
||||
whole_stream_command(Lines),
|
||||
whole_stream_command(Trim),
|
||||
per_item_command(Echo),
|
||||
per_item_command(Parse),
|
||||
// Column manipulation
|
||||
whole_stream_command(Reject),
|
||||
whole_stream_command(Pick),
|
||||
whole_stream_command(Get),
|
||||
per_item_command(Edit),
|
||||
per_item_command(Insert),
|
||||
whole_stream_command(SplitBy),
|
||||
// Row manipulation
|
||||
whole_stream_command(Reverse),
|
||||
whole_stream_command(Append),
|
||||
whole_stream_command(Prepend),
|
||||
whole_stream_command(Trim),
|
||||
whole_stream_command(SortBy),
|
||||
whole_stream_command(GroupBy),
|
||||
whole_stream_command(First),
|
||||
whole_stream_command(Last),
|
||||
whole_stream_command(Skip),
|
||||
whole_stream_command(Nth),
|
||||
per_item_command(Format),
|
||||
per_item_command(Where),
|
||||
whole_stream_command(Compact),
|
||||
whole_stream_command(Default),
|
||||
whole_stream_command(SkipWhile),
|
||||
whole_stream_command(Range),
|
||||
// Table manipulation
|
||||
whole_stream_command(Wrap),
|
||||
whole_stream_command(Pivot),
|
||||
// Data processing
|
||||
whole_stream_command(Histogram),
|
||||
// File format output
|
||||
whole_stream_command(ToBSON),
|
||||
whole_stream_command(ToCSV),
|
||||
whole_stream_command(ToJSON),
|
||||
@ -256,13 +309,7 @@ pub async fn cli() -> Result<(), Box<dyn Error>> {
|
||||
whole_stream_command(ToTSV),
|
||||
whole_stream_command(ToURL),
|
||||
whole_stream_command(ToYAML),
|
||||
whole_stream_command(SortBy),
|
||||
whole_stream_command(GroupBy),
|
||||
whole_stream_command(Tags),
|
||||
whole_stream_command(Count),
|
||||
whole_stream_command(First),
|
||||
whole_stream_command(Last),
|
||||
whole_stream_command(Env),
|
||||
// File format input
|
||||
whole_stream_command(FromCSV),
|
||||
whole_stream_command(FromTSV),
|
||||
whole_stream_command(FromSSV),
|
||||
@ -277,40 +324,6 @@ pub async fn cli() -> Result<(), Box<dyn Error>> {
|
||||
whole_stream_command(FromXML),
|
||||
whole_stream_command(FromYAML),
|
||||
whole_stream_command(FromYML),
|
||||
whole_stream_command(Pick),
|
||||
whole_stream_command(Get),
|
||||
whole_stream_command(Histogram),
|
||||
per_item_command(Remove),
|
||||
per_item_command(Open),
|
||||
per_item_command(Where),
|
||||
per_item_command(Echo),
|
||||
per_item_command(Edit),
|
||||
per_item_command(Insert),
|
||||
per_item_command(Format),
|
||||
per_item_command(Parse),
|
||||
whole_stream_command(Config),
|
||||
whole_stream_command(Compact),
|
||||
whole_stream_command(Default),
|
||||
whole_stream_command(SkipWhile),
|
||||
per_item_command(Enter),
|
||||
per_item_command(Help),
|
||||
per_item_command(History),
|
||||
whole_stream_command(Exit),
|
||||
whole_stream_command(Autoview),
|
||||
whole_stream_command(Pivot),
|
||||
per_item_command(Cpy),
|
||||
whole_stream_command(Date),
|
||||
per_item_command(Mkdir),
|
||||
per_item_command(Move),
|
||||
whole_stream_command(Save),
|
||||
whole_stream_command(SplitBy),
|
||||
whole_stream_command(Table),
|
||||
whole_stream_command(Version),
|
||||
whole_stream_command(What),
|
||||
whole_stream_command(Which),
|
||||
whole_stream_command(Debug),
|
||||
whole_stream_command(Range),
|
||||
whole_stream_command(Wrap),
|
||||
]);
|
||||
|
||||
cfg_if::cfg_if! {
|
||||
|
@ -71,6 +71,7 @@ pub(crate) mod rm;
|
||||
pub(crate) mod save;
|
||||
pub(crate) mod shells;
|
||||
pub(crate) mod size;
|
||||
pub(crate) mod skip;
|
||||
pub(crate) mod skip_while;
|
||||
pub(crate) mod sort_by;
|
||||
pub(crate) mod split_by;
|
||||
@ -164,6 +165,7 @@ pub(crate) use rm::Remove;
|
||||
pub(crate) use save::Save;
|
||||
pub(crate) use shells::Shells;
|
||||
pub(crate) use size::Size;
|
||||
pub(crate) use skip::Skip;
|
||||
pub(crate) use skip_while::SkipWhile;
|
||||
pub(crate) use sort_by::SortBy;
|
||||
pub(crate) use split_by::SplitBy;
|
||||
|
@ -2,12 +2,12 @@ use crate::commands::WholeStreamCommand;
|
||||
use crate::data::base::property_get::get_data_by_column_path;
|
||||
use crate::data::base::shape::Shapes;
|
||||
use crate::prelude::*;
|
||||
use crate::utils::did_you_mean;
|
||||
use futures_util::pin_mut;
|
||||
use log::trace;
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{
|
||||
ColumnPath, ReturnSuccess, ReturnValue, Signature, SyntaxShape, UntaggedValue, Value,
|
||||
did_you_mean, ColumnPath, ReturnSuccess, ReturnValue, Signature, SyntaxShape, UntaggedValue,
|
||||
Value,
|
||||
};
|
||||
use nu_source::{span_for_spanned_list, PrettyDebug};
|
||||
|
||||
|
47
src/commands/skip.rs
Normal file
47
src/commands/skip.rs
Normal file
@ -0,0 +1,47 @@
|
||||
use crate::commands::WholeStreamCommand;
|
||||
use crate::context::CommandRegistry;
|
||||
use crate::prelude::*;
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{Signature, SyntaxShape};
|
||||
use nu_source::Tagged;
|
||||
|
||||
pub struct Skip;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct SkipArgs {
|
||||
rows: Option<Tagged<u64>>,
|
||||
}
|
||||
|
||||
impl WholeStreamCommand for Skip {
|
||||
fn name(&self) -> &str {
|
||||
"skip"
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build("skip").optional("rows", SyntaxShape::Int, "how many rows to skip")
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"Skip some number of rows."
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
args: CommandArgs,
|
||||
registry: &CommandRegistry,
|
||||
) -> Result<OutputStream, ShellError> {
|
||||
args.process(registry, skip)?.run()
|
||||
}
|
||||
}
|
||||
|
||||
fn skip(SkipArgs { rows }: SkipArgs, context: RunnableContext) -> Result<OutputStream, ShellError> {
|
||||
let rows_desired = if let Some(quantity) = rows {
|
||||
*quantity
|
||||
} else {
|
||||
1
|
||||
};
|
||||
|
||||
Ok(OutputStream::from_input(
|
||||
context.input.values.skip(rows_desired),
|
||||
))
|
||||
}
|
@ -1,4 +1,3 @@
|
||||
use crate::data::primitive::format_primitive;
|
||||
use crate::prelude::*;
|
||||
use chrono::{DateTime, Utc};
|
||||
use chrono_humanize::Humanize;
|
||||
@ -6,8 +5,8 @@ use derive_new::new;
|
||||
use indexmap::IndexMap;
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{
|
||||
ColumnPath, Dictionary, Evaluate, Primitive, ShellTypeName, TaggedDictBuilder, UntaggedValue,
|
||||
Value,
|
||||
format_primitive, ColumnPath, Dictionary, Evaluate, Primitive, ShellTypeName,
|
||||
TaggedDictBuilder, UntaggedValue, Value,
|
||||
};
|
||||
use nu_source::{b, DebugDoc, PrettyDebug};
|
||||
use std::collections::BTreeMap;
|
||||
|
@ -1,7 +1,5 @@
|
||||
use chrono_humanize::Humanize;
|
||||
use nu_parser::Number;
|
||||
use nu_protocol::Primitive;
|
||||
use nu_source::PrettyDebug;
|
||||
|
||||
pub fn number(number: impl Into<Number>) -> Primitive {
|
||||
let number = number.into();
|
||||
@ -12,64 +10,6 @@ pub fn number(number: impl Into<Number>) -> Primitive {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn format_primitive(primitive: &Primitive, field_name: Option<&String>) -> String {
|
||||
match primitive {
|
||||
Primitive::Nothing => String::new(),
|
||||
Primitive::BeginningOfStream => String::new(),
|
||||
Primitive::EndOfStream => String::new(),
|
||||
Primitive::Path(p) => format!("{}", p.display()),
|
||||
Primitive::Bytes(b) => {
|
||||
let byte = byte_unit::Byte::from_bytes(*b as u128);
|
||||
|
||||
if byte.get_bytes() == 0u128 {
|
||||
return "—".to_string();
|
||||
}
|
||||
|
||||
let byte = byte.get_appropriate_unit(false);
|
||||
|
||||
match byte.get_unit() {
|
||||
byte_unit::ByteUnit::B => format!("{} B ", byte.get_value()),
|
||||
_ => byte.format(1).to_string(),
|
||||
}
|
||||
}
|
||||
Primitive::Duration(sec) => format_duration(*sec),
|
||||
Primitive::Int(i) => i.to_string(),
|
||||
Primitive::Decimal(decimal) => decimal.to_string(),
|
||||
Primitive::Pattern(s) => s.to_string(),
|
||||
Primitive::String(s) => s.to_owned(),
|
||||
Primitive::Line(s) => s.to_owned(),
|
||||
Primitive::ColumnPath(p) => {
|
||||
let mut members = p.iter();
|
||||
let mut f = String::new();
|
||||
|
||||
f.push_str(
|
||||
&members
|
||||
.next()
|
||||
.expect("BUG: column path with zero members")
|
||||
.display(),
|
||||
);
|
||||
|
||||
for member in members {
|
||||
f.push_str(".");
|
||||
f.push_str(&member.display())
|
||||
}
|
||||
|
||||
f
|
||||
}
|
||||
Primitive::Boolean(b) => match (b, field_name) {
|
||||
(true, None) => "Yes",
|
||||
(false, None) => "No",
|
||||
(true, Some(s)) if !s.is_empty() => s,
|
||||
(false, Some(s)) if !s.is_empty() => "",
|
||||
(true, Some(_)) => "Yes",
|
||||
(false, Some(_)) => "No",
|
||||
}
|
||||
.to_owned(),
|
||||
Primitive::Binary(_) => "<binary>".to_owned(),
|
||||
Primitive::Date(d) => d.humanize().to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn style_primitive(primitive: &Primitive) -> &'static str {
|
||||
match primitive {
|
||||
Primitive::Bytes(0) => "c", // centre 'missing' indicator
|
||||
@ -77,17 +17,3 @@ pub fn style_primitive(primitive: &Primitive) -> &'static str {
|
||||
_ => "",
|
||||
}
|
||||
}
|
||||
|
||||
fn format_duration(sec: u64) -> String {
|
||||
let (minutes, seconds) = (sec / 60, sec % 60);
|
||||
let (hours, minutes) = (minutes / 60, minutes % 60);
|
||||
let (days, hours) = (hours / 24, hours % 24);
|
||||
|
||||
match (days, hours, minutes, seconds) {
|
||||
(0, 0, 0, 1) => "1 sec".to_owned(),
|
||||
(0, 0, 0, s) => format!("{} secs", s),
|
||||
(0, 0, m, s) => format!("{}:{:02}", m, s),
|
||||
(0, h, m, s) => format!("{}:{:02}:{:02}", h, m, s),
|
||||
(d, h, m, s) => format!("{}:{:02}:{:02}:{:02}", d, h, m, s),
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,9 @@
|
||||
use crate::data::primitive::format_primitive;
|
||||
use crate::data::value::format_leaf;
|
||||
use crate::format::{EntriesView, RenderView, TableView};
|
||||
use crate::prelude::*;
|
||||
use derive_new::new;
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{UntaggedValue, Value};
|
||||
use nu_protocol::{format_primitive, UntaggedValue, Value};
|
||||
|
||||
// A list is printed one line at a time with an optional separator between groups
|
||||
#[derive(new)]
|
||||
|
@ -27,9 +27,9 @@ pub use crate::data::dict::TaggedListBuilder;
|
||||
pub use crate::data::primitive;
|
||||
pub use crate::data::value;
|
||||
pub use crate::env::host::BasicHost;
|
||||
pub use crate::utils::{did_you_mean, AbsoluteFile, AbsolutePath, RelativePath};
|
||||
pub use crate::utils::{AbsoluteFile, AbsolutePath, RelativePath};
|
||||
pub use nu_parser::TokenTreeBuilder;
|
||||
pub use num_traits::cast::ToPrimitive;
|
||||
|
||||
// TODO: Temporary redirect
|
||||
pub use nu_protocol::{serve_plugin, Plugin, TaggedDictBuilder};
|
||||
pub use nu_protocol::{did_you_mean, serve_plugin, Plugin, TaggedDictBuilder};
|
||||
|
@ -1,117 +0,0 @@
|
||||
use nu::{serve_plugin, Plugin};
|
||||
use nu_errors::{CoerceInto, ShellError};
|
||||
use nu_protocol::{
|
||||
CallInfo, Primitive, ReturnSuccess, ReturnValue, Signature, UntaggedValue, Value,
|
||||
};
|
||||
use nu_source::TaggedItem;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Average {
|
||||
total: Option<Value>,
|
||||
count: u64,
|
||||
}
|
||||
|
||||
impl Average {
|
||||
fn new() -> Average {
|
||||
Average {
|
||||
total: None,
|
||||
count: 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn average(&mut self, value: Value) -> Result<(), ShellError> {
|
||||
match &value.value {
|
||||
UntaggedValue::Primitive(Primitive::Nothing) => Ok(()),
|
||||
UntaggedValue::Primitive(Primitive::Int(i)) => match &self.total {
|
||||
Some(Value {
|
||||
value: UntaggedValue::Primitive(Primitive::Int(j)),
|
||||
tag,
|
||||
}) => {
|
||||
self.total = Some(UntaggedValue::int(i + j).into_value(tag));
|
||||
self.count += 1;
|
||||
Ok(())
|
||||
}
|
||||
None => {
|
||||
self.total = Some(value.clone());
|
||||
self.count += 1;
|
||||
Ok(())
|
||||
}
|
||||
_ => Err(ShellError::labeled_error(
|
||||
"Could calculate average of non-integer or unrelated types",
|
||||
"source",
|
||||
value.tag,
|
||||
)),
|
||||
},
|
||||
UntaggedValue::Primitive(Primitive::Bytes(b)) => match &self.total {
|
||||
Some(Value {
|
||||
value: UntaggedValue::Primitive(Primitive::Bytes(j)),
|
||||
tag,
|
||||
}) => {
|
||||
self.total = Some(UntaggedValue::bytes(b + j).into_value(tag));
|
||||
self.count += 1;
|
||||
Ok(())
|
||||
}
|
||||
None => {
|
||||
self.total = Some(value);
|
||||
self.count += 1;
|
||||
Ok(())
|
||||
}
|
||||
_ => Err(ShellError::labeled_error(
|
||||
"Could calculate average of non-integer or unrelated types",
|
||||
"source",
|
||||
value.tag,
|
||||
)),
|
||||
},
|
||||
x => Err(ShellError::labeled_error(
|
||||
format!("Unrecognized type in stream: {:?}", x),
|
||||
"source",
|
||||
value.tag,
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Plugin for Average {
|
||||
fn config(&mut self) -> Result<Signature, ShellError> {
|
||||
Ok(Signature::build("average")
|
||||
.desc("Compute the average of a column of numerical values.")
|
||||
.filter())
|
||||
}
|
||||
|
||||
fn begin_filter(&mut self, _: CallInfo) -> Result<Vec<ReturnValue>, ShellError> {
|
||||
Ok(vec![])
|
||||
}
|
||||
|
||||
fn filter(&mut self, input: Value) -> Result<Vec<ReturnValue>, ShellError> {
|
||||
self.average(input)?;
|
||||
Ok(vec![])
|
||||
}
|
||||
|
||||
fn end_filter(&mut self) -> Result<Vec<ReturnValue>, ShellError> {
|
||||
match self.total {
|
||||
None => Ok(vec![]),
|
||||
Some(ref inner) => match &inner.value {
|
||||
UntaggedValue::Primitive(Primitive::Int(i)) => {
|
||||
let total: u64 = i
|
||||
.tagged(inner.tag.clone())
|
||||
.coerce_into("converting for average")?;
|
||||
let avg = total as f64 / self.count as f64;
|
||||
let primitive_value: UntaggedValue = Primitive::from(avg).into();
|
||||
let value = primitive_value.into_value(inner.tag.clone());
|
||||
Ok(vec![ReturnSuccess::value(value)])
|
||||
}
|
||||
UntaggedValue::Primitive(Primitive::Bytes(bytes)) => {
|
||||
let avg = *bytes as f64 / self.count as f64;
|
||||
let primitive_value: UntaggedValue = Primitive::from(avg).into();
|
||||
let tagged_value = primitive_value.into_value(inner.tag.clone());
|
||||
Ok(vec![ReturnSuccess::value(tagged_value)])
|
||||
}
|
||||
_ => Ok(vec![]),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
serve_plugin(&mut Average::new());
|
||||
}
|
@ -1,456 +0,0 @@
|
||||
use nu::{did_you_mean, serve_plugin, Plugin, ValueExt};
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{
|
||||
CallInfo, ColumnPath, Primitive, ReturnSuccess, ReturnValue, ShellTypeName, Signature,
|
||||
SyntaxShape, UntaggedValue, Value,
|
||||
};
|
||||
use nu_source::{span_for_spanned_list, HasSpan, SpannedItem, Tagged};
|
||||
|
||||
enum Action {
|
||||
SemVerAction(SemVerAction),
|
||||
Default,
|
||||
}
|
||||
|
||||
pub enum SemVerAction {
|
||||
Major,
|
||||
Minor,
|
||||
Patch,
|
||||
}
|
||||
|
||||
struct Inc {
|
||||
field: Option<Tagged<ColumnPath>>,
|
||||
error: Option<String>,
|
||||
action: Option<Action>,
|
||||
}
|
||||
|
||||
impl Inc {
|
||||
fn new() -> Inc {
|
||||
Inc {
|
||||
field: None,
|
||||
error: None,
|
||||
action: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn apply(&self, input: &str) -> Result<UntaggedValue, ShellError> {
|
||||
let applied = match &self.action {
|
||||
Some(Action::SemVerAction(act_on)) => {
|
||||
let mut ver = match semver::Version::parse(&input) {
|
||||
Ok(parsed_ver) => parsed_ver,
|
||||
Err(_) => return Ok(UntaggedValue::string(input.to_string())),
|
||||
};
|
||||
|
||||
match act_on {
|
||||
SemVerAction::Major => ver.increment_major(),
|
||||
SemVerAction::Minor => ver.increment_minor(),
|
||||
SemVerAction::Patch => ver.increment_patch(),
|
||||
}
|
||||
|
||||
UntaggedValue::string(ver.to_string())
|
||||
}
|
||||
Some(Action::Default) | None => match input.parse::<u64>() {
|
||||
Ok(v) => UntaggedValue::string(format!("{}", v + 1)),
|
||||
Err(_) => UntaggedValue::string(input),
|
||||
},
|
||||
};
|
||||
|
||||
Ok(applied)
|
||||
}
|
||||
|
||||
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]"
|
||||
}
|
||||
|
||||
fn inc(&self, value: Value) -> Result<Value, ShellError> {
|
||||
match &value.value {
|
||||
UntaggedValue::Primitive(Primitive::Int(i)) => {
|
||||
Ok(UntaggedValue::int(i + 1).into_value(value.tag()))
|
||||
}
|
||||
UntaggedValue::Primitive(Primitive::Bytes(b)) => {
|
||||
Ok(UntaggedValue::bytes(b + 1 as u64).into_value(value.tag()))
|
||||
}
|
||||
UntaggedValue::Primitive(Primitive::String(ref s)) => {
|
||||
Ok(self.apply(&s)?.into_value(value.tag()))
|
||||
}
|
||||
UntaggedValue::Table(values) => {
|
||||
if values.len() == 1 {
|
||||
Ok(UntaggedValue::Table(vec![self.inc(values[0].clone())?])
|
||||
.into_value(value.tag()))
|
||||
} else {
|
||||
Err(ShellError::type_error(
|
||||
"incrementable value",
|
||||
value.type_name().spanned(value.span()),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
UntaggedValue::Row(_) => match self.field {
|
||||
Some(ref f) => {
|
||||
let fields = f.clone();
|
||||
|
||||
let replace_for = value.get_data_by_column_path(
|
||||
&f,
|
||||
Box::new(move |(obj_source, column_path_tried, _)| {
|
||||
match did_you_mean(&obj_source, &column_path_tried) {
|
||||
Some(suggestions) => ShellError::labeled_error(
|
||||
"Unknown column",
|
||||
format!("did you mean '{}'?", suggestions[0].1),
|
||||
span_for_spanned_list(fields.iter().map(|p| p.span)),
|
||||
),
|
||||
None => ShellError::labeled_error(
|
||||
"Unknown column",
|
||||
"row does not contain this column",
|
||||
span_for_spanned_list(fields.iter().map(|p| p.span)),
|
||||
),
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
let got = replace_for?;
|
||||
let replacement = self.inc(got.clone())?;
|
||||
|
||||
match value.replace_data_at_column_path(
|
||||
&f,
|
||||
replacement.value.clone().into_untagged_value(),
|
||||
) {
|
||||
Some(v) => Ok(v),
|
||||
None => Err(ShellError::labeled_error(
|
||||
"inc could not find field to replace",
|
||||
"column name",
|
||||
value.tag(),
|
||||
)),
|
||||
}
|
||||
}
|
||||
None => Err(ShellError::untagged_runtime_error(
|
||||
"inc needs a field when incrementing a column in a table",
|
||||
)),
|
||||
},
|
||||
_ => Err(ShellError::type_error(
|
||||
"incrementable value",
|
||||
value.type_name().spanned(value.span()),
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Plugin for Inc {
|
||||
fn config(&mut self) -> Result<Signature, ShellError> {
|
||||
Ok(Signature::build("inc")
|
||||
.desc("Increment a value or version. Optionally use the column of a table.")
|
||||
.switch("major", "increment the major version (eg 1.2.1 -> 2.0.0)")
|
||||
.switch("minor", "increment the minor version (eg 1.2.1 -> 1.3.0)")
|
||||
.switch("patch", "increment the patch version (eg 1.2.1 -> 1.2.2)")
|
||||
.rest(SyntaxShape::ColumnPath, "the column(s) to update")
|
||||
.filter())
|
||||
}
|
||||
|
||||
fn begin_filter(&mut self, call_info: CallInfo) -> Result<Vec<ReturnValue>, ShellError> {
|
||||
if call_info.args.has("major") {
|
||||
self.for_semver(SemVerAction::Major);
|
||||
}
|
||||
if call_info.args.has("minor") {
|
||||
self.for_semver(SemVerAction::Minor);
|
||||
}
|
||||
if call_info.args.has("patch") {
|
||||
self.for_semver(SemVerAction::Patch);
|
||||
}
|
||||
|
||||
if let Some(args) = call_info.args.positional {
|
||||
for arg in args {
|
||||
match arg {
|
||||
table @ Value {
|
||||
value: UntaggedValue::Primitive(Primitive::ColumnPath(_)),
|
||||
..
|
||||
} => {
|
||||
self.field = Some(table.as_column_path()?);
|
||||
}
|
||||
value => {
|
||||
return Err(ShellError::type_error(
|
||||
"table",
|
||||
value.type_name().spanned(value.span()),
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if self.action.is_none() {
|
||||
self.action = Some(Action::Default);
|
||||
}
|
||||
|
||||
match &self.error {
|
||||
Some(reason) => Err(ShellError::untagged_runtime_error(format!(
|
||||
"{}: {}",
|
||||
reason,
|
||||
Inc::usage()
|
||||
))),
|
||||
None => Ok(vec![]),
|
||||
}
|
||||
}
|
||||
|
||||
fn filter(&mut self, input: Value) -> Result<Vec<ReturnValue>, ShellError> {
|
||||
Ok(vec![ReturnSuccess::value(self.inc(input)?)])
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
serve_plugin(&mut Inc::new());
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
use super::{Inc, SemVerAction};
|
||||
use indexmap::IndexMap;
|
||||
use nu::{Plugin, TaggedDictBuilder};
|
||||
use nu_protocol::{
|
||||
CallInfo, EvaluatedArgs, PathMember, ReturnSuccess, UnspannedPathMember, UntaggedValue,
|
||||
Value,
|
||||
};
|
||||
use nu_source::{Span, Tag};
|
||||
|
||||
struct CallStub {
|
||||
positionals: Vec<Value>,
|
||||
flags: IndexMap<String, Value>,
|
||||
}
|
||||
|
||||
impl CallStub {
|
||||
fn new() -> CallStub {
|
||||
CallStub {
|
||||
positionals: vec![],
|
||||
flags: indexmap::IndexMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn with_long_flag(&mut self, name: &str) -> &mut Self {
|
||||
self.flags.insert(
|
||||
name.to_string(),
|
||||
UntaggedValue::boolean(true).into_value(Tag::unknown()),
|
||||
);
|
||||
self
|
||||
}
|
||||
|
||||
fn with_parameter(&mut self, name: &str) -> &mut Self {
|
||||
let fields: Vec<PathMember> = name
|
||||
.split(".")
|
||||
.map(|s| {
|
||||
UnspannedPathMember::String(s.to_string()).into_path_member(Span::unknown())
|
||||
})
|
||||
.collect();
|
||||
|
||||
self.positionals
|
||||
.push(UntaggedValue::column_path(fields).into_untagged_value());
|
||||
self
|
||||
}
|
||||
|
||||
fn create(&self) -> CallInfo {
|
||||
CallInfo {
|
||||
args: EvaluatedArgs::new(Some(self.positionals.clone()), Some(self.flags.clone())),
|
||||
name_tag: Tag::unknown(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn cargo_sample_record(with_version: &str) -> Value {
|
||||
let mut package = TaggedDictBuilder::new(Tag::unknown());
|
||||
package.insert_untagged("version", UntaggedValue::string(with_version));
|
||||
package.into_value()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inc_plugin_configuration_flags_wired() {
|
||||
let mut plugin = Inc::new();
|
||||
|
||||
let configured = plugin.config().expect("Can not configure plugin");
|
||||
|
||||
for action_flag in &["major", "minor", "patch"] {
|
||||
assert!(configured.named.get(*action_flag).is_some());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inc_plugin_accepts_major() {
|
||||
let mut plugin = Inc::new();
|
||||
|
||||
assert!(plugin
|
||||
.begin_filter(CallStub::new().with_long_flag("major").create())
|
||||
.is_ok());
|
||||
assert!(plugin.action.is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inc_plugin_accepts_minor() {
|
||||
let mut plugin = Inc::new();
|
||||
|
||||
assert!(plugin
|
||||
.begin_filter(CallStub::new().with_long_flag("minor").create())
|
||||
.is_ok());
|
||||
assert!(plugin.action.is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inc_plugin_accepts_patch() {
|
||||
let mut plugin = Inc::new();
|
||||
|
||||
assert!(plugin
|
||||
.begin_filter(CallStub::new().with_long_flag("patch").create())
|
||||
.is_ok());
|
||||
assert!(plugin.action.is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inc_plugin_accepts_only_one_action() {
|
||||
let mut plugin = Inc::new();
|
||||
|
||||
assert!(plugin
|
||||
.begin_filter(
|
||||
CallStub::new()
|
||||
.with_long_flag("major")
|
||||
.with_long_flag("minor")
|
||||
.create(),
|
||||
)
|
||||
.is_err());
|
||||
assert_eq!(plugin.error, Some("can only apply one".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inc_plugin_accepts_field() {
|
||||
let mut plugin = Inc::new();
|
||||
|
||||
assert!(plugin
|
||||
.begin_filter(CallStub::new().with_parameter("package.version").create())
|
||||
.is_ok());
|
||||
|
||||
assert_eq!(
|
||||
plugin
|
||||
.field
|
||||
.map(|f| f.iter().map(|f| f.unspanned.clone()).collect()),
|
||||
Some(vec![
|
||||
UnspannedPathMember::String("package".to_string()),
|
||||
UnspannedPathMember::String("version".to_string())
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn incs_major() {
|
||||
let mut inc = Inc::new();
|
||||
inc.for_semver(SemVerAction::Major);
|
||||
assert_eq!(inc.apply("0.1.3").unwrap(), UntaggedValue::string("1.0.0"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn incs_minor() {
|
||||
let mut inc = Inc::new();
|
||||
inc.for_semver(SemVerAction::Minor);
|
||||
assert_eq!(inc.apply("0.1.3").unwrap(), UntaggedValue::string("0.2.0"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn incs_patch() {
|
||||
let mut inc = Inc::new();
|
||||
inc.for_semver(SemVerAction::Patch);
|
||||
assert_eq!(inc.apply("0.1.3").unwrap(), UntaggedValue::string("0.1.4"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inc_plugin_applies_major() {
|
||||
let mut plugin = Inc::new();
|
||||
|
||||
assert!(plugin
|
||||
.begin_filter(
|
||||
CallStub::new()
|
||||
.with_long_flag("major")
|
||||
.with_parameter("version")
|
||||
.create()
|
||||
)
|
||||
.is_ok());
|
||||
|
||||
let subject = cargo_sample_record("0.1.3");
|
||||
let output = plugin.filter(subject).unwrap();
|
||||
|
||||
match output[0].as_ref().unwrap() {
|
||||
ReturnSuccess::Value(Value {
|
||||
value: UntaggedValue::Row(o),
|
||||
..
|
||||
}) => assert_eq!(
|
||||
*o.get_data(&String::from("version")).borrow(),
|
||||
UntaggedValue::string(String::from("1.0.0")).into_untagged_value()
|
||||
),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inc_plugin_applies_minor() {
|
||||
let mut plugin = Inc::new();
|
||||
|
||||
assert!(plugin
|
||||
.begin_filter(
|
||||
CallStub::new()
|
||||
.with_long_flag("minor")
|
||||
.with_parameter("version")
|
||||
.create()
|
||||
)
|
||||
.is_ok());
|
||||
|
||||
let subject = cargo_sample_record("0.1.3");
|
||||
let output = plugin.filter(subject).unwrap();
|
||||
|
||||
match output[0].as_ref().unwrap() {
|
||||
ReturnSuccess::Value(Value {
|
||||
value: UntaggedValue::Row(o),
|
||||
..
|
||||
}) => assert_eq!(
|
||||
*o.get_data(&String::from("version")).borrow(),
|
||||
UntaggedValue::string(String::from("0.2.0")).into_untagged_value()
|
||||
),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inc_plugin_applies_patch() {
|
||||
let field = String::from("version");
|
||||
let mut plugin = Inc::new();
|
||||
|
||||
assert!(plugin
|
||||
.begin_filter(
|
||||
CallStub::new()
|
||||
.with_long_flag("patch")
|
||||
.with_parameter(&field)
|
||||
.create()
|
||||
)
|
||||
.is_ok());
|
||||
|
||||
let subject = cargo_sample_record("0.1.3");
|
||||
let output = plugin.filter(subject).unwrap();
|
||||
|
||||
match output[0].as_ref().unwrap() {
|
||||
ReturnSuccess::Value(Value {
|
||||
value: UntaggedValue::Row(o),
|
||||
..
|
||||
}) => assert_eq!(
|
||||
*o.get_data(&field).borrow(),
|
||||
UntaggedValue::string(String::from("0.1.4")).into_untagged_value()
|
||||
),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,106 +0,0 @@
|
||||
use nu::{serve_plugin, Plugin};
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{
|
||||
CallInfo, Primitive, ReturnSuccess, ReturnValue, Signature, SyntaxShape, UntaggedValue, Value,
|
||||
};
|
||||
|
||||
use regex::Regex;
|
||||
|
||||
struct Match {
|
||||
column: String,
|
||||
regex: Regex,
|
||||
}
|
||||
|
||||
impl Match {
|
||||
fn new() -> Self {
|
||||
Match {
|
||||
column: String::new(),
|
||||
regex: Regex::new("").unwrap(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Plugin for Match {
|
||||
fn config(&mut self) -> Result<Signature, ShellError> {
|
||||
Ok(Signature::build("match")
|
||||
.desc("filter rows by regex")
|
||||
.required("member", SyntaxShape::Member, "the column name to match")
|
||||
.required("regex", SyntaxShape::String, "the regex to match with")
|
||||
.filter())
|
||||
}
|
||||
fn begin_filter(&mut self, call_info: CallInfo) -> Result<Vec<ReturnValue>, ShellError> {
|
||||
if let Some(args) = call_info.args.positional {
|
||||
match &args[0] {
|
||||
Value {
|
||||
value: UntaggedValue::Primitive(Primitive::String(s)),
|
||||
..
|
||||
} => {
|
||||
self.column = s.clone();
|
||||
}
|
||||
Value { tag, .. } => {
|
||||
return Err(ShellError::labeled_error(
|
||||
"Unrecognized type in params",
|
||||
"value",
|
||||
tag,
|
||||
));
|
||||
}
|
||||
}
|
||||
match &args[1] {
|
||||
Value {
|
||||
value: UntaggedValue::Primitive(Primitive::String(s)),
|
||||
..
|
||||
} => {
|
||||
self.regex = Regex::new(s).unwrap();
|
||||
}
|
||||
Value { tag, .. } => {
|
||||
return Err(ShellError::labeled_error(
|
||||
"Unrecognized type in params",
|
||||
"value",
|
||||
tag,
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(vec![])
|
||||
}
|
||||
|
||||
fn filter(&mut self, input: Value) -> Result<Vec<ReturnValue>, ShellError> {
|
||||
let flag: bool;
|
||||
match &input {
|
||||
Value {
|
||||
value: UntaggedValue::Row(dict),
|
||||
tag,
|
||||
} => {
|
||||
if let Some(val) = dict.entries.get(&self.column) {
|
||||
if let Ok(s) = val.as_string() {
|
||||
flag = self.regex.is_match(&s);
|
||||
} else {
|
||||
return Err(ShellError::labeled_error(
|
||||
"expected string",
|
||||
"value",
|
||||
val.tag(),
|
||||
));
|
||||
}
|
||||
} else {
|
||||
return Err(ShellError::labeled_error(
|
||||
format!("column not in row! {:?} {:?}", &self.column, dict),
|
||||
"row",
|
||||
tag,
|
||||
));
|
||||
}
|
||||
}
|
||||
Value { tag, .. } => {
|
||||
return Err(ShellError::labeled_error("Expected row", "value", tag));
|
||||
}
|
||||
}
|
||||
if flag {
|
||||
Ok(vec![Ok(ReturnSuccess::Value(input))])
|
||||
} else {
|
||||
Ok(vec![])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
serve_plugin(&mut Match::new());
|
||||
}
|
@ -1,61 +0,0 @@
|
||||
use nu::{serve_plugin, Plugin};
|
||||
use nu_errors::{CoerceInto, ShellError};
|
||||
use nu_protocol::{
|
||||
CallInfo, Primitive, ReturnSuccess, ReturnValue, Signature, SyntaxShape, UntaggedValue, Value,
|
||||
};
|
||||
use nu_source::TaggedItem;
|
||||
|
||||
struct Skip {
|
||||
skip_amount: i64,
|
||||
}
|
||||
|
||||
impl Skip {
|
||||
fn new() -> Skip {
|
||||
Skip { skip_amount: 0 }
|
||||
}
|
||||
}
|
||||
|
||||
impl Plugin for Skip {
|
||||
fn config(&mut self) -> Result<Signature, ShellError> {
|
||||
Ok(Signature::build("skip")
|
||||
.desc("Skip a number of rows")
|
||||
.rest(SyntaxShape::Number, "the number of rows to skip")
|
||||
.filter())
|
||||
}
|
||||
fn begin_filter(&mut self, call_info: CallInfo) -> Result<Vec<ReturnValue>, ShellError> {
|
||||
if let Some(args) = call_info.args.positional {
|
||||
for arg in args {
|
||||
match arg {
|
||||
Value {
|
||||
value: UntaggedValue::Primitive(Primitive::Int(i)),
|
||||
tag,
|
||||
} => {
|
||||
self.skip_amount = i.tagged(tag).coerce_into("converting for skip")?;
|
||||
}
|
||||
_ => {
|
||||
return Err(ShellError::labeled_error(
|
||||
"Unrecognized type in params",
|
||||
"expected an integer",
|
||||
arg.tag(),
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(vec![])
|
||||
}
|
||||
|
||||
fn filter(&mut self, input: Value) -> Result<Vec<ReturnValue>, ShellError> {
|
||||
if self.skip_amount == 0 {
|
||||
Ok(vec![ReturnSuccess::value(input)])
|
||||
} else {
|
||||
self.skip_amount -= 1;
|
||||
Ok(vec![])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
serve_plugin(&mut Skip::new());
|
||||
}
|
@ -1,977 +0,0 @@
|
||||
use nu::{did_you_mean, serve_plugin, Plugin, ValueExt};
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{
|
||||
CallInfo, ColumnPath, Primitive, ReturnSuccess, ReturnValue, ShellTypeName, Signature,
|
||||
SyntaxShape, UntaggedValue, Value,
|
||||
};
|
||||
use nu_source::{span_for_spanned_list, Tagged};
|
||||
|
||||
use regex::Regex;
|
||||
use std::cmp;
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
enum Action {
|
||||
Downcase,
|
||||
Upcase,
|
||||
ToInteger,
|
||||
Substring(usize, usize),
|
||||
Replace(ReplaceAction),
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
enum ReplaceAction {
|
||||
Direct(String),
|
||||
FindAndReplace(String, String),
|
||||
}
|
||||
|
||||
struct Str {
|
||||
field: Option<Tagged<ColumnPath>>,
|
||||
error: Option<String>,
|
||||
action: Option<Action>,
|
||||
}
|
||||
|
||||
impl Str {
|
||||
fn new() -> Str {
|
||||
Str {
|
||||
field: None,
|
||||
error: None,
|
||||
action: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn apply(&self, input: &str) -> Result<UntaggedValue, ShellError> {
|
||||
let applied = match self.action.as_ref() {
|
||||
Some(Action::Downcase) => UntaggedValue::string(input.to_ascii_lowercase()),
|
||||
Some(Action::Upcase) => UntaggedValue::string(input.to_ascii_uppercase()),
|
||||
Some(Action::Substring(s, e)) => {
|
||||
let end: usize = cmp::min(*e, input.len());
|
||||
let start: usize = *s;
|
||||
if start > input.len() - 1 {
|
||||
UntaggedValue::string("")
|
||||
} else {
|
||||
UntaggedValue::string(
|
||||
&input
|
||||
.chars()
|
||||
.skip(start)
|
||||
.take(end - start)
|
||||
.collect::<String>(),
|
||||
)
|
||||
}
|
||||
}
|
||||
Some(Action::Replace(mode)) => match mode {
|
||||
ReplaceAction::Direct(replacement) => UntaggedValue::string(replacement.as_str()),
|
||||
ReplaceAction::FindAndReplace(find, replacement) => {
|
||||
let regex = Regex::new(find.as_str());
|
||||
|
||||
match regex {
|
||||
Ok(re) => UntaggedValue::string(
|
||||
re.replace(input, replacement.as_str()).to_owned(),
|
||||
),
|
||||
Err(_) => UntaggedValue::string(input),
|
||||
}
|
||||
}
|
||||
},
|
||||
Some(Action::ToInteger) => match input.trim() {
|
||||
other => match other.parse::<i64>() {
|
||||
Ok(v) => UntaggedValue::int(v),
|
||||
Err(_) => UntaggedValue::string(input),
|
||||
},
|
||||
},
|
||||
None => UntaggedValue::string(input),
|
||||
};
|
||||
|
||||
Ok(applied)
|
||||
}
|
||||
|
||||
fn for_field(&mut self, column_path: Tagged<ColumnPath>) {
|
||||
self.field = Some(column_path);
|
||||
}
|
||||
|
||||
fn permit(&mut self) -> bool {
|
||||
self.action.is_none()
|
||||
}
|
||||
|
||||
fn log_error(&mut self, message: &str) {
|
||||
self.error = Some(message.to_string());
|
||||
}
|
||||
|
||||
fn for_to_int(&mut self) {
|
||||
if self.permit() {
|
||||
self.action = Some(Action::ToInteger);
|
||||
} else {
|
||||
self.log_error("can only apply one");
|
||||
}
|
||||
}
|
||||
|
||||
fn for_downcase(&mut self) {
|
||||
if self.permit() {
|
||||
self.action = Some(Action::Downcase);
|
||||
} else {
|
||||
self.log_error("can only apply one");
|
||||
}
|
||||
}
|
||||
|
||||
fn for_upcase(&mut self) {
|
||||
if self.permit() {
|
||||
self.action = Some(Action::Upcase);
|
||||
} else {
|
||||
self.log_error("can only apply one");
|
||||
}
|
||||
}
|
||||
|
||||
fn for_substring(&mut self, s: String) {
|
||||
let v: Vec<&str> = s.split(',').collect();
|
||||
let start: usize = match v[0] {
|
||||
"" => 0,
|
||||
_ => v[0].trim().parse().unwrap(),
|
||||
};
|
||||
let end: usize = match v[1] {
|
||||
"" => usize::max_value(),
|
||||
_ => v[1].trim().parse().unwrap(),
|
||||
};
|
||||
if start > end {
|
||||
self.log_error("End must be greater than or equal to Start");
|
||||
} else if self.permit() {
|
||||
self.action = Some(Action::Substring(start, end));
|
||||
} else {
|
||||
self.log_error("can only apply one");
|
||||
}
|
||||
}
|
||||
|
||||
fn for_replace(&mut self, mode: ReplaceAction) {
|
||||
if self.permit() {
|
||||
self.action = Some(Action::Replace(mode));
|
||||
} else {
|
||||
self.log_error("can only apply one");
|
||||
}
|
||||
}
|
||||
|
||||
pub fn usage() -> &'static str {
|
||||
"Usage: str field [--downcase|--upcase|--to-int|--substring \"start,end\"|--replace|--find-replace [pattern replacement]]]"
|
||||
}
|
||||
}
|
||||
|
||||
impl Str {
|
||||
fn strutils(&self, value: Value) -> Result<Value, ShellError> {
|
||||
match &value.value {
|
||||
UntaggedValue::Primitive(Primitive::String(ref s)) => {
|
||||
Ok(self.apply(&s)?.into_value(value.tag()))
|
||||
}
|
||||
UntaggedValue::Primitive(Primitive::Line(ref s)) => {
|
||||
Ok(self.apply(&s)?.into_value(value.tag()))
|
||||
}
|
||||
UntaggedValue::Row(_) => match self.field {
|
||||
Some(ref f) => {
|
||||
let fields = f.clone();
|
||||
|
||||
let replace_for =
|
||||
value.get_data_by_column_path(
|
||||
&f,
|
||||
Box::new(move |(obj_source, column_path_tried, error)| {
|
||||
match did_you_mean(&obj_source, &column_path_tried) {
|
||||
Some(suggestions) => ShellError::labeled_error(
|
||||
"Unknown column",
|
||||
format!("did you mean '{}'?", suggestions[0].1),
|
||||
span_for_spanned_list(fields.iter().map(|p| p.span)),
|
||||
),
|
||||
None => error,
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
let got = replace_for?;
|
||||
let replacement = self.strutils(got.clone())?;
|
||||
|
||||
match value.replace_data_at_column_path(
|
||||
&f,
|
||||
replacement.value.clone().into_untagged_value(),
|
||||
) {
|
||||
Some(v) => Ok(v),
|
||||
None => Err(ShellError::labeled_error(
|
||||
"str could not find field to replace",
|
||||
"column name",
|
||||
value.tag(),
|
||||
)),
|
||||
}
|
||||
}
|
||||
None => Err(ShellError::untagged_runtime_error(format!(
|
||||
"{}: {}",
|
||||
"str needs a column when applied to a value in a row",
|
||||
Str::usage()
|
||||
))),
|
||||
},
|
||||
_ => Err(ShellError::labeled_error(
|
||||
"Unrecognized type in stream",
|
||||
value.type_name(),
|
||||
value.tag,
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Plugin for Str {
|
||||
fn config(&mut self) -> Result<Signature, ShellError> {
|
||||
Ok(Signature::build("str")
|
||||
.desc("Apply string function. Optional use the column of a table")
|
||||
.switch("downcase", "convert string to lowercase")
|
||||
.switch("upcase", "convert string to uppercase")
|
||||
.switch("to-int", "convert string to integer")
|
||||
.named("replace", SyntaxShape::String, "replaces the string")
|
||||
.named(
|
||||
"find-replace",
|
||||
SyntaxShape::Any,
|
||||
"finds and replaces [pattern replacement]",
|
||||
)
|
||||
.named(
|
||||
"substring",
|
||||
SyntaxShape::String,
|
||||
"convert string to portion of original, requires \"start,end\"",
|
||||
)
|
||||
.rest(SyntaxShape::ColumnPath, "the column(s) to convert")
|
||||
.filter())
|
||||
}
|
||||
|
||||
fn begin_filter(&mut self, call_info: CallInfo) -> Result<Vec<ReturnValue>, ShellError> {
|
||||
let args = call_info.args;
|
||||
|
||||
if args.has("downcase") {
|
||||
self.for_downcase();
|
||||
}
|
||||
if args.has("upcase") {
|
||||
self.for_upcase();
|
||||
}
|
||||
if args.has("to-int") {
|
||||
self.for_to_int();
|
||||
}
|
||||
if args.has("substring") {
|
||||
if let Some(start_end) = args.get("substring") {
|
||||
match start_end {
|
||||
Value {
|
||||
value: UntaggedValue::Primitive(Primitive::String(s)),
|
||||
..
|
||||
} => {
|
||||
self.for_substring(s.to_string());
|
||||
}
|
||||
_ => {
|
||||
return Err(ShellError::labeled_error(
|
||||
"Unrecognized type in params",
|
||||
start_end.type_name(),
|
||||
&start_end.tag,
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if args.has("replace") {
|
||||
if let Some(Value {
|
||||
value: UntaggedValue::Primitive(Primitive::String(replacement)),
|
||||
..
|
||||
}) = args.get("replace")
|
||||
{
|
||||
self.for_replace(ReplaceAction::Direct(replacement.clone()));
|
||||
}
|
||||
}
|
||||
|
||||
if args.has("find-replace") {
|
||||
if let Some(Value {
|
||||
value: UntaggedValue::Table(arguments),
|
||||
..
|
||||
}) = args.get("find-replace")
|
||||
{
|
||||
self.for_replace(ReplaceAction::FindAndReplace(
|
||||
arguments.get(0).unwrap().as_string()?.to_string(),
|
||||
arguments.get(1).unwrap().as_string()?.to_string(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(possible_field) = args.nth(0) {
|
||||
let possible_field = possible_field.as_column_path()?;
|
||||
self.for_field(possible_field);
|
||||
}
|
||||
|
||||
match &self.error {
|
||||
Some(reason) => Err(ShellError::untagged_runtime_error(format!(
|
||||
"{}: {}",
|
||||
reason,
|
||||
Str::usage()
|
||||
))),
|
||||
None => Ok(vec![]),
|
||||
}
|
||||
}
|
||||
|
||||
fn filter(&mut self, input: Value) -> Result<Vec<ReturnValue>, ShellError> {
|
||||
Ok(vec![ReturnSuccess::value(self.strutils(input)?)])
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
serve_plugin(&mut Str::new());
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{Action, ReplaceAction, Str};
|
||||
use indexmap::IndexMap;
|
||||
use nu::{Plugin, TaggedDictBuilder, ValueExt};
|
||||
use nu_protocol::{CallInfo, EvaluatedArgs, Primitive, ReturnSuccess, UntaggedValue, Value};
|
||||
use nu_source::Tag;
|
||||
use num_bigint::BigInt;
|
||||
|
||||
fn string(input: impl Into<String>) -> Value {
|
||||
UntaggedValue::string(input.into()).into_untagged_value()
|
||||
}
|
||||
|
||||
fn table(list: &Vec<Value>) -> Value {
|
||||
UntaggedValue::table(list).into_untagged_value()
|
||||
}
|
||||
|
||||
fn column_path(paths: &Vec<Value>) -> Value {
|
||||
UntaggedValue::Primitive(Primitive::ColumnPath(
|
||||
table(&paths.iter().cloned().collect())
|
||||
.as_column_path()
|
||||
.unwrap()
|
||||
.item,
|
||||
))
|
||||
.into_untagged_value()
|
||||
}
|
||||
struct CallStub {
|
||||
positionals: Vec<Value>,
|
||||
flags: IndexMap<String, Value>,
|
||||
}
|
||||
|
||||
impl CallStub {
|
||||
fn new() -> CallStub {
|
||||
CallStub {
|
||||
positionals: vec![],
|
||||
flags: indexmap::IndexMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn with_named_parameter(&mut self, name: &str, value: Value) -> &mut Self {
|
||||
self.flags.insert(name.to_string(), value);
|
||||
self
|
||||
}
|
||||
|
||||
fn with_long_flag(&mut self, name: &str) -> &mut Self {
|
||||
self.flags.insert(
|
||||
name.to_string(),
|
||||
UntaggedValue::boolean(true).into_value(Tag::unknown()),
|
||||
);
|
||||
self
|
||||
}
|
||||
|
||||
fn with_parameter(&mut self, name: &str) -> &mut Self {
|
||||
let fields: Vec<Value> = name
|
||||
.split(".")
|
||||
.map(|s| UntaggedValue::string(s.to_string()).into_value(Tag::unknown()))
|
||||
.collect();
|
||||
|
||||
self.positionals.push(column_path(&fields));
|
||||
self
|
||||
}
|
||||
|
||||
fn create(&self) -> CallInfo {
|
||||
CallInfo {
|
||||
args: EvaluatedArgs::new(Some(self.positionals.clone()), Some(self.flags.clone())),
|
||||
name_tag: Tag::unknown(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn structured_sample_record(key: &str, value: &str) -> Value {
|
||||
let mut record = TaggedDictBuilder::new(Tag::unknown());
|
||||
record.insert_untagged(key.clone(), UntaggedValue::string(value));
|
||||
record.into_value()
|
||||
}
|
||||
|
||||
fn unstructured_sample_record(value: &str) -> Value {
|
||||
UntaggedValue::string(value).into_value(Tag::unknown())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn str_plugin_configuration_flags_wired() {
|
||||
let mut plugin = Str::new();
|
||||
|
||||
let configured = plugin.config().unwrap();
|
||||
|
||||
for action_flag in &[
|
||||
"downcase",
|
||||
"upcase",
|
||||
"to-int",
|
||||
"substring",
|
||||
"replace",
|
||||
"find-replace",
|
||||
] {
|
||||
assert!(configured.named.get(*action_flag).is_some());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn str_plugin_accepts_downcase() {
|
||||
let mut plugin = Str::new();
|
||||
|
||||
assert!(plugin
|
||||
.begin_filter(CallStub::new().with_long_flag("downcase").create())
|
||||
.is_ok());
|
||||
assert_eq!(plugin.action.unwrap(), Action::Downcase);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn str_plugin_accepts_upcase() {
|
||||
let mut plugin = Str::new();
|
||||
|
||||
assert!(plugin
|
||||
.begin_filter(CallStub::new().with_long_flag("upcase").create())
|
||||
.is_ok());
|
||||
assert_eq!(plugin.action.unwrap(), Action::Upcase);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn str_plugin_accepts_to_int() {
|
||||
let mut plugin = Str::new();
|
||||
|
||||
assert!(plugin
|
||||
.begin_filter(CallStub::new().with_long_flag("to-int").create())
|
||||
.is_ok());
|
||||
assert_eq!(plugin.action.unwrap(), Action::ToInteger);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn str_plugin_accepts_replace() {
|
||||
let mut plugin = Str::new();
|
||||
|
||||
let argument = String::from("replace_text");
|
||||
|
||||
assert!(plugin
|
||||
.begin_filter(
|
||||
CallStub::new()
|
||||
.with_named_parameter("replace", string(&argument))
|
||||
.create()
|
||||
)
|
||||
.is_ok());
|
||||
|
||||
match plugin.action {
|
||||
Some(Action::Replace(ReplaceAction::Direct(replace_with))) => {
|
||||
assert_eq!(replace_with, argument)
|
||||
}
|
||||
Some(_) | None => panic!("Din't accept."),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn str_plugin_accepts_find_replace() {
|
||||
let mut plugin = Str::new();
|
||||
|
||||
let search_argument = String::from("kittens");
|
||||
let replace_argument = String::from("jotandrehuda");
|
||||
|
||||
assert!(plugin
|
||||
.begin_filter(
|
||||
CallStub::new()
|
||||
.with_named_parameter(
|
||||
"find-replace",
|
||||
table(&vec![string(&search_argument), string(&replace_argument)])
|
||||
)
|
||||
.create()
|
||||
)
|
||||
.is_ok());
|
||||
|
||||
match plugin.action {
|
||||
Some(Action::Replace(ReplaceAction::FindAndReplace(find_with, replace_with))) => {
|
||||
assert_eq!(find_with, search_argument);
|
||||
assert_eq!(replace_with, replace_argument);
|
||||
}
|
||||
Some(_) | None => panic!("Din't accept."),
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn str_plugin_accepts_field() {
|
||||
let mut plugin = Str::new();
|
||||
|
||||
assert!(plugin
|
||||
.begin_filter(
|
||||
CallStub::new()
|
||||
.with_parameter("package.description")
|
||||
.create()
|
||||
)
|
||||
.is_ok());
|
||||
|
||||
let actual = &*plugin.field.unwrap();
|
||||
let actual = UntaggedValue::Primitive(Primitive::ColumnPath(actual.clone()));
|
||||
let actual = actual.into_value(Tag::unknown());
|
||||
|
||||
assert_eq!(
|
||||
actual,
|
||||
column_path(&vec![string("package"), string("description")])
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn str_plugin_accepts_only_one_action() {
|
||||
let mut plugin = Str::new();
|
||||
|
||||
assert!(plugin
|
||||
.begin_filter(
|
||||
CallStub::new()
|
||||
.with_long_flag("upcase")
|
||||
.with_long_flag("downcase")
|
||||
.with_long_flag("to-int")
|
||||
.with_long_flag("substring")
|
||||
.create(),
|
||||
)
|
||||
.is_err());
|
||||
assert_eq!(plugin.error, Some("can only apply one".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn str_downcases() {
|
||||
let mut strutils = Str::new();
|
||||
strutils.for_downcase();
|
||||
assert_eq!(
|
||||
strutils.apply("ANDRES").unwrap(),
|
||||
UntaggedValue::string("andres")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn str_upcases() {
|
||||
let mut strutils = Str::new();
|
||||
strutils.for_upcase();
|
||||
assert_eq!(
|
||||
strutils.apply("andres").unwrap(),
|
||||
UntaggedValue::string("ANDRES")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn str_to_int() {
|
||||
let mut strutils = Str::new();
|
||||
strutils.for_to_int();
|
||||
assert_eq!(
|
||||
strutils.apply("9999").unwrap(),
|
||||
UntaggedValue::int(9999 as i64)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn str_replace() {
|
||||
let mut strutils = Str::new();
|
||||
strutils.for_replace(ReplaceAction::Direct("robalino".to_string()));
|
||||
|
||||
assert_eq!(
|
||||
strutils.apply("andres").unwrap(),
|
||||
UntaggedValue::string("robalino")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn str_find_replace() {
|
||||
let mut strutils = Str::new();
|
||||
strutils.for_replace(ReplaceAction::FindAndReplace(
|
||||
"kittens".to_string(),
|
||||
"jotandrehuda".to_string(),
|
||||
));
|
||||
assert_eq!(
|
||||
strutils.apply("wykittens").unwrap(),
|
||||
UntaggedValue::string("wyjotandrehuda")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn str_plugin_applies_upcase_with_field() {
|
||||
let mut plugin = Str::new();
|
||||
|
||||
assert!(plugin
|
||||
.begin_filter(
|
||||
CallStub::new()
|
||||
.with_long_flag("upcase")
|
||||
.with_parameter("name")
|
||||
.create()
|
||||
)
|
||||
.is_ok());
|
||||
|
||||
let subject = structured_sample_record("name", "jotandrehuda");
|
||||
let output = plugin.filter(subject).unwrap();
|
||||
|
||||
match output[0].as_ref().unwrap() {
|
||||
ReturnSuccess::Value(Value {
|
||||
value: UntaggedValue::Row(o),
|
||||
..
|
||||
}) => assert_eq!(
|
||||
*o.get_data(&String::from("name")).borrow(),
|
||||
UntaggedValue::string(String::from("JOTANDREHUDA")).into_untagged_value()
|
||||
),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn str_plugin_applies_upcase_without_field() {
|
||||
let mut plugin = Str::new();
|
||||
|
||||
assert!(plugin
|
||||
.begin_filter(CallStub::new().with_long_flag("upcase").create())
|
||||
.is_ok());
|
||||
|
||||
let subject = unstructured_sample_record("jotandrehuda");
|
||||
let output = plugin.filter(subject).unwrap();
|
||||
|
||||
match output[0].as_ref().unwrap() {
|
||||
ReturnSuccess::Value(Value {
|
||||
value: UntaggedValue::Primitive(Primitive::String(s)),
|
||||
..
|
||||
}) => assert_eq!(*s, String::from("JOTANDREHUDA")),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn str_plugin_applies_downcase_with_field() {
|
||||
let mut plugin = Str::new();
|
||||
|
||||
assert!(plugin
|
||||
.begin_filter(
|
||||
CallStub::new()
|
||||
.with_long_flag("downcase")
|
||||
.with_parameter("name")
|
||||
.create()
|
||||
)
|
||||
.is_ok());
|
||||
|
||||
let subject = structured_sample_record("name", "JOTANDREHUDA");
|
||||
let output = plugin.filter(subject).unwrap();
|
||||
|
||||
match output[0].as_ref().unwrap() {
|
||||
ReturnSuccess::Value(Value {
|
||||
value: UntaggedValue::Row(o),
|
||||
..
|
||||
}) => assert_eq!(
|
||||
*o.get_data(&String::from("name")).borrow(),
|
||||
UntaggedValue::string(String::from("jotandrehuda")).into_untagged_value()
|
||||
),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn str_plugin_applies_downcase_without_field() {
|
||||
let mut plugin = Str::new();
|
||||
|
||||
assert!(plugin
|
||||
.begin_filter(CallStub::new().with_long_flag("downcase").create())
|
||||
.is_ok());
|
||||
|
||||
let subject = unstructured_sample_record("JOTANDREHUDA");
|
||||
let output = plugin.filter(subject).unwrap();
|
||||
|
||||
match output[0].as_ref().unwrap() {
|
||||
ReturnSuccess::Value(Value {
|
||||
value: UntaggedValue::Primitive(Primitive::String(s)),
|
||||
..
|
||||
}) => assert_eq!(*s, String::from("jotandrehuda")),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn str_plugin_applies_to_int_with_field() {
|
||||
let mut plugin = Str::new();
|
||||
|
||||
assert!(plugin
|
||||
.begin_filter(
|
||||
CallStub::new()
|
||||
.with_long_flag("to-int")
|
||||
.with_parameter("Nu_birthday")
|
||||
.create()
|
||||
)
|
||||
.is_ok());
|
||||
|
||||
let subject = structured_sample_record("Nu_birthday", "10");
|
||||
let output = plugin.filter(subject).unwrap();
|
||||
|
||||
match output[0].as_ref().unwrap() {
|
||||
ReturnSuccess::Value(Value {
|
||||
value: UntaggedValue::Row(o),
|
||||
..
|
||||
}) => assert_eq!(
|
||||
*o.get_data(&String::from("Nu_birthday")).borrow(),
|
||||
UntaggedValue::int(10).into_untagged_value()
|
||||
),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn str_plugin_applies_to_int_without_field() {
|
||||
let mut plugin = Str::new();
|
||||
|
||||
assert!(plugin
|
||||
.begin_filter(CallStub::new().with_long_flag("to-int").create())
|
||||
.is_ok());
|
||||
|
||||
let subject = unstructured_sample_record("10");
|
||||
let output = plugin.filter(subject).unwrap();
|
||||
|
||||
match output[0].as_ref().unwrap() {
|
||||
ReturnSuccess::Value(Value {
|
||||
value: UntaggedValue::Primitive(Primitive::Int(i)),
|
||||
..
|
||||
}) => assert_eq!(*i, BigInt::from(10)),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn str_plugin_applies_substring_without_field() {
|
||||
let mut plugin = Str::new();
|
||||
|
||||
assert!(plugin
|
||||
.begin_filter(
|
||||
CallStub::new()
|
||||
.with_named_parameter("substring", string("0,1"))
|
||||
.create()
|
||||
)
|
||||
.is_ok());
|
||||
|
||||
let subject = unstructured_sample_record("0123456789");
|
||||
let output = plugin.filter(subject).unwrap();
|
||||
|
||||
match output[0].as_ref().unwrap() {
|
||||
ReturnSuccess::Value(Value {
|
||||
value: UntaggedValue::Primitive(Primitive::String(s)),
|
||||
..
|
||||
}) => assert_eq!(*s, String::from("0")),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn str_plugin_applies_substring_exceeding_string_length() {
|
||||
let mut plugin = Str::new();
|
||||
|
||||
assert!(plugin
|
||||
.begin_filter(
|
||||
CallStub::new()
|
||||
.with_named_parameter("substring", string("0,11"))
|
||||
.create()
|
||||
)
|
||||
.is_ok());
|
||||
|
||||
let subject = unstructured_sample_record("0123456789");
|
||||
let output = plugin.filter(subject).unwrap();
|
||||
|
||||
match output[0].as_ref().unwrap() {
|
||||
ReturnSuccess::Value(Value {
|
||||
value: UntaggedValue::Primitive(Primitive::String(s)),
|
||||
..
|
||||
}) => assert_eq!(*s, String::from("0123456789")),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn str_plugin_applies_substring_returns_blank_if_start_exceeds_length() {
|
||||
let mut plugin = Str::new();
|
||||
|
||||
assert!(plugin
|
||||
.begin_filter(
|
||||
CallStub::new()
|
||||
.with_named_parameter("substring", string("20,30"))
|
||||
.create()
|
||||
)
|
||||
.is_ok());
|
||||
|
||||
let subject = unstructured_sample_record("0123456789");
|
||||
let output = plugin.filter(subject).unwrap();
|
||||
|
||||
match output[0].as_ref().unwrap() {
|
||||
ReturnSuccess::Value(Value {
|
||||
value: UntaggedValue::Primitive(Primitive::String(s)),
|
||||
..
|
||||
}) => assert_eq!(*s, String::from("")),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn str_plugin_applies_substring_treats_blank_start_as_zero() {
|
||||
let mut plugin = Str::new();
|
||||
|
||||
assert!(plugin
|
||||
.begin_filter(
|
||||
CallStub::new()
|
||||
.with_named_parameter("substring", string(",5"))
|
||||
.create()
|
||||
)
|
||||
.is_ok());
|
||||
|
||||
let subject = unstructured_sample_record("0123456789");
|
||||
let output = plugin.filter(subject).unwrap();
|
||||
|
||||
match output[0].as_ref().unwrap() {
|
||||
ReturnSuccess::Value(Value {
|
||||
value: UntaggedValue::Primitive(Primitive::String(s)),
|
||||
..
|
||||
}) => assert_eq!(*s, String::from("01234")),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn str_plugin_applies_substring_treats_blank_end_as_length() {
|
||||
let mut plugin = Str::new();
|
||||
|
||||
assert!(plugin
|
||||
.begin_filter(
|
||||
CallStub::new()
|
||||
.with_named_parameter("substring", string("2,"))
|
||||
.create()
|
||||
)
|
||||
.is_ok());
|
||||
|
||||
let subject = unstructured_sample_record("0123456789");
|
||||
let output = plugin.filter(subject).unwrap();
|
||||
|
||||
match output[0].as_ref().unwrap() {
|
||||
ReturnSuccess::Value(Value {
|
||||
value: UntaggedValue::Primitive(Primitive::String(s)),
|
||||
..
|
||||
}) => assert_eq!(*s, String::from("23456789")),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn str_plugin_applies_substring_returns_error_if_start_exceeds_end() {
|
||||
let mut plugin = Str::new();
|
||||
|
||||
assert!(plugin
|
||||
.begin_filter(
|
||||
CallStub::new()
|
||||
.with_named_parameter("substring", string("3,1"))
|
||||
.create()
|
||||
)
|
||||
.is_err());
|
||||
assert_eq!(
|
||||
plugin.error,
|
||||
Some("End must be greater than or equal to Start".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn str_plugin_applies_replace_with_field() {
|
||||
let mut plugin = Str::new();
|
||||
|
||||
assert!(plugin
|
||||
.begin_filter(
|
||||
CallStub::new()
|
||||
.with_parameter("rustconf")
|
||||
.with_named_parameter("replace", string("22nd August 2019"))
|
||||
.create()
|
||||
)
|
||||
.is_ok());
|
||||
|
||||
let subject = structured_sample_record("rustconf", "1st January 1970");
|
||||
let output = plugin.filter(subject).unwrap();
|
||||
|
||||
match output[0].as_ref().unwrap() {
|
||||
ReturnSuccess::Value(Value {
|
||||
value: UntaggedValue::Row(o),
|
||||
..
|
||||
}) => assert_eq!(
|
||||
*o.get_data(&String::from("rustconf")).borrow(),
|
||||
Value {
|
||||
value: UntaggedValue::string(String::from("22nd August 2019")),
|
||||
tag: Tag::unknown()
|
||||
}
|
||||
),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn str_plugin_applies_replace_without_field() {
|
||||
let mut plugin = Str::new();
|
||||
|
||||
assert!(plugin
|
||||
.begin_filter(
|
||||
CallStub::new()
|
||||
.with_named_parameter("replace", string("22nd August 2019"))
|
||||
.create()
|
||||
)
|
||||
.is_ok());
|
||||
|
||||
let subject = unstructured_sample_record("1st January 1970");
|
||||
let output = plugin.filter(subject).unwrap();
|
||||
|
||||
match output[0].as_ref().unwrap() {
|
||||
ReturnSuccess::Value(Value {
|
||||
value: UntaggedValue::Primitive(Primitive::String(s)),
|
||||
..
|
||||
}) => assert_eq!(*s, String::from("22nd August 2019")),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn str_plugin_applies_find_replace_with_field() {
|
||||
let mut plugin = Str::new();
|
||||
|
||||
assert!(plugin
|
||||
.begin_filter(
|
||||
CallStub::new()
|
||||
.with_parameter("staff")
|
||||
.with_named_parameter(
|
||||
"find-replace",
|
||||
table(&vec![string("kittens"), string("jotandrehuda")])
|
||||
)
|
||||
.create()
|
||||
)
|
||||
.is_ok());
|
||||
|
||||
let subject = structured_sample_record("staff", "wykittens");
|
||||
let output = plugin.filter(subject).unwrap();
|
||||
|
||||
match output[0].as_ref().unwrap() {
|
||||
ReturnSuccess::Value(Value {
|
||||
value: UntaggedValue::Row(o),
|
||||
..
|
||||
}) => assert_eq!(
|
||||
*o.get_data(&String::from("staff")).borrow(),
|
||||
Value {
|
||||
value: UntaggedValue::string(String::from("wyjotandrehuda")),
|
||||
tag: Tag::unknown()
|
||||
}
|
||||
),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn str_plugin_applies_find_replace_without_field() {
|
||||
let mut plugin = Str::new();
|
||||
|
||||
assert!(plugin
|
||||
.begin_filter(
|
||||
CallStub::new()
|
||||
.with_named_parameter(
|
||||
"find-replace",
|
||||
table(&vec![string("kittens"), string("jotandrehuda")])
|
||||
)
|
||||
.create()
|
||||
)
|
||||
.is_ok());
|
||||
|
||||
let subject = unstructured_sample_record("wykittens");
|
||||
let output = plugin.filter(subject).unwrap();
|
||||
|
||||
match output[0].as_ref().unwrap() {
|
||||
ReturnSuccess::Value(Value {
|
||||
value: UntaggedValue::Primitive(Primitive::String(s)),
|
||||
..
|
||||
}) => assert_eq!(*s, String::from("wyjotandrehuda")),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,95 +0,0 @@
|
||||
use nu::{serve_plugin, Plugin};
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{
|
||||
CallInfo, Primitive, ReturnSuccess, ReturnValue, Signature, UntaggedValue, Value,
|
||||
};
|
||||
|
||||
struct Sum {
|
||||
total: Option<Value>,
|
||||
}
|
||||
impl Sum {
|
||||
fn new() -> Sum {
|
||||
Sum { total: None }
|
||||
}
|
||||
|
||||
fn sum(&mut self, value: Value) -> Result<(), ShellError> {
|
||||
match &value.value {
|
||||
UntaggedValue::Primitive(Primitive::Nothing) => Ok(()),
|
||||
UntaggedValue::Primitive(Primitive::Int(i)) => {
|
||||
match &self.total {
|
||||
Some(Value {
|
||||
value: UntaggedValue::Primitive(Primitive::Int(j)),
|
||||
tag,
|
||||
}) => {
|
||||
//TODO: handle overflow
|
||||
self.total = Some(UntaggedValue::int(i + j).into_value(tag));
|
||||
Ok(())
|
||||
}
|
||||
None => {
|
||||
self.total = Some(value.clone());
|
||||
Ok(())
|
||||
}
|
||||
_ => Err(ShellError::labeled_error(
|
||||
"Could not sum non-integer or unrelated types",
|
||||
"source",
|
||||
value.tag,
|
||||
)),
|
||||
}
|
||||
}
|
||||
UntaggedValue::Primitive(Primitive::Bytes(b)) => {
|
||||
match &self.total {
|
||||
Some(Value {
|
||||
value: UntaggedValue::Primitive(Primitive::Bytes(j)),
|
||||
tag,
|
||||
}) => {
|
||||
//TODO: handle overflow
|
||||
self.total = Some(UntaggedValue::bytes(b + j).into_value(tag));
|
||||
Ok(())
|
||||
}
|
||||
None => {
|
||||
self.total = Some(value);
|
||||
Ok(())
|
||||
}
|
||||
_ => Err(ShellError::labeled_error(
|
||||
"Could not sum non-integer or unrelated types",
|
||||
"source",
|
||||
value.tag,
|
||||
)),
|
||||
}
|
||||
}
|
||||
x => Err(ShellError::labeled_error(
|
||||
format!("Unrecognized type in stream: {:?}", x),
|
||||
"source",
|
||||
value.tag,
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Plugin for Sum {
|
||||
fn config(&mut self) -> Result<Signature, ShellError> {
|
||||
Ok(Signature::build("sum")
|
||||
.desc("Sum a column of values.")
|
||||
.filter())
|
||||
}
|
||||
|
||||
fn begin_filter(&mut self, _: CallInfo) -> Result<Vec<ReturnValue>, ShellError> {
|
||||
Ok(vec![])
|
||||
}
|
||||
|
||||
fn filter(&mut self, input: Value) -> Result<Vec<ReturnValue>, ShellError> {
|
||||
self.sum(input)?;
|
||||
Ok(vec![])
|
||||
}
|
||||
|
||||
fn end_filter(&mut self) -> Result<Vec<ReturnValue>, ShellError> {
|
||||
match self.total {
|
||||
None => Ok(vec![]),
|
||||
Some(ref v) => Ok(vec![ReturnSuccess::value(v.clone())]),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
serve_plugin(&mut Sum::new());
|
||||
}
|
@ -1,97 +0,0 @@
|
||||
use derive_new::new;
|
||||
use nu::{serve_plugin, CallInfo, Plugin, ShellError, Signature, Tagged, Value};
|
||||
use ptree::item::StringItem;
|
||||
use ptree::output::print_tree_with;
|
||||
use ptree::print_config::PrintConfig;
|
||||
use ptree::style::{Color, Style};
|
||||
use ptree::TreeBuilder;
|
||||
|
||||
#[derive(new)]
|
||||
pub struct TreeView {
|
||||
tree: StringItem,
|
||||
}
|
||||
|
||||
impl TreeView {
|
||||
fn from_value_helper(value: &Value, mut builder: &mut TreeBuilder) {
|
||||
match value {
|
||||
UntaggedValue::Primitive(p) => {
|
||||
let _ = builder.add_empty_child(p.format(None));
|
||||
}
|
||||
UntaggedValue::Row(o) => {
|
||||
for (k, v) in o.entries.iter() {
|
||||
builder = builder.begin_child(k.clone());
|
||||
Self::from_value_helper(v, builder);
|
||||
builder = builder.end_child();
|
||||
}
|
||||
}
|
||||
UntaggedValue::Table(l) => {
|
||||
for elem in l.iter() {
|
||||
Self::from_value_helper(elem, builder);
|
||||
}
|
||||
}
|
||||
UntaggedValue::Block(_) => {}
|
||||
UntaggedValue::Binary(_) => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn from_value(value: &Value) -> TreeView {
|
||||
let descs = value.data_descriptors();
|
||||
|
||||
let mut tree = TreeBuilder::new("".to_string());
|
||||
let mut builder = &mut tree;
|
||||
|
||||
for desc in descs {
|
||||
let value = value.get_data(&desc);
|
||||
builder = builder.begin_child(desc.clone());
|
||||
Self::from_value_helper(value.borrow(), &mut builder);
|
||||
builder = builder.end_child();
|
||||
//entries.push((desc.name.clone(), value.borrow().copy()))
|
||||
}
|
||||
|
||||
TreeView::new(builder.build())
|
||||
}
|
||||
|
||||
fn render_view(&self) -> Result<(), ShellError> {
|
||||
// Set up the print configuration
|
||||
let config = {
|
||||
let mut config = PrintConfig::from_env();
|
||||
config.branch = Style {
|
||||
foreground: Some(Color::Green),
|
||||
dimmed: true,
|
||||
..Style::default()
|
||||
};
|
||||
config.leaf = Style {
|
||||
bold: true,
|
||||
..Style::default()
|
||||
};
|
||||
config.indent = 4;
|
||||
config
|
||||
};
|
||||
|
||||
// Print out the tree using custom formatting
|
||||
print_tree_with(&self.tree, &config)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
struct TreeViewer;
|
||||
|
||||
impl Plugin for TreeViewer {
|
||||
fn config(&mut self) -> Result<Signature, ShellError> {
|
||||
Ok(Signature::build("tree").desc("View the contents of the pipeline as a tree."))
|
||||
}
|
||||
|
||||
fn sink(&mut self, _call_info: CallInfo, input: Vec<Value>) {
|
||||
if input.len() > 0 {
|
||||
for i in input.iter() {
|
||||
let view = TreeView::from_value(&i);
|
||||
let _ = view.render_view();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
serve_plugin(&mut TreeViewer);
|
||||
}
|
28
src/utils.rs
28
src/utils.rs
@ -1,35 +1,9 @@
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{PathMember, UnspannedPathMember, UntaggedValue, Value};
|
||||
use nu_protocol::{UntaggedValue, Value};
|
||||
use nu_source::{b, DebugDocBuilder, PrettyDebug};
|
||||
use std::ops::Div;
|
||||
use std::path::{Component, Path, PathBuf};
|
||||
|
||||
pub fn did_you_mean(obj_source: &Value, field_tried: &PathMember) -> Option<Vec<(usize, String)>> {
|
||||
let field_tried = match &field_tried.unspanned {
|
||||
UnspannedPathMember::String(string) => string.clone(),
|
||||
UnspannedPathMember::Int(int) => format!("{}", int),
|
||||
};
|
||||
|
||||
let possibilities = obj_source.data_descriptors();
|
||||
|
||||
let mut possible_matches: Vec<_> = possibilities
|
||||
.into_iter()
|
||||
.map(|x| {
|
||||
let word = x.clone();
|
||||
let distance = natural::distance::levenshtein_distance(&word, &field_tried);
|
||||
|
||||
(distance, word)
|
||||
})
|
||||
.collect();
|
||||
|
||||
if !possible_matches.is_empty() {
|
||||
possible_matches.sort();
|
||||
Some(possible_matches)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub struct AbsoluteFile {
|
||||
inner: PathBuf,
|
||||
}
|
||||
|
Reference in New Issue
Block a user