#[cfg(feature = "encoding")]
use alloc::string::String;
use alloc::{borrow::Cow, vec::Vec};
use crate::tree::{line::IcalLine, param::lens::IcalParamLens};
pub struct IcalValueCursor<'c, 'a> {
pub line: &'c mut IcalLine<'a>,
}
impl IcalValueCursor<'_, '_> {
pub fn text(&self) -> Cow<'_, str> {
self.line.value.decode()
}
pub fn set_text(&mut self, value: impl AsRef<str>) {
self.line.value.set(&[value]);
}
pub fn bytes(&self) -> Cow<'_, [u8]> {
self.line.value.decode_bytes()
}
pub fn set_bytes(&mut self, value: impl AsRef<[u8]>) {
self.line.value.set_bytes(&[value]);
}
#[cfg(feature = "quoted-printable")]
pub fn quoted_printable(&self) -> Vec<u8> {
let raw = self.bytes();
if self.line.is_quoted_printable() {
quoted_printable::decode(raw.as_ref(), quoted_printable::ParseMode::Robust)
.unwrap_or_else(|_| raw.into_owned())
} else {
raw.into_owned()
}
}
#[cfg(feature = "encoding")]
pub fn charset(&self) -> String {
#[cfg(feature = "quoted-printable")]
let bytes = self.quoted_printable();
#[cfg(not(feature = "quoted-printable"))]
let bytes = self.bytes().into_owned();
let encoding = self
.line
.charset_label()
.and_then(|label| encoding_rs::Encoding::for_label(label.as_bytes()))
.unwrap_or(encoding_rs::UTF_8);
encoding.decode_without_bom_handling(&bytes).0.into_owned()
}
pub fn list(&self) -> Vec<Cow<'_, str>> {
self.line.value.decode_list()
}
pub fn set_list<S: AsRef<str>>(&mut self, values: &[S]) {
self.line.value.set(values);
}
pub fn component(&self, i: usize) -> Vec<Cow<'_, str>> {
self.line.value.decode_component_list(i)
}
pub fn set_component<S: AsRef<str>>(&mut self, i: usize, values: &[S]) {
self.line.value.set_component(i, values);
}
pub fn param<P: IcalParamLens>(&self) -> Option<P::Target<'_>> {
self.line.param::<P>()
}
}
#[cfg(test)]
mod tests {
use alloc::{
format,
string::{String, ToString},
vec,
};
use crate::{prop::summary::SUMMARY, tree::cst::IcalCst};
const HEAD: &str = "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//x//EN\r\n";
const TAIL: &str = "END:VCALENDAR\r\n";
fn cal(prop_line: &str) -> String {
format!("{HEAD}{prop_line}\r\n{TAIL}")
}
#[test]
fn edits_a_scalar_value_in_place_escaping_it() {
let raw = cal("SUMMARY:Lunch");
let mut c = IcalCst::parse(&raw).unwrap();
c.prop_mut::<SUMMARY>().unwrap().set_text("Tea, now");
assert!(c.to_string().contains("SUMMARY:Tea\\, now\r\n"));
}
#[test]
fn keeps_a_newline_written_into_a_vcalendar_1_0_value_on_its_line() {
let raw = "BEGIN:VCALENDAR\r\nVERSION:1.0\r\nSUMMARY:x\r\nEND:VCALENDAR\r\n";
let mut c = IcalCst::parse(raw.as_bytes()).unwrap();
c.prop_mut::<SUMMARY>().unwrap().set_text("a\nb");
assert!(c.to_string().contains("SUMMARY:a\\nb\r\n"));
assert!(IcalCst::parse(&c.to_bytes()).is_ok());
}
#[test]
fn writes_and_reads_a_foreign_charset_value_as_raw_bytes() {
use crate::prop::description::DESCRIPTION;
let raw = cal("DESCRIPTION;CHARSET=ISO-8859-1:x");
let mut c = IcalCst::parse(&raw).unwrap();
let latin1 = [b'c', b'a', b'f', 0xE9];
c.prop_mut::<DESCRIPTION>().unwrap().set_bytes(latin1);
assert_eq!(
c.prop_mut::<DESCRIPTION>().unwrap().bytes().as_ref(),
&latin1,
);
assert!(c.to_bytes().windows(4).any(|window| window == latin1));
}
#[cfg(feature = "quoted-printable")]
#[test]
fn quoted_printable_helper_resolves_octets() {
use crate::prop::description::DESCRIPTION;
let raw = cal("DESCRIPTION;CHARSET=ISO-8859-1;ENCODING=QUOTED-PRINTABLE:caf=E9");
let mut c = IcalCst::parse(&raw).unwrap();
assert_eq!(
c.prop_mut::<DESCRIPTION>().unwrap().quoted_printable(),
[b'c', b'a', b'f', 0xE9],
);
}
#[cfg(all(feature = "encoding", feature = "quoted-printable"))]
#[test]
fn charset_helper_transcodes_to_utf8() {
use crate::prop::description::DESCRIPTION;
let raw = cal("DESCRIPTION;CHARSET=ISO-8859-1;ENCODING=QUOTED-PRINTABLE:caf=E9");
let mut c = IcalCst::parse(&raw).unwrap();
assert_eq!(c.prop_mut::<DESCRIPTION>().unwrap().charset(), "café");
}
#[test]
fn edits_one_structured_component_preserving_the_rest() {
use crate::prop::geo::GEO;
let raw = cal("GEO:37.0;-122.0");
let mut c = IcalCst::parse(&raw).unwrap();
c.prop_mut::<GEO>().unwrap().set_component(1, &["-100.0"]);
assert!(c.to_string().contains("GEO:37.0;-100.0\r\n"));
}
#[test]
fn reads_and_writes_the_whole_value_not_its_first_component() {
use crate::prop::description::DESCRIPTION;
let raw = cal("DESCRIPTION:a;b");
let mut c = IcalCst::parse(&raw).unwrap();
{
let cursor = c.prop_mut::<DESCRIPTION>().unwrap();
assert_eq!(cursor.text(), "a;b");
assert_eq!(cursor.bytes().as_ref(), b"a;b");
assert_eq!(cursor.list(), vec!["a;b"]);
}
let whole = c.prop_mut::<DESCRIPTION>().unwrap().text().into_owned();
c.prop_mut::<DESCRIPTION>().unwrap().set_text(&whole);
assert!(c.to_string().contains("DESCRIPTION:a\\;b\r\n"), "got: {c}");
assert_eq!(c.prop_mut::<DESCRIPTION>().unwrap().text(), "a;b");
}
#[test]
fn reads_a_structured_component_past_its_first_comma() {
use crate::prop::request_status::REQUEST_STATUS;
let raw = cal("REQUEST-STATUS:2.0;ok;rcpt,two");
let c = IcalCst::parse(&raw).unwrap();
let status = c.prop::<REQUEST_STATUS>().unwrap();
assert_eq!(status.description, "ok");
assert_eq!(status.extra, "rcpt,two");
}
#[test]
fn exercises_every_generic_accessor() {
use crate::prop::categories::CATEGORIES;
let raw = cal("CATEGORIES:a,b");
let mut c = IcalCst::parse(&raw).unwrap();
{
let mut cursor = c.prop_mut::<CATEGORIES>().unwrap();
assert_eq!(cursor.text(), "a,b");
assert_eq!(cursor.list(), vec!["a", "b"]);
assert_eq!(cursor.component(0), vec!["a", "b"]);
cursor.set_text("x");
assert_eq!(cursor.text(), "x");
cursor.set_list(&["a", "b"]);
assert_eq!(cursor.list(), vec!["a", "b"]);
cursor.set_component(1, &["y"]);
assert_eq!(cursor.component(1), vec!["y"]);
}
assert!(c.to_string().contains("CATEGORIES:a,b;y\r\n"), "got: {c}");
}
}