Skip to content

Commit

Permalink
WIP: add value-type mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
hoodie committed Aug 10, 2024
1 parent 24f6994 commit b46d918
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,10 @@ mod tests {
let event = Event::new().url(url).done();

let serialized = event.to_string();
let reparsed =
dbg!(Other::try_from(crate::parser::Component::<'_>::try_from(serialized.as_str()).unwrap())
.unwrap());
let reparsed = dbg!(Other::try_from(
crate::parser::Component::<'_>::try_from(serialized.as_str()).unwrap()
)
.unwrap());

assert_eq!(event.get_url(), Some(url));
assert_eq!(reparsed.get_url(), Some(url));
Expand Down
93 changes: 93 additions & 0 deletions src/parser/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,99 @@ use pretty_assertions::assert_eq;

use super::parsed_string::ParseString;

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
enum ValueType {
/// [RFC 5545 3.3.1](https://datatracker.ietf.org/doc/html/rfc5545#section-3.3.1)
Binary,
/// [RFC 5545 3.3.2](https://datatracker.ietf.org/doc/html/rfc5545#section-3.3.2)
Boolean,
/// [RFC 5545 3.3.3](https://datatracker.ietf.org/doc/html/rfc5545#section-3.3.3)
CalAddress,
/// [RFC 5545 3.3.4](https://datatracker.ietf.org/doc/html/rfc5545#section-3.3.4)
Date,
/// [RFC 5545 3.3.5](https://datatracker.ietf.org/doc/html/rfc5545#section-3.3.5)
DateTime,
/// [RFC 5545 3.3.6](https://datatracker.ietf.org/doc/html/rfc5545#section-3.3.6)
Duration,
/// [RFC 5545 3.3.7](https://datatracker.ietf.org/doc/html/rfc5545#section-3.3.7)
Float,
/// [RFC 5545 3.3.8](https://datatracker.ietf.org/doc/html/rfc5545#section-3.3.8)
Integer,
/// [RFC 5545 3.3.9](https://datatracker.ietf.org/doc/html/rfc5545#section-3.3.9)
Period,
/// [RFC 5545 3.3.10](https://datatracker.ietf.org/doc/html/rfc5545#section-3.3.10)
Recur,
/// [RFC 5545 3.3.11](https://datatracker.ietf.org/doc/html/rfc5545#section-3.3.11)
Text,
/// [RFC 5545 3.3.12](https://datatracker.ietf.org/doc/html/rfc5545#section-3.3.12)
Time,
/// [RFC 5545 3.3.13](https://datatracker.ietf.org/doc/html/rfc5545#section-3.3.13)
Uri,
/// [RFC 5545 3.3.14](https://datatracker.ietf.org/doc/html/rfc5545#section-3.3.14)
UtcOffset,
Invalid
}

/// returns the proper value type for a given property according to RFC 5545
fn value_type(property_name: &str) -> ValueType {
use ValueType::*;
if property_name.chars().any(|c| c.is_lowercase()) {
panic!("property_name must be uppercase");
}
match property_name {
"CALSCALE" => Text,
"METHOD" => Text,
"PRODID" => Text,
"VERSION" => Text,
"ATTACH" => Uri, // or BINARY
"CATEGORIES" => Text,
"CLASS" => Text,
"COMMENT" => Text,
"DESCRIPTION" => Text,
"GEO" => Float,
"LOCATION" => Text,
"PERCENT-COMPLETE" => Integer,
"PRIORITY" => Integer,
"RESOURCES" => Text,
"STATUS" => Text, // 3.8.1.11
"SUMMARY" => Text, // 3.8.8.1.12
"COMPLETED" => DateTime, // 3.8.2.1
"DTEND" => DateTime, // or DATE // 3.8.2.2
"DUE" => DateTime, // or DATE // 3.8.2.3
"DTSTART" => DateTime, // or DATE 3.8.2.4
"DURATION" => Duration, // 3.8.2.5
"FREEBUSY" => Period, // 3.8.2.6
"TRANSP" => Text, // 3.8.2.7
"TZID" => Text, // 3.8.3.1
"TZNAME" => Text, // 3.8.3.2
"TZOFFSETFROM" => UtcOffset, //
"TZOFFSETTO" => UtcOffset, //
"TZURL" => Uri, //
"ATTENDEE" => CalAddress, // 3.8.4.1
"CONTACT" => Text, // 3.8.4.2
"ORGANIZER" => CalAddress, // 3.8.4.3
"RECURRENCE-ID" => DateTime, // 3.8.4.4
"RELATED-TO" => Text, //
"URL" => Uri, //
"UID" => Text, //
"EXDATE" => DateTime, // or DATE-TIME //
"RDATE" => DateTime, // or PERIOD // or DATE-TIME // or DATE //
"RRULE" => Recur, //
"ACTION" => Text, //
"REPEAT" => Integer, //
"TRIGGER" => Duration, // or DATE-TIME (must be UTC) // 3.8.6.3
"CREATED" => DateTime, // 3.8.7.1
"DTSTAMP" => DateTime, // 3.8.7.2
"LAST-MODIFIED" => DateTime, // 3.8.7.3
"SEQUENCE" => Integer, // 3.8.7.4
// 3.8.8.1
// "An IANA-registered property name" => Any parameter can be specified on this property.
"X-"/* ... */ => Text, // any type 3.8.8.2
"REQUEST-STATUS" => Text, // 3.8.8.3
_ => Invalid,
}
}

// TODO: how do I express <<alpha_or_dash, but not "END">>
pub fn property_key<'a, E: ParseError<&'a str> + ContextError<&'a str>>(
input: &'a str,
Expand Down
2 changes: 2 additions & 0 deletions src/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ impl Property {
}

/// <https://datatracker.ietf.org/doc/html/rfc5545#section-3.3.11>
/// TODO: this should not be used for properties but TEXT
#[allow(dead_code)]
fn escape_text(input: &str) -> String {
input
.replace('\\', r#"\\"#)
Expand Down

0 comments on commit b46d918

Please sign in to comment.