2020-03-20 20:35:09 +01:00
|
|
|
extern crate ical;
|
|
|
|
use crate::prelude::*;
|
|
|
|
use ical::parser::vcard::component::*;
|
|
|
|
use ical::property::Property;
|
2021-01-10 03:50:49 +01:00
|
|
|
use nu_engine::WholeStreamCommand;
|
2020-03-20 20:35:09 +01:00
|
|
|
use nu_errors::ShellError;
|
|
|
|
use nu_protocol::{Primitive, ReturnSuccess, Signature, TaggedDictBuilder, UntaggedValue, Value};
|
|
|
|
|
|
|
|
pub struct FromVcf;
|
|
|
|
|
2020-05-29 10:22:52 +02:00
|
|
|
#[async_trait]
|
2020-03-20 20:35:09 +01:00
|
|
|
impl WholeStreamCommand for FromVcf {
|
|
|
|
fn name(&self) -> &str {
|
2020-05-04 10:44:33 +02:00
|
|
|
"from vcf"
|
2020-03-20 20:35:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
2020-05-04 10:44:33 +02:00
|
|
|
Signature::build("from vcf")
|
2020-03-20 20:35:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Parse text as .vcf and create table."
|
|
|
|
}
|
|
|
|
|
2020-12-18 08:53:49 +01:00
|
|
|
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|
|
|
from_vcf(args).await
|
2020-03-20 20:35:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-18 08:53:49 +01:00
|
|
|
async fn from_vcf(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|
|
|
let args = args.evaluate_once().await?;
|
2020-06-06 09:42:06 +02:00
|
|
|
let tag = args.name_tag();
|
|
|
|
let input = args.input;
|
2020-05-16 05:18:24 +02:00
|
|
|
|
2020-06-06 09:42:06 +02:00
|
|
|
let input_string = input.collect_string(tag.clone()).await?.item;
|
2020-07-08 15:20:57 +02:00
|
|
|
let input_bytes = input_string.into_bytes();
|
2020-07-09 01:55:01 +02:00
|
|
|
let cursor = std::io::Cursor::new(input_bytes);
|
|
|
|
let parser = ical::VcardParser::new(cursor);
|
2020-03-20 20:35:09 +01:00
|
|
|
|
2020-07-08 15:20:57 +02:00
|
|
|
let iter = parser.map(move |contact| match contact {
|
|
|
|
Ok(c) => ReturnSuccess::value(contact_to_value(c, tag.clone())),
|
|
|
|
Err(_) => Err(ShellError::labeled_error(
|
|
|
|
"Could not parse as .vcf",
|
|
|
|
"input cannot be parsed as .vcf",
|
|
|
|
tag.clone(),
|
|
|
|
)),
|
|
|
|
});
|
2020-06-06 09:42:06 +02:00
|
|
|
|
2020-07-08 15:20:57 +02:00
|
|
|
Ok(futures::stream::iter(iter).to_output_stream())
|
2020-03-20 20:35:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn contact_to_value(contact: VcardContact, tag: Tag) -> Value {
|
|
|
|
let mut row = TaggedDictBuilder::new(tag.clone());
|
|
|
|
row.insert_untagged("properties", properties_to_value(contact.properties, tag));
|
|
|
|
row.into_value()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn properties_to_value(properties: Vec<Property>, tag: Tag) -> UntaggedValue {
|
|
|
|
UntaggedValue::table(
|
|
|
|
&properties
|
|
|
|
.into_iter()
|
|
|
|
.map(|prop| {
|
|
|
|
let mut row = TaggedDictBuilder::new(tag.clone());
|
|
|
|
|
|
|
|
let name = UntaggedValue::string(prop.name);
|
|
|
|
let value = match prop.value {
|
|
|
|
Some(val) => UntaggedValue::string(val),
|
|
|
|
None => UntaggedValue::Primitive(Primitive::Nothing),
|
|
|
|
};
|
|
|
|
let params = match prop.params {
|
|
|
|
Some(param_list) => params_to_value(param_list, tag.clone()).into(),
|
|
|
|
None => UntaggedValue::Primitive(Primitive::Nothing),
|
|
|
|
};
|
|
|
|
|
|
|
|
row.insert_untagged("name", name);
|
|
|
|
row.insert_untagged("value", value);
|
|
|
|
row.insert_untagged("params", params);
|
|
|
|
row.into_value()
|
|
|
|
})
|
|
|
|
.collect::<Vec<Value>>(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn params_to_value(params: Vec<(String, Vec<String>)>, tag: Tag) -> Value {
|
|
|
|
let mut row = TaggedDictBuilder::new(tag);
|
|
|
|
|
|
|
|
for (param_name, param_values) in params {
|
|
|
|
let values: Vec<Value> = param_values.into_iter().map(|val| val.into()).collect();
|
|
|
|
let values = UntaggedValue::table(&values);
|
|
|
|
row.insert_untagged(param_name, values);
|
|
|
|
}
|
|
|
|
|
|
|
|
row.into_value()
|
|
|
|
}
|
2020-05-18 14:56:01 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::FromVcf;
|
2020-10-03 16:06:02 +02:00
|
|
|
use super::ShellError;
|
2020-05-18 14:56:01 +02:00
|
|
|
|
|
|
|
#[test]
|
2020-10-03 16:06:02 +02:00
|
|
|
fn examples_work_as_expected() -> Result<(), ShellError> {
|
2020-05-18 14:56:01 +02:00
|
|
|
use crate::examples::test as test_examples;
|
|
|
|
|
2021-02-12 11:13:14 +01:00
|
|
|
test_examples(FromVcf {})
|
2020-05-18 14:56:01 +02:00
|
|
|
}
|
|
|
|
}
|