mirror of
https://github.com/nushell/nushell.git
synced 2025-06-30 22:50:14 +02:00
Move some from xxx
commands to plugin (#7942)
# Description From nushell 0.8 philosophy: https://github.com/nushell/nushell.github.io/blob/main/contributor-book/philosophy_0_80.md#core-categories > The following categories should be moved to plugins: Uncommon format support So this pr is trying to move following commands to plugin: - [X] from eml - [x] from ics - [x] from ini - [x] from vcf And we can have a new plugin handles for these formatting, currently it's implemented here: https://github.com/WindSoilder/nu_plugin_format The command usage should be the same to original command. If it's ok, the plugin can support more formats like [parquet](https://github.com/fdncred/nu_plugin_from_parquet), or [EDN format](https://github.com/nushell/nushell/issues/6415), or something else. Just create a draft pr to show what's the blueprint looks like, and is it a good direction to move forward? # User-Facing Changes _(List of all changes that impact the user experience here. This helps us keep track of breaking changes.)_ # 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.
This commit is contained in:
@ -46,14 +46,12 @@ csv = "1.1.6"
|
||||
dialoguer = { default-features = false, version = "0.10.3" }
|
||||
digest = { default-features = false, version = "0.10.0" }
|
||||
dtparse = "1.2.0"
|
||||
eml-parser = "0.1.0"
|
||||
encoding_rs = "0.8.30"
|
||||
fancy-regex = "0.11.0"
|
||||
filesize = "0.2.0"
|
||||
filetime = "0.2.15"
|
||||
fs_extra = "1.3.0"
|
||||
htmlescape = "0.3.1"
|
||||
ical = "0.8.0"
|
||||
indexmap = { version = "1.7", features = ["serde-1"] }
|
||||
indicatif = "0.17.2"
|
||||
is-root = "0.1.2"
|
||||
|
@ -328,9 +328,6 @@ pub fn create_default_context() -> EngineState {
|
||||
bind_command! {
|
||||
From,
|
||||
FromCsv,
|
||||
FromEml,
|
||||
FromIcs,
|
||||
FromIni,
|
||||
FromJson,
|
||||
FromNuon,
|
||||
FromOds,
|
||||
@ -338,7 +335,6 @@ pub fn create_default_context() -> EngineState {
|
||||
FromToml,
|
||||
FromTsv,
|
||||
FromUrl,
|
||||
FromVcf,
|
||||
FromXlsx,
|
||||
FromXml,
|
||||
FromYaml,
|
||||
|
@ -1,247 +0,0 @@
|
||||
use ::eml_parser::eml::*;
|
||||
use ::eml_parser::EmlParser;
|
||||
use indexmap::map::IndexMap;
|
||||
use nu_engine::CallExt;
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::Category;
|
||||
use nu_protocol::{
|
||||
Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Type, Value,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct FromEml;
|
||||
|
||||
const DEFAULT_BODY_PREVIEW: usize = 50;
|
||||
|
||||
impl Command for FromEml {
|
||||
fn name(&self) -> &str {
|
||||
"from eml"
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build("from eml")
|
||||
.input_output_types(vec![(Type::String, Type::Record(vec![]))])
|
||||
.named(
|
||||
"preview-body",
|
||||
SyntaxShape::Int,
|
||||
"How many bytes of the body to preview",
|
||||
Some('b'),
|
||||
)
|
||||
.category(Category::Formats)
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"Parse text as .eml and create record."
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
engine_state: &EngineState,
|
||||
stack: &mut Stack,
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let head = call.head;
|
||||
let preview_body: Option<Spanned<i64>> =
|
||||
call.get_flag(engine_state, stack, "preview-body")?;
|
||||
from_eml(input, preview_body, head)
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![
|
||||
Example {
|
||||
description: "Convert eml structured data into record",
|
||||
example: "'From: test@email.com
|
||||
Subject: Welcome
|
||||
To: someone@somewhere.com
|
||||
|
||||
Test' | from eml",
|
||||
result: Some(Value::Record {
|
||||
cols: vec![
|
||||
"Subject".to_string(),
|
||||
"From".to_string(),
|
||||
"To".to_string(),
|
||||
"Body".to_string(),
|
||||
],
|
||||
vals: vec![
|
||||
Value::test_string("Welcome"),
|
||||
Value::Record {
|
||||
cols: vec!["Name".to_string(), "Address".to_string()],
|
||||
vals: vec![
|
||||
Value::nothing(Span::test_data()),
|
||||
Value::test_string("test@email.com"),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Record {
|
||||
cols: vec!["Name".to_string(), "Address".to_string()],
|
||||
vals: vec![
|
||||
Value::nothing(Span::test_data()),
|
||||
Value::test_string("someone@somewhere.com"),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::test_string("Test"),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
},
|
||||
Example {
|
||||
description: "Convert eml structured data into record",
|
||||
example: "'From: test@email.com
|
||||
Subject: Welcome
|
||||
To: someone@somewhere.com
|
||||
|
||||
Test' | from eml -b 1",
|
||||
result: Some(Value::Record {
|
||||
cols: vec![
|
||||
"Subject".to_string(),
|
||||
"From".to_string(),
|
||||
"To".to_string(),
|
||||
"Body".to_string(),
|
||||
],
|
||||
vals: vec![
|
||||
Value::test_string("Welcome"),
|
||||
Value::Record {
|
||||
cols: vec!["Name".to_string(), "Address".to_string()],
|
||||
vals: vec![
|
||||
Value::nothing(Span::test_data()),
|
||||
Value::test_string("test@email.com"),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Record {
|
||||
cols: vec!["Name".to_string(), "Address".to_string()],
|
||||
vals: vec![
|
||||
Value::nothing(Span::test_data()),
|
||||
Value::test_string("someone@somewhere.com"),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::test_string("T"),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
fn emailaddress_to_value(span: Span, email_address: &EmailAddress) -> Value {
|
||||
let (n, a) = match email_address {
|
||||
EmailAddress::AddressOnly { address } => (
|
||||
Value::nothing(span),
|
||||
Value::String {
|
||||
val: address.to_string(),
|
||||
span,
|
||||
},
|
||||
),
|
||||
EmailAddress::NameAndEmailAddress { name, address } => (
|
||||
Value::String {
|
||||
val: name.to_string(),
|
||||
span,
|
||||
},
|
||||
Value::String {
|
||||
val: address.to_string(),
|
||||
span,
|
||||
},
|
||||
),
|
||||
};
|
||||
|
||||
Value::Record {
|
||||
cols: vec!["Name".to_string(), "Address".to_string()],
|
||||
vals: vec![n, a],
|
||||
span,
|
||||
}
|
||||
}
|
||||
|
||||
fn headerfieldvalue_to_value(head: Span, value: &HeaderFieldValue) -> Value {
|
||||
use HeaderFieldValue::*;
|
||||
|
||||
match value {
|
||||
SingleEmailAddress(address) => emailaddress_to_value(head, address),
|
||||
MultipleEmailAddresses(addresses) => Value::List {
|
||||
vals: addresses
|
||||
.iter()
|
||||
.map(|a| emailaddress_to_value(head, a))
|
||||
.collect(),
|
||||
span: head,
|
||||
},
|
||||
Unstructured(s) => Value::string(s, head),
|
||||
Empty => Value::nothing(head),
|
||||
}
|
||||
}
|
||||
|
||||
fn from_eml(
|
||||
input: PipelineData,
|
||||
preview_body: Option<Spanned<i64>>,
|
||||
head: Span,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let (value, _span, metadata, ..) = input.collect_string_strict(head)?;
|
||||
|
||||
let body_preview = preview_body
|
||||
.map(|b| b.item as usize)
|
||||
.unwrap_or(DEFAULT_BODY_PREVIEW);
|
||||
|
||||
let eml = EmlParser::from_string(value)
|
||||
.with_body_preview(body_preview)
|
||||
.parse()
|
||||
.map_err(|_| {
|
||||
ShellError::CantConvert("structured eml data".into(), "string".into(), head, None)
|
||||
})?;
|
||||
|
||||
let mut collected = IndexMap::new();
|
||||
|
||||
if let Some(subj) = eml.subject {
|
||||
collected.insert(
|
||||
"Subject".to_string(),
|
||||
Value::String {
|
||||
val: subj,
|
||||
span: head,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
if let Some(from) = eml.from {
|
||||
collected.insert("From".to_string(), headerfieldvalue_to_value(head, &from));
|
||||
}
|
||||
|
||||
if let Some(to) = eml.to {
|
||||
collected.insert("To".to_string(), headerfieldvalue_to_value(head, &to));
|
||||
}
|
||||
|
||||
for HeaderField { name, value } in &eml.headers {
|
||||
collected.insert(name.to_string(), headerfieldvalue_to_value(head, value));
|
||||
}
|
||||
|
||||
if let Some(body) = eml.body {
|
||||
collected.insert(
|
||||
"Body".to_string(),
|
||||
Value::String {
|
||||
val: body,
|
||||
span: head,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Ok(PipelineData::Value(
|
||||
Value::from(Spanned {
|
||||
item: collected,
|
||||
span: head,
|
||||
}),
|
||||
metadata,
|
||||
))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_examples() {
|
||||
use crate::test_examples;
|
||||
|
||||
test_examples(FromEml {})
|
||||
}
|
||||
}
|
@ -1,337 +0,0 @@
|
||||
extern crate ical;
|
||||
use ical::parser::ical::component::*;
|
||||
use ical::property::Property;
|
||||
use indexmap::map::IndexMap;
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{
|
||||
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Spanned, Type,
|
||||
Value,
|
||||
};
|
||||
use std::io::BufReader;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct FromIcs;
|
||||
|
||||
impl Command for FromIcs {
|
||||
fn name(&self) -> &str {
|
||||
"from ics"
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build("from ics")
|
||||
.input_output_types(vec![(Type::String, Type::Table(vec![]))])
|
||||
.category(Category::Formats)
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"Parse text as .ics and create table."
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
_engine_state: &EngineState,
|
||||
_stack: &mut Stack,
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let head = call.head;
|
||||
from_ics(input, head)
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![Example {
|
||||
example: "'BEGIN:VCALENDAR
|
||||
END:VCALENDAR' | from ics",
|
||||
description: "Converts ics formatted string to table",
|
||||
result: Some(Value::List {
|
||||
vals: vec![Value::Record {
|
||||
cols: vec![
|
||||
"properties".to_string(),
|
||||
"events".to_string(),
|
||||
"alarms".to_string(),
|
||||
"to-Dos".to_string(),
|
||||
"journals".to_string(),
|
||||
"free-busys".to_string(),
|
||||
"timezones".to_string(),
|
||||
],
|
||||
vals: vec![
|
||||
Value::List {
|
||||
vals: vec![],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::List {
|
||||
vals: vec![],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::List {
|
||||
vals: vec![],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::List {
|
||||
vals: vec![],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::List {
|
||||
vals: vec![],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::List {
|
||||
vals: vec![],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::List {
|
||||
vals: vec![],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
],
|
||||
span: Span::test_data(),
|
||||
}],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
}]
|
||||
}
|
||||
}
|
||||
|
||||
fn from_ics(input: PipelineData, head: Span) -> Result<PipelineData, ShellError> {
|
||||
let (input_string, span, metadata) = input.collect_string_strict(head)?;
|
||||
|
||||
let input_string = input_string
|
||||
.lines()
|
||||
.map(|x| x.trim().to_string())
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
|
||||
let input_bytes = input_string.as_bytes();
|
||||
let buf_reader = BufReader::new(input_bytes);
|
||||
let parser = ical::IcalParser::new(buf_reader);
|
||||
|
||||
let mut output = vec![];
|
||||
|
||||
for calendar in parser {
|
||||
match calendar {
|
||||
Ok(c) => output.push(calendar_to_value(c, head)),
|
||||
Err(e) => output.push(Value::Error {
|
||||
error: ShellError::UnsupportedInput(
|
||||
format!("input cannot be parsed as .ics ({e})"),
|
||||
"value originates from here".into(),
|
||||
head,
|
||||
span,
|
||||
),
|
||||
}),
|
||||
}
|
||||
}
|
||||
Ok(Value::List {
|
||||
vals: output,
|
||||
span: head,
|
||||
}
|
||||
.into_pipeline_data_with_metadata(metadata))
|
||||
}
|
||||
|
||||
fn calendar_to_value(calendar: IcalCalendar, span: Span) -> Value {
|
||||
let mut row = IndexMap::new();
|
||||
|
||||
row.insert(
|
||||
"properties".to_string(),
|
||||
properties_to_value(calendar.properties, span),
|
||||
);
|
||||
row.insert("events".to_string(), events_to_value(calendar.events, span));
|
||||
row.insert("alarms".to_string(), alarms_to_value(calendar.alarms, span));
|
||||
row.insert("to-Dos".to_string(), todos_to_value(calendar.todos, span));
|
||||
row.insert(
|
||||
"journals".to_string(),
|
||||
journals_to_value(calendar.journals, span),
|
||||
);
|
||||
row.insert(
|
||||
"free-busys".to_string(),
|
||||
free_busys_to_value(calendar.free_busys, span),
|
||||
);
|
||||
row.insert(
|
||||
"timezones".to_string(),
|
||||
timezones_to_value(calendar.timezones, span),
|
||||
);
|
||||
|
||||
Value::from(Spanned { item: row, span })
|
||||
}
|
||||
|
||||
fn events_to_value(events: Vec<IcalEvent>, span: Span) -> Value {
|
||||
Value::List {
|
||||
vals: events
|
||||
.into_iter()
|
||||
.map(|event| {
|
||||
let mut row = IndexMap::new();
|
||||
row.insert(
|
||||
"properties".to_string(),
|
||||
properties_to_value(event.properties, span),
|
||||
);
|
||||
row.insert("alarms".to_string(), alarms_to_value(event.alarms, span));
|
||||
Value::from(Spanned { item: row, span })
|
||||
})
|
||||
.collect::<Vec<Value>>(),
|
||||
span,
|
||||
}
|
||||
}
|
||||
|
||||
fn alarms_to_value(alarms: Vec<IcalAlarm>, span: Span) -> Value {
|
||||
Value::List {
|
||||
vals: alarms
|
||||
.into_iter()
|
||||
.map(|alarm| {
|
||||
let mut row = IndexMap::new();
|
||||
row.insert(
|
||||
"properties".to_string(),
|
||||
properties_to_value(alarm.properties, span),
|
||||
);
|
||||
Value::from(Spanned { item: row, span })
|
||||
})
|
||||
.collect::<Vec<Value>>(),
|
||||
span,
|
||||
}
|
||||
}
|
||||
|
||||
fn todos_to_value(todos: Vec<IcalTodo>, span: Span) -> Value {
|
||||
Value::List {
|
||||
vals: todos
|
||||
.into_iter()
|
||||
.map(|todo| {
|
||||
let mut row = IndexMap::new();
|
||||
row.insert(
|
||||
"properties".to_string(),
|
||||
properties_to_value(todo.properties, span),
|
||||
);
|
||||
row.insert("alarms".to_string(), alarms_to_value(todo.alarms, span));
|
||||
Value::from(Spanned { item: row, span })
|
||||
})
|
||||
.collect::<Vec<Value>>(),
|
||||
span,
|
||||
}
|
||||
}
|
||||
|
||||
fn journals_to_value(journals: Vec<IcalJournal>, span: Span) -> Value {
|
||||
Value::List {
|
||||
vals: journals
|
||||
.into_iter()
|
||||
.map(|journal| {
|
||||
let mut row = IndexMap::new();
|
||||
row.insert(
|
||||
"properties".to_string(),
|
||||
properties_to_value(journal.properties, span),
|
||||
);
|
||||
Value::from(Spanned { item: row, span })
|
||||
})
|
||||
.collect::<Vec<Value>>(),
|
||||
span,
|
||||
}
|
||||
}
|
||||
|
||||
fn free_busys_to_value(free_busys: Vec<IcalFreeBusy>, span: Span) -> Value {
|
||||
Value::List {
|
||||
vals: free_busys
|
||||
.into_iter()
|
||||
.map(|free_busy| {
|
||||
let mut row = IndexMap::new();
|
||||
row.insert(
|
||||
"properties".to_string(),
|
||||
properties_to_value(free_busy.properties, span),
|
||||
);
|
||||
Value::from(Spanned { item: row, span })
|
||||
})
|
||||
.collect::<Vec<Value>>(),
|
||||
span,
|
||||
}
|
||||
}
|
||||
|
||||
fn timezones_to_value(timezones: Vec<IcalTimeZone>, span: Span) -> Value {
|
||||
Value::List {
|
||||
vals: timezones
|
||||
.into_iter()
|
||||
.map(|timezone| {
|
||||
let mut row = IndexMap::new();
|
||||
row.insert(
|
||||
"properties".to_string(),
|
||||
properties_to_value(timezone.properties, span),
|
||||
);
|
||||
row.insert(
|
||||
"transitions".to_string(),
|
||||
timezone_transitions_to_value(timezone.transitions, span),
|
||||
);
|
||||
Value::from(Spanned { item: row, span })
|
||||
})
|
||||
.collect::<Vec<Value>>(),
|
||||
span,
|
||||
}
|
||||
}
|
||||
|
||||
fn timezone_transitions_to_value(transitions: Vec<IcalTimeZoneTransition>, span: Span) -> Value {
|
||||
Value::List {
|
||||
vals: transitions
|
||||
.into_iter()
|
||||
.map(|transition| {
|
||||
let mut row = IndexMap::new();
|
||||
row.insert(
|
||||
"properties".to_string(),
|
||||
properties_to_value(transition.properties, span),
|
||||
);
|
||||
Value::from(Spanned { item: row, span })
|
||||
})
|
||||
.collect::<Vec<Value>>(),
|
||||
span,
|
||||
}
|
||||
}
|
||||
|
||||
fn properties_to_value(properties: Vec<Property>, span: Span) -> Value {
|
||||
Value::List {
|
||||
vals: properties
|
||||
.into_iter()
|
||||
.map(|prop| {
|
||||
let mut row = IndexMap::new();
|
||||
|
||||
let name = Value::String {
|
||||
val: prop.name,
|
||||
span,
|
||||
};
|
||||
let value = match prop.value {
|
||||
Some(val) => Value::String { val, span },
|
||||
None => Value::nothing(span),
|
||||
};
|
||||
let params = match prop.params {
|
||||
Some(param_list) => params_to_value(param_list, span),
|
||||
None => Value::nothing(span),
|
||||
};
|
||||
|
||||
row.insert("name".to_string(), name);
|
||||
row.insert("value".to_string(), value);
|
||||
row.insert("params".to_string(), params);
|
||||
Value::from(Spanned { item: row, span })
|
||||
})
|
||||
.collect::<Vec<Value>>(),
|
||||
span,
|
||||
}
|
||||
}
|
||||
|
||||
fn params_to_value(params: Vec<(String, Vec<String>)>, span: Span) -> Value {
|
||||
let mut row = IndexMap::new();
|
||||
|
||||
for (param_name, param_values) in params {
|
||||
let values: Vec<Value> = param_values
|
||||
.into_iter()
|
||||
.map(|val| Value::string(val, span))
|
||||
.collect();
|
||||
let values = Value::List { vals: values, span };
|
||||
row.insert(param_name, values);
|
||||
}
|
||||
|
||||
Value::from(Spanned { item: row, span })
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_examples() {
|
||||
use crate::test_examples;
|
||||
|
||||
test_examples(FromIcs {})
|
||||
}
|
||||
}
|
@ -1,157 +0,0 @@
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{
|
||||
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Type, Value,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct FromIni;
|
||||
|
||||
impl Command for FromIni {
|
||||
fn name(&self) -> &str {
|
||||
"from ini"
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build("from ini")
|
||||
.input_output_types(vec![(Type::String, Type::Record(vec![]))])
|
||||
.category(Category::Formats)
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"Parse text as .ini and create record"
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![Example {
|
||||
example: "'[foo]
|
||||
a=1
|
||||
b=2' | from ini",
|
||||
description: "Converts ini formatted string to record",
|
||||
result: Some(Value::Record {
|
||||
cols: vec!["foo".to_string()],
|
||||
vals: vec![Value::Record {
|
||||
cols: vec!["a".to_string(), "b".to_string()],
|
||||
vals: vec![Value::test_string("1"), Value::test_string("2")],
|
||||
span: Span::test_data(),
|
||||
}],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
}]
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
_engine_state: &EngineState,
|
||||
_stack: &mut Stack,
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let head = call.head;
|
||||
from_ini(input, head)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_ini_string_to_value(
|
||||
s: String,
|
||||
span: Span,
|
||||
val_span: Span,
|
||||
) -> Result<Value, ShellError> {
|
||||
let ini_config: Result<ini::Ini, ini::ParseError> = ini::Ini::load_from_str(&s);
|
||||
|
||||
match ini_config {
|
||||
Ok(config) => {
|
||||
let mut sections: Vec<String> = Vec::new();
|
||||
let mut sections_key_value_pairs: Vec<Value> = Vec::new();
|
||||
|
||||
for (section, properties) in config.iter() {
|
||||
let mut keys_for_section: Vec<String> = Vec::new();
|
||||
let mut values_for_section: Vec<Value> = Vec::new();
|
||||
|
||||
// section
|
||||
match section {
|
||||
Some(section_name) => {
|
||||
sections.push(section_name.to_owned());
|
||||
}
|
||||
None => {
|
||||
sections.push(String::new());
|
||||
}
|
||||
}
|
||||
|
||||
// section's key value pairs
|
||||
for (key, value) in properties.iter() {
|
||||
keys_for_section.push(key.to_owned());
|
||||
values_for_section.push(Value::String {
|
||||
val: value.to_owned(),
|
||||
span,
|
||||
});
|
||||
}
|
||||
|
||||
// section with its key value pairs
|
||||
sections_key_value_pairs.push(Value::Record {
|
||||
cols: keys_for_section,
|
||||
vals: values_for_section,
|
||||
span,
|
||||
});
|
||||
}
|
||||
|
||||
// all sections with all its key value pairs
|
||||
Ok(Value::Record {
|
||||
cols: sections,
|
||||
vals: sections_key_value_pairs,
|
||||
span,
|
||||
})
|
||||
}
|
||||
Err(err) => Err(ShellError::UnsupportedInput(
|
||||
format!("Could not load ini: {err}"),
|
||||
"value originates from here".into(),
|
||||
span,
|
||||
val_span,
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
fn from_ini(input: PipelineData, head: Span) -> Result<PipelineData, ShellError> {
|
||||
let (concat_string, span, metadata) = input.collect_string_strict(head)?;
|
||||
|
||||
match from_ini_string_to_value(concat_string, head, span) {
|
||||
Ok(x) => Ok(x.into_pipeline_data_with_metadata(metadata)),
|
||||
Err(other) => Err(other),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_examples() {
|
||||
use crate::test_examples;
|
||||
|
||||
test_examples(FromIni {})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn read_ini_config_passes() {
|
||||
let ini_test_config = r"
|
||||
min-width=450
|
||||
max-width=820
|
||||
|
||||
[normal]
|
||||
sound-file=/usr/share/sounds/freedesktop/stereo/dialog-information.oga
|
||||
|
||||
[critical]
|
||||
border-color=FAB387ff
|
||||
default-timeout=20
|
||||
sound-file=/usr/share/sounds/freedesktop/stereo/dialog-warning.oga
|
||||
";
|
||||
|
||||
let result = from_ini_string_to_value(
|
||||
ini_test_config.to_owned(),
|
||||
Span::test_data(),
|
||||
Span::test_data(),
|
||||
);
|
||||
|
||||
assert!(result.is_ok());
|
||||
}
|
||||
}
|
@ -1,9 +1,6 @@
|
||||
mod command;
|
||||
mod csv;
|
||||
mod delimited;
|
||||
mod eml;
|
||||
mod ics;
|
||||
mod ini;
|
||||
mod json;
|
||||
mod nuon;
|
||||
mod ods;
|
||||
@ -11,7 +8,6 @@ mod ssv;
|
||||
mod toml;
|
||||
mod tsv;
|
||||
mod url;
|
||||
mod vcf;
|
||||
mod xlsx;
|
||||
mod xml;
|
||||
mod yaml;
|
||||
@ -19,16 +15,12 @@ mod yaml;
|
||||
pub use self::csv::FromCsv;
|
||||
pub use self::toml::FromToml;
|
||||
pub use self::url::FromUrl;
|
||||
pub use crate::formats::from::ini::FromIni;
|
||||
pub use command::From;
|
||||
pub use eml::FromEml;
|
||||
pub use ics::FromIcs;
|
||||
pub use json::FromJson;
|
||||
pub use nuon::FromNuon;
|
||||
pub use ods::FromOds;
|
||||
pub use ssv::FromSsv;
|
||||
pub use tsv::FromTsv;
|
||||
pub use vcf::FromVcf;
|
||||
pub use xlsx::FromXlsx;
|
||||
pub use xml::FromXml;
|
||||
pub use yaml::FromYaml;
|
||||
|
@ -1,206 +0,0 @@
|
||||
use ical::parser::vcard::component::*;
|
||||
use ical::property::Property;
|
||||
use indexmap::map::IndexMap;
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{
|
||||
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Spanned, Type,
|
||||
Value,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct FromVcf;
|
||||
|
||||
impl Command for FromVcf {
|
||||
fn name(&self) -> &str {
|
||||
"from vcf"
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build("from vcf")
|
||||
.input_output_types(vec![(Type::String, Type::Table(vec![]))])
|
||||
.category(Category::Formats)
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"Parse text as .vcf and create table."
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
_engine_state: &EngineState,
|
||||
_stack: &mut Stack,
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let head = call.head;
|
||||
from_vcf(input, head)
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![Example {
|
||||
example: "'BEGIN:VCARD
|
||||
N:Foo
|
||||
FN:Bar
|
||||
EMAIL:foo@bar.com
|
||||
END:VCARD' | from vcf",
|
||||
description: "Converts ics formatted string to table",
|
||||
result: Some(Value::List {
|
||||
vals: vec![Value::Record {
|
||||
cols: vec!["properties".to_string()],
|
||||
vals: vec![Value::List {
|
||||
vals: vec![
|
||||
Value::Record {
|
||||
cols: vec![
|
||||
"name".to_string(),
|
||||
"value".to_string(),
|
||||
"params".to_string(),
|
||||
],
|
||||
vals: vec![
|
||||
Value::test_string("N"),
|
||||
Value::test_string("Foo"),
|
||||
Value::Nothing {
|
||||
span: Span::test_data(),
|
||||
},
|
||||
],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Record {
|
||||
cols: vec![
|
||||
"name".to_string(),
|
||||
"value".to_string(),
|
||||
"params".to_string(),
|
||||
],
|
||||
vals: vec![
|
||||
Value::test_string("FN"),
|
||||
Value::test_string("Bar"),
|
||||
Value::Nothing {
|
||||
span: Span::test_data(),
|
||||
},
|
||||
],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Record {
|
||||
cols: vec![
|
||||
"name".to_string(),
|
||||
"value".to_string(),
|
||||
"params".to_string(),
|
||||
],
|
||||
vals: vec![
|
||||
Value::test_string("EMAIL"),
|
||||
Value::test_string("foo@bar.com"),
|
||||
Value::Nothing {
|
||||
span: Span::test_data(),
|
||||
},
|
||||
],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
],
|
||||
span: Span::test_data(),
|
||||
}],
|
||||
span: Span::test_data(),
|
||||
}],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
}]
|
||||
}
|
||||
}
|
||||
|
||||
fn from_vcf(input: PipelineData, head: Span) -> Result<PipelineData, ShellError> {
|
||||
let (input_string, span, metadata) = input.collect_string_strict(head)?;
|
||||
|
||||
let input_string = input_string
|
||||
.lines()
|
||||
.map(|x| x.trim().to_string())
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
|
||||
let input_bytes = input_string.as_bytes();
|
||||
let cursor = std::io::Cursor::new(input_bytes);
|
||||
let parser = ical::VcardParser::new(cursor);
|
||||
|
||||
let iter = parser.map(move |contact| match contact {
|
||||
Ok(c) => contact_to_value(c, head),
|
||||
Err(e) => Value::Error {
|
||||
error: ShellError::UnsupportedInput(
|
||||
format!("input cannot be parsed as .vcf ({e})"),
|
||||
"value originates from here".into(),
|
||||
head,
|
||||
span,
|
||||
),
|
||||
},
|
||||
});
|
||||
|
||||
let collected: Vec<_> = iter.collect();
|
||||
Ok(Value::List {
|
||||
vals: collected,
|
||||
span: head,
|
||||
}
|
||||
.into_pipeline_data_with_metadata(metadata))
|
||||
}
|
||||
|
||||
fn contact_to_value(contact: VcardContact, span: Span) -> Value {
|
||||
let mut row = IndexMap::new();
|
||||
row.insert(
|
||||
"properties".to_string(),
|
||||
properties_to_value(contact.properties, span),
|
||||
);
|
||||
Value::from(Spanned { item: row, span })
|
||||
}
|
||||
|
||||
fn properties_to_value(properties: Vec<Property>, span: Span) -> Value {
|
||||
Value::List {
|
||||
vals: properties
|
||||
.into_iter()
|
||||
.map(|prop| {
|
||||
let mut row = IndexMap::new();
|
||||
|
||||
let name = Value::String {
|
||||
val: prop.name,
|
||||
span,
|
||||
};
|
||||
let value = match prop.value {
|
||||
Some(val) => Value::String { val, span },
|
||||
None => Value::Nothing { span },
|
||||
};
|
||||
let params = match prop.params {
|
||||
Some(param_list) => params_to_value(param_list, span),
|
||||
None => Value::Nothing { span },
|
||||
};
|
||||
|
||||
row.insert("name".to_string(), name);
|
||||
row.insert("value".to_string(), value);
|
||||
row.insert("params".to_string(), params);
|
||||
Value::from(Spanned { item: row, span })
|
||||
})
|
||||
.collect::<Vec<Value>>(),
|
||||
span,
|
||||
}
|
||||
}
|
||||
|
||||
fn params_to_value(params: Vec<(String, Vec<String>)>, span: Span) -> Value {
|
||||
let mut row = IndexMap::new();
|
||||
|
||||
for (param_name, param_values) in params {
|
||||
let values: Vec<Value> = param_values
|
||||
.into_iter()
|
||||
.map(|val| Value::string(val, span))
|
||||
.collect();
|
||||
let values = Value::List { vals: values, span };
|
||||
row.insert(param_name, values);
|
||||
}
|
||||
|
||||
Value::from(Spanned { item: row, span })
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_examples() {
|
||||
use crate::test_examples;
|
||||
|
||||
test_examples(FromVcf {})
|
||||
}
|
||||
}
|
@ -188,26 +188,6 @@ fn parses_xml() {
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_ini() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats",
|
||||
"open sample.ini | get SectionOne.integer"
|
||||
);
|
||||
|
||||
assert_eq!(actual.out, "1234")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_utf16_ini() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats",
|
||||
"open ./utf16.ini --raw | decode utf-16 | from ini | rename info | get info | get IconIndex"
|
||||
);
|
||||
|
||||
assert_eq!(actual.out, "-236")
|
||||
}
|
||||
|
||||
#[cfg(feature = "dataframe")]
|
||||
#[test]
|
||||
fn parses_arrow_ipc() {
|
||||
|
@ -1,93 +0,0 @@
|
||||
use nu_test_support::{nu, pipeline};
|
||||
|
||||
const TEST_CWD: &str = "tests/fixtures/formats";
|
||||
|
||||
// The To field in this email is just "to@example.com", which gets parsed out as the Address. The Name is empty.
|
||||
#[test]
|
||||
fn from_eml_get_to_field() {
|
||||
let actual = nu!(
|
||||
cwd: TEST_CWD,
|
||||
pipeline(
|
||||
r#"
|
||||
open sample.eml
|
||||
| get To
|
||||
| get Address
|
||||
"#
|
||||
)
|
||||
);
|
||||
|
||||
assert_eq!(actual.out, "to@example.com");
|
||||
|
||||
let actual = nu!(
|
||||
cwd: TEST_CWD,
|
||||
pipeline(
|
||||
r#"
|
||||
open sample.eml
|
||||
| get To
|
||||
| get Name
|
||||
"#
|
||||
)
|
||||
);
|
||||
|
||||
assert_eq!(actual.out, "");
|
||||
}
|
||||
|
||||
// The Reply-To field in this email is "replyto@example.com" <replyto@example.com>, meaning both the Name and Address values are identical.
|
||||
#[test]
|
||||
fn from_eml_get_replyto_field() {
|
||||
let actual = nu!(
|
||||
cwd: TEST_CWD,
|
||||
pipeline(
|
||||
r#"
|
||||
open sample.eml
|
||||
| get Reply-To
|
||||
| get Address
|
||||
"#
|
||||
)
|
||||
);
|
||||
|
||||
assert_eq!(actual.out, "replyto@example.com");
|
||||
|
||||
let actual = nu!(
|
||||
cwd: TEST_CWD,
|
||||
pipeline(
|
||||
r#"
|
||||
open sample.eml
|
||||
| get Reply-To
|
||||
| get Name
|
||||
"#
|
||||
)
|
||||
);
|
||||
|
||||
assert_eq!(actual.out, "replyto@example.com");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn from_eml_get_subject_field() {
|
||||
let actual = nu!(
|
||||
cwd: TEST_CWD,
|
||||
pipeline(
|
||||
r#"
|
||||
open sample.eml
|
||||
| get Subject
|
||||
"#
|
||||
)
|
||||
);
|
||||
|
||||
assert_eq!(actual.out, "Test Message");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn from_eml_get_another_header_field() {
|
||||
let actual = nu!(
|
||||
cwd: TEST_CWD,
|
||||
pipeline(
|
||||
r#"
|
||||
open sample.eml
|
||||
| get MIME-Version
|
||||
"#
|
||||
)
|
||||
);
|
||||
|
||||
assert_eq!(actual.out, "1.0");
|
||||
}
|
@ -1,99 +0,0 @@
|
||||
use nu_test_support::fs::Stub::FileWithContentToBeTrimmed;
|
||||
use nu_test_support::playground::Playground;
|
||||
use nu_test_support::{nu, pipeline};
|
||||
|
||||
#[test]
|
||||
fn infers_types() {
|
||||
Playground::setup("filter_from_ics_test_1", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"calendar.ics",
|
||||
r#"
|
||||
BEGIN:VCALENDAR
|
||||
PRODID:-//Google Inc//Google Calendar 70.9054//EN
|
||||
VERSION:2.0
|
||||
BEGIN:VEVENT
|
||||
DTSTART:20171007T200000Z
|
||||
DTEND:20171007T233000Z
|
||||
DTSTAMP:20200319T182138Z
|
||||
UID:4l80f6dcovnriq38g57g07btid@google.com
|
||||
CREATED:20170719T202915Z
|
||||
DESCRIPTION:
|
||||
LAST-MODIFIED:20170930T190808Z
|
||||
LOCATION:
|
||||
SEQUENCE:1
|
||||
STATUS:CONFIRMED
|
||||
SUMMARY:Maryland Game
|
||||
TRANSP:TRANSPARENT
|
||||
END:VEVENT
|
||||
BEGIN:VEVENT
|
||||
DTSTART:20171002T010000Z
|
||||
DTEND:20171002T020000Z
|
||||
DTSTAMP:20200319T182138Z
|
||||
UID:2v61g7mij4s7ieoubm3sjpun5d@google.com
|
||||
CREATED:20171001T180103Z
|
||||
DESCRIPTION:
|
||||
LAST-MODIFIED:20171001T180103Z
|
||||
LOCATION:
|
||||
SEQUENCE:0
|
||||
STATUS:CONFIRMED
|
||||
SUMMARY:Halloween Wars
|
||||
TRANSP:OPAQUE
|
||||
END:VEVENT
|
||||
END:VCALENDAR
|
||||
"#,
|
||||
)]);
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
open calendar.ics
|
||||
| get events.0
|
||||
| length
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual.out, "2");
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn from_ics_text_to_table() {
|
||||
Playground::setup("filter_from_ics_test_2", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"calendar.txt",
|
||||
r#"
|
||||
BEGIN:VCALENDAR
|
||||
BEGIN:VEVENT
|
||||
DTSTART:20171007T200000Z
|
||||
DTEND:20171007T233000Z
|
||||
DTSTAMP:20200319T182138Z
|
||||
UID:4l80f6dcovnriq38g57g07btid@google.com
|
||||
CREATED:20170719T202915Z
|
||||
DESCRIPTION:
|
||||
LAST-MODIFIED:20170930T190808Z
|
||||
LOCATION:
|
||||
SEQUENCE:1
|
||||
STATUS:CONFIRMED
|
||||
SUMMARY:Maryland Game
|
||||
TRANSP:TRANSPARENT
|
||||
END:VEVENT
|
||||
END:VCALENDAR
|
||||
"#,
|
||||
)]);
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
open calendar.txt
|
||||
| from ics
|
||||
| get events.0
|
||||
| get properties.0
|
||||
| where name == "SUMMARY"
|
||||
| first
|
||||
| get value
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual.out, "Maryland Game");
|
||||
})
|
||||
}
|
@ -1,8 +1,6 @@
|
||||
mod bson;
|
||||
mod csv;
|
||||
mod eml;
|
||||
mod html;
|
||||
mod ics;
|
||||
mod json;
|
||||
mod markdown;
|
||||
mod nuon;
|
||||
@ -11,7 +9,6 @@ mod ssv;
|
||||
mod toml;
|
||||
mod tsv;
|
||||
mod url;
|
||||
mod vcf;
|
||||
mod xlsx;
|
||||
mod xml;
|
||||
mod yaml;
|
||||
|
@ -1,82 +0,0 @@
|
||||
use nu_test_support::fs::Stub::FileWithContentToBeTrimmed;
|
||||
use nu_test_support::playground::Playground;
|
||||
use nu_test_support::{nu, pipeline};
|
||||
|
||||
#[test]
|
||||
fn infers_types() {
|
||||
Playground::setup("filter_from_vcf_test_1", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"contacts.vcf",
|
||||
r#"
|
||||
BEGIN:VCARD
|
||||
VERSION:3.0
|
||||
FN:John Doe
|
||||
N:Doe;John;;;
|
||||
EMAIL;TYPE=INTERNET:john.doe99@gmail.com
|
||||
item1.ORG:'Alpine Ski Resort'
|
||||
item1.X-ABLabel:Other
|
||||
item2.TITLE:'Ski Instructor'
|
||||
item2.X-ABLabel:Other
|
||||
BDAY:19001106
|
||||
NOTE:Facebook: john.doe.3\nWebsite: \nHometown: Cleveland\, Ohio
|
||||
CATEGORIES:myContacts
|
||||
END:VCARD
|
||||
BEGIN:VCARD
|
||||
VERSION:3.0
|
||||
FN:Alex Smith
|
||||
N:Smith;Alex;;;
|
||||
TEL;TYPE=CELL:(890) 123-4567
|
||||
CATEGORIES:Band,myContacts
|
||||
END:VCARD
|
||||
"#,
|
||||
)]);
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
open contacts.vcf
|
||||
| length
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual.out, "2");
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn from_vcf_text_to_table() {
|
||||
Playground::setup("filter_from_vcf_test_2", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"contacts.txt",
|
||||
r#"
|
||||
BEGIN:VCARD
|
||||
VERSION:3.0
|
||||
FN:John Doe
|
||||
N:Doe;John;;;
|
||||
EMAIL;TYPE=INTERNET:john.doe99@gmail.com
|
||||
item1.ORG:'Alpine Ski Resort'
|
||||
item1.X-ABLabel:Other
|
||||
item2.TITLE:'Ski Instructor'
|
||||
item2.X-ABLabel:Other
|
||||
BDAY:19001106
|
||||
NOTE:Facebook: john.doe.3\nWebsite: \nHometown: Cleveland\, Ohio
|
||||
CATEGORIES:myContacts
|
||||
END:VCARD
|
||||
"#,
|
||||
)]);
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
open contacts.txt
|
||||
| from vcf
|
||||
| get properties.0
|
||||
| where name == "EMAIL"
|
||||
| first
|
||||
| get value
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual.out, "john.doe99@gmail.com");
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user