extract_vevent_objs

This commit is contained in:
Astro 2019-10-10 18:42:49 +02:00
parent b20594deac
commit 5cd02e689d
1 changed files with 20 additions and 5 deletions

View File

@ -1,6 +1,7 @@
#[macro_use] #[macro_use]
extern crate diesel; extern crate diesel;
use std::mem::replace;
use std::io::Read; use std::io::Read;
use std::fs::read_to_string; use std::fs::read_to_string;
use std::sync::RwLock; use std::sync::RwLock;
@ -18,6 +19,16 @@ use model::{Calendar, NewCalendar, Event};
mod ics; mod ics;
use ics::{Parser, Object, GetValue}; use ics::{Parser, Object, GetValue};
fn extract_vevent_objs(results: &mut Vec<Object>, mut obj: Object) {
let children = replace(&mut obj.children, vec![]);
if obj.name == "VEVENT" {
results.push(obj);
}
for child in children.into_iter() {
extract_vevent_objs(results, *child);
}
}
fn obj_to_event(calendar: String, obj: Object) -> Option<Event> { fn obj_to_event(calendar: String, obj: Object) -> Option<Event> {
if obj.name != "VEVENT" { if obj.name != "VEVENT" {
return None; return None;
@ -141,12 +152,16 @@ impl Resources {
len if len > 0 => { len if len > 0 => {
let data = &buf[..len]; let data = &buf[..len];
p.feed(data, |obj| { p.feed(data, |obj| {
let mut objs = vec![];
extract_vevent_objs(&mut objs, obj);
for obj in objs {
let dbg = format!("{:?}", obj); let dbg = format!("{:?}", obj);
if let Some(event) = obj_to_event(cal_opts.url.clone(), obj) { if let Some(event) = obj_to_event(cal_opts.url.clone(), obj) {
events.push(event); events.push(event);
} else { } else {
println!("ignore {}", dbg); println!("ignore {}", dbg);
} }
}
}); });
} }
_ => break, _ => break,