libticker: parse quoted props
This commit is contained in:
parent
e6468dc4d3
commit
c329bc5c81
|
@ -104,7 +104,7 @@ impl FromStr for Timestamp {
|
||||||
NaiveDate::parse_from_str(s, "%Y%m%d")
|
NaiveDate::parse_from_str(s, "%Y%m%d")
|
||||||
.map(Timestamp::Date)
|
.map(Timestamp::Date)
|
||||||
)
|
)
|
||||||
.map_err(|e| format!("Cannot parse date: {:?}", e))
|
.map_err(|e| format!("Cannot parse date {:?}: {:?}", s, e))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,8 @@ enum LineState {
|
||||||
Key,
|
Key,
|
||||||
PropName,
|
PropName,
|
||||||
PropValue,
|
PropValue,
|
||||||
|
PropValueQuoted,
|
||||||
|
PropValueQuotedEnd,
|
||||||
Value,
|
Value,
|
||||||
ValueEscape,
|
ValueEscape,
|
||||||
}
|
}
|
||||||
|
@ -111,6 +113,9 @@ impl Tokenizer {
|
||||||
}
|
}
|
||||||
self.line_state = LineState::Value;
|
self.line_state = LineState::Value;
|
||||||
}
|
}
|
||||||
|
(LineState::PropValue, '"') if self.buffer.is_empty() => {
|
||||||
|
self.line_state = LineState::PropValueQuoted;
|
||||||
|
}
|
||||||
(LineState::PropValue, ':') => {
|
(LineState::PropValue, ':') => {
|
||||||
let buffer = replace(&mut self.buffer, vec![]);
|
let buffer = replace(&mut self.buffer, vec![]);
|
||||||
match String::from_utf8(buffer) {
|
match String::from_utf8(buffer) {
|
||||||
|
@ -121,6 +126,20 @@ impl Tokenizer {
|
||||||
}
|
}
|
||||||
self.line_state = LineState::Value;
|
self.line_state = LineState::Value;
|
||||||
}
|
}
|
||||||
|
(LineState::PropValueQuoted, '"') => {
|
||||||
|
let buffer = replace(&mut self.buffer, vec![]);
|
||||||
|
match String::from_utf8(buffer) {
|
||||||
|
Ok(s) =>
|
||||||
|
f(Token::PropValue(s)),
|
||||||
|
Err(e) =>
|
||||||
|
println!("UTF8 error: {:?}", e),
|
||||||
|
}
|
||||||
|
self.line_state = LineState::PropValueQuotedEnd;
|
||||||
|
}
|
||||||
|
(LineState::PropValueQuotedEnd, ':') => {
|
||||||
|
self.line_state = LineState::Value;
|
||||||
|
}
|
||||||
|
(LineState::PropValueQuotedEnd, _) => {}
|
||||||
(LineState::Value, '\n') => {
|
(LineState::Value, '\n') => {
|
||||||
let buffer = replace(&mut self.buffer, vec![]);
|
let buffer = replace(&mut self.buffer, vec![]);
|
||||||
match String::from_utf8(buffer) {
|
match String::from_utf8(buffer) {
|
||||||
|
@ -202,4 +221,19 @@ END:VEVENT
|
||||||
Token::Key("SUMMARY".to_owned()), Token::Value("HelloWorld".to_owned()),
|
Token::Key("SUMMARY".to_owned()), Token::Value("HelloWorld".to_owned()),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn tokenize_quoted_prop() {
|
||||||
|
let mut t = Tokenizer::new();
|
||||||
|
let mut tokens = vec![];
|
||||||
|
t.feed(b"DTSTART;TZID=\"+02:00\":20221230T220000
|
||||||
|
|
||||||
|
", |token| tokens.push(token));
|
||||||
|
assert_eq!(tokens, vec![
|
||||||
|
Token::Key("DTSTART".to_owned()),
|
||||||
|
Token::PropName("TZID".to_owned()),
|
||||||
|
Token::PropValue("+02:00".to_owned()),
|
||||||
|
Token::Value("20221230T220000".to_owned()),
|
||||||
|
]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue