Correct line folding in from ics/from vcf (#10577)

# Description
Potential fix for #10398

datatracker.ietf.org/doc/html/rfc5545#section-3.1
datatracker.ietf.org/doc/html/rfc6350#section-3.2

---------

Co-authored-by: Joerg <joerg@schuetter.org>
This commit is contained in:
joergsch 2023-10-03 19:11:16 +02:00 committed by GitHub
parent f481879ed3
commit e8da57b05e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 107 additions and 8 deletions

View File

@ -14,9 +14,17 @@ pub fn from_ics_call(call: &EvaluatedCall, input: &Value) -> Result<Value, Label
let input_string = input_string let input_string = input_string
.lines() .lines()
.map(|x| x.trim().to_string()) .enumerate()
.collect::<Vec<_>>() .map(|(i, x)| {
.join("\n"); if i == 0 {
x.trim().to_string()
} else if x.len() > 1 && (x.starts_with(' ') || x.starts_with('\t')) {
x[1..].trim_end().to_string()
} else {
format!("\n{}", x.trim())
}
})
.collect::<String>();
let input_bytes = input_string.as_bytes(); let input_bytes = input_string.as_bytes();
let buf_reader = BufReader::new(input_bytes); let buf_reader = BufReader::new(input_bytes);

View File

@ -13,9 +13,17 @@ pub fn from_vcf_call(call: &EvaluatedCall, input: &Value) -> Result<Value, Label
let input_string = input_string let input_string = input_string
.lines() .lines()
.map(|x| x.trim().to_string()) .enumerate()
.collect::<Vec<_>>() .map(|(i, x)| {
.join("\n"); if i == 0 {
x.trim().to_string()
} else if x.len() > 1 && (x.starts_with(' ') || x.starts_with('\t')) {
x[1..].trim_end().to_string()
} else {
format!("\n{}", x.trim())
}
})
.collect::<String>();
let input_bytes = input_string.as_bytes(); let input_bytes = input_string.as_bytes();
let cursor = std::io::Cursor::new(input_bytes); let cursor = std::io::Cursor::new(input_bytes);

View File

@ -1,4 +1,4 @@
use nu_test_support::fs::Stub::FileWithContentToBeTrimmed; use nu_test_support::fs::Stub::{FileWithContent, FileWithContentToBeTrimmed};
use nu_test_support::nu_with_plugins; use nu_test_support::nu_with_plugins;
use nu_test_support::playground::Playground; use nu_test_support::playground::Playground;
use pretty_assertions::assert_eq; use pretty_assertions::assert_eq;
@ -98,3 +98,47 @@ fn from_ics_text_to_table() {
assert_eq!(actual.out, "Maryland Game"); assert_eq!(actual.out, "Maryland Game");
}) })
} }
#[test]
fn from_ics_text_with_linebreak_to_table() {
Playground::setup("filter_from_ics_test_3", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContent(
"calendar.txt",
r#"BEGIN:VCALENDAR
BEGIN:VEVENT
DTSTART:20171007T200000Z
DTEND:20171007T233000Z
DTSTAMP:20200319T182138Z
UID:4l80f6dcovnriq38g57g07btid@google.com
CREATED:20170719T202915Z
DESCRIPTION:
LAST-MODIFIED:20170930T190808Z
LOCATION:The Restaurant n
ear the
Belltower
SEQUENCE:1
STATUS:CONFIRMED
SUMMARY:Dinner
TRANSP:TRANSPARENT
END:VEVENT
END:VCALENDAR"#,
)]);
let cwd = dirs.test();
let actual = nu_with_plugins!(
cwd: cwd,
plugin: ("nu_plugin_formats"),
r#"
open calendar.txt
| from ics
| get events.0
| get properties.0
| where name == "LOCATION"
| first
| get value
"#
);
assert_eq!(actual.out, "The Restaurant near the Belltower");
})
}

View File

@ -1,4 +1,4 @@
use nu_test_support::fs::Stub::FileWithContentToBeTrimmed; use nu_test_support::fs::Stub::{FileWithContent, FileWithContentToBeTrimmed};
use nu_test_support::nu_with_plugins; use nu_test_support::nu_with_plugins;
use nu_test_support::playground::Playground; use nu_test_support::playground::Playground;
use pretty_assertions::assert_eq; use pretty_assertions::assert_eq;
@ -82,3 +82,42 @@ fn from_vcf_text_to_table() {
assert_eq!(actual.out, "john.doe99@gmail.com"); assert_eq!(actual.out, "john.doe99@gmail.com");
}) })
} }
#[test]
fn from_vcf_text_with_linebreak_to_table() {
Playground::setup("filter_from_vcf_test_3", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContent(
"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 cwd = dirs.test();
let actual = nu_with_plugins!(
cwd: cwd,
plugin: ("nu_plugin_formats"),
r#"
open contacts.txt
| from vcf
| get properties.0
| where name == "EMAIL"
| first
| get value
"#
);
assert_eq!(actual.out, "john.doe99@gmail.com");
})
}