mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 06:45:59 +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:
190
crates/nu_plugin_formats/src/from/eml.rs
Normal file
190
crates/nu_plugin_formats/src/from/eml.rs
Normal file
@ -0,0 +1,190 @@
|
||||
use eml_parser::eml::*;
|
||||
use eml_parser::EmlParser;
|
||||
use indexmap::map::IndexMap;
|
||||
use nu_plugin::{EvaluatedCall, LabeledError};
|
||||
use nu_protocol::{PluginExample, ShellError, Span, Spanned, Value};
|
||||
|
||||
const DEFAULT_BODY_PREVIEW: usize = 50;
|
||||
pub const CMD_NAME: &str = "from eml";
|
||||
|
||||
pub fn from_eml_call(call: &EvaluatedCall, input: &Value) -> Result<Value, LabeledError> {
|
||||
let preview_body: usize = call
|
||||
.get_flag::<i64>("preview-body")?
|
||||
.map(|l| if l < 0 { 0 } else { l as usize })
|
||||
.unwrap_or(DEFAULT_BODY_PREVIEW);
|
||||
from_eml(input, preview_body, call.head)
|
||||
}
|
||||
|
||||
pub fn examples() -> Vec<PluginExample> {
|
||||
vec![
|
||||
PluginExample {
|
||||
description: "Convert eml structured data into record".into(),
|
||||
example: "'From: test@email.com
|
||||
Subject: Welcome
|
||||
To: someone@somewhere.com
|
||||
Test' | from eml"
|
||||
.into(),
|
||||
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(),
|
||||
}),
|
||||
},
|
||||
PluginExample {
|
||||
description: "Convert eml structured data into record".into(),
|
||||
example: "'From: test@email.com
|
||||
Subject: Welcome
|
||||
To: someone@somewhere.com
|
||||
Test' | from eml -b 1"
|
||||
.into(),
|
||||
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: &Value, body_preview: usize, head: Span) -> Result<Value, LabeledError> {
|
||||
let value = input.as_string()?;
|
||||
|
||||
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(Value::from(Spanned {
|
||||
item: collected,
|
||||
span: head,
|
||||
}))
|
||||
}
|
294
crates/nu_plugin_formats/src/from/ics.rs
Normal file
294
crates/nu_plugin_formats/src/from/ics.rs
Normal file
@ -0,0 +1,294 @@
|
||||
use ical::parser::ical::component::*;
|
||||
use ical::property::Property;
|
||||
use indexmap::map::IndexMap;
|
||||
use nu_plugin::{EvaluatedCall, LabeledError};
|
||||
use nu_protocol::{PluginExample, ShellError, Span, Spanned, Value};
|
||||
use std::io::BufReader;
|
||||
|
||||
pub const CMD_NAME: &str = "from ics";
|
||||
|
||||
pub fn from_ics_call(call: &EvaluatedCall, input: &Value) -> Result<Value, LabeledError> {
|
||||
let span = input.span().unwrap_or(call.head);
|
||||
let input_string = input.as_string()?;
|
||||
let head = call.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,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn examples() -> Vec<PluginExample> {
|
||||
vec![PluginExample {
|
||||
example: "'BEGIN:VCALENDAR
|
||||
END:VCALENDAR' | from ics"
|
||||
.into(),
|
||||
description: "Converts ics formatted string to table".into(),
|
||||
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 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 })
|
||||
}
|
82
crates/nu_plugin_formats/src/from/ini.rs
Normal file
82
crates/nu_plugin_formats/src/from/ini.rs
Normal file
@ -0,0 +1,82 @@
|
||||
use nu_plugin::{EvaluatedCall, LabeledError};
|
||||
use nu_protocol::{PluginExample, ShellError, Span, Value};
|
||||
|
||||
pub const CMD_NAME: &str = "from ini";
|
||||
|
||||
pub fn from_ini_call(call: &EvaluatedCall, input: &Value) -> Result<Value, LabeledError> {
|
||||
let span = input.span().unwrap_or(call.head);
|
||||
let input_string = input.as_string()?;
|
||||
let head = call.head;
|
||||
|
||||
let ini_config: Result<ini::Ini, ini::ParseError> = ini::Ini::load_from_str(&input_string);
|
||||
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(),
|
||||
head,
|
||||
span,
|
||||
)
|
||||
.into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn examples() -> Vec<PluginExample> {
|
||||
vec![PluginExample {
|
||||
example: "'[foo]
|
||||
a=1
|
||||
b=2' | from ini"
|
||||
.into(),
|
||||
description: "Converts ini formatted string to record".into(),
|
||||
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(),
|
||||
}),
|
||||
}]
|
||||
}
|
4
crates/nu_plugin_formats/src/from/mod.rs
Normal file
4
crates/nu_plugin_formats/src/from/mod.rs
Normal file
@ -0,0 +1,4 @@
|
||||
pub mod eml;
|
||||
pub mod ics;
|
||||
pub mod ini;
|
||||
pub mod vcf;
|
164
crates/nu_plugin_formats/src/from/vcf.rs
Normal file
164
crates/nu_plugin_formats/src/from/vcf.rs
Normal file
@ -0,0 +1,164 @@
|
||||
use ical::parser::vcard::component::*;
|
||||
use ical::property::Property;
|
||||
use indexmap::map::IndexMap;
|
||||
use nu_plugin::{EvaluatedCall, LabeledError};
|
||||
use nu_protocol::{PluginExample, ShellError, Span, Spanned, Value};
|
||||
|
||||
pub const CMD_NAME: &str = "from vcf";
|
||||
|
||||
pub fn from_vcf_call(call: &EvaluatedCall, input: &Value) -> Result<Value, LabeledError> {
|
||||
let span = input.span().unwrap_or(call.head);
|
||||
let input_string = input.as_string()?;
|
||||
let head = call.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,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn examples() -> Vec<PluginExample> {
|
||||
vec![PluginExample {
|
||||
example: "'BEGIN:VCARD
|
||||
N:Foo
|
||||
FN:Bar
|
||||
EMAIL:foo@bar.com
|
||||
END:VCARD' | from vcf"
|
||||
.into(),
|
||||
description: "Converts ics formatted string to table".into(),
|
||||
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 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 })
|
||||
}
|
55
crates/nu_plugin_formats/src/lib.rs
Normal file
55
crates/nu_plugin_formats/src/lib.rs
Normal file
@ -0,0 +1,55 @@
|
||||
mod from;
|
||||
|
||||
use from::{eml, ics, ini, vcf};
|
||||
use nu_plugin::{EvaluatedCall, LabeledError, Plugin};
|
||||
use nu_protocol::{Category, PluginSignature, SyntaxShape, Type, Value};
|
||||
|
||||
pub struct FromCmds;
|
||||
|
||||
impl Plugin for FromCmds {
|
||||
fn signature(&self) -> Vec<PluginSignature> {
|
||||
vec![
|
||||
PluginSignature::build(eml::CMD_NAME)
|
||||
.input_output_types(vec![(Type::String, Type::Record(vec![]))])
|
||||
.named(
|
||||
"preview-body",
|
||||
SyntaxShape::Int,
|
||||
"How many bytes of the body to preview",
|
||||
Some('b'),
|
||||
)
|
||||
.plugin_examples(eml::examples())
|
||||
.category(Category::Formats),
|
||||
PluginSignature::build(ics::CMD_NAME)
|
||||
.input_output_types(vec![(Type::String, Type::Table(vec![]))])
|
||||
.plugin_examples(ics::examples())
|
||||
.category(Category::Formats),
|
||||
PluginSignature::build(vcf::CMD_NAME)
|
||||
.input_output_types(vec![(Type::String, Type::Table(vec![]))])
|
||||
.plugin_examples(vcf::examples())
|
||||
.category(Category::Formats),
|
||||
PluginSignature::build(ini::CMD_NAME)
|
||||
.input_output_types(vec![(Type::String, Type::Record(vec![]))])
|
||||
.plugin_examples(ini::examples())
|
||||
.category(Category::Formats),
|
||||
]
|
||||
}
|
||||
|
||||
fn run(
|
||||
&mut self,
|
||||
name: &str,
|
||||
call: &EvaluatedCall,
|
||||
input: &Value,
|
||||
) -> Result<Value, LabeledError> {
|
||||
match name {
|
||||
eml::CMD_NAME => eml::from_eml_call(call, input),
|
||||
ics::CMD_NAME => ics::from_ics_call(call, input),
|
||||
vcf::CMD_NAME => vcf::from_vcf_call(call, input),
|
||||
ini::CMD_NAME => ini::from_ini_call(call, input),
|
||||
_ => Err(LabeledError {
|
||||
label: "Plugin call with wrong name signature".into(),
|
||||
msg: "the signature used to call the plugin does not match any name in the plugin signature vector".into(),
|
||||
span: Some(call.head),
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
6
crates/nu_plugin_formats/src/main.rs
Normal file
6
crates/nu_plugin_formats/src/main.rs
Normal file
@ -0,0 +1,6 @@
|
||||
use nu_plugin::{serve_plugin, MsgPackSerializer};
|
||||
use nu_plugin_formats::FromCmds;
|
||||
|
||||
fn main() {
|
||||
serve_plugin(&mut FromCmds, MsgPackSerializer {})
|
||||
}
|
Reference in New Issue
Block a user