1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use icalendar::Calendar;
use std::ffi::OsString;
use std::path::Path;
#[derive(Debug)]
struct IcsReadError {
path: OsString,
//error: Box<dyn std::error::Error>,
}
fn list_ics_from_dir(
path: &Path,
) -> Result<impl Iterator<Item = OsString>, Box<dyn std::error::Error>> {
match std::fs::read_dir(path) {
Ok(dirs) => Ok(dirs.filter_map(|res| match res {
Ok(t) => {
if let Some(x) = t.path().extension() {
if x.eq_ignore_ascii_case("ics") {
Some(t.path().into_os_string())
} else {
None
}
} else {
None
}
}
Err(_) => None,
})),
Err(error) => Err(Box::new(error)),
}
}
fn read_ics_from_dir(
path: &Path,
) -> Result<impl Iterator<Item = Result<Calendar, IcsReadError>>, Box<dyn std::error::Error>> {
match list_ics_from_dir(path) {
Ok(ics_it) => Ok(ics_it.map(|ipath| {
// Read file to output
let ics_path = ipath.to_os_string();
match &mut std::fs::File::open(ipath) {
Ok(f) => {
let readable: &mut dyn std::io::Read = f;
let mut output = String::new();
match readable.read_to_string(&mut output) {
Ok(_) => {
//icalendar::parser::read_calendar(&output)
match output.parse::<Calendar>() {
Ok(read) => Ok(read),
Err(_error) => Err(IcsReadError {
path: ics_path,
//error: Box::new(error.into()),
}),
}
}
Err(_error) => Err(IcsReadError {
path: ics_path,
//error: Box::new(error.to_string()),
}),
}
}
Err(_error) => Err(IcsReadError {
path: ics_path,
//error: Box::new(error.to_string()),
}),
}
})),
Err(error) => Err(error),
}
}
pub fn read_vdir_cal(path: &Path) -> Result<Calendar, Box<dyn std::error::Error>> {
// TODO: we can read some metadata like displayname and color
/*if path.join("displayname").is_file() {
cal.
}*/
match read_ics_from_dir(Path::new(path)) {
Ok(entries_it) => {
let mut cal = Calendar::new();
cal.components.extend(
entries_it
.filter_map(|entry| match entry {
Ok(p) => Some(p.components),
Err(error) => {
eprintln!("Issue reading {:#?}\n\t{:#?}", error.path, error);
None
}
})
.flatten(),
);
Ok(cal)
}
Err(error) => Err(error),
}
}