Skip to content

Commit 41e94cd

Browse files
authored
Merge pull request #56 from hoodie/feature/alarms
Alarms
2 parents 1b3165b + 1d6cb60 commit 41e94cd

18 files changed

+1121
-84
lines changed

Cargo.toml

+6-4
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@ readme = "README.md"
1515
rust-version = "1.56"
1616
exclude = ["fixtures", ".github", ".gitignore", "*.json"]
1717

18+
[features]
19+
default = []
20+
parser = ["nom"]
21+
1822
[dependencies]
1923
serde = { version = "1.0", optional = true, features = ["derive"] }
2024
serde_json = { version = "1.0", optional = true }
25+
iso8601 = "0.5"
26+
pretty_assertions = "1"
2127

2228
[dependencies.chrono]
2329
version = "0.4"
@@ -35,10 +41,6 @@ version = "1"
3541
[dev-dependencies]
3642
pretty_assertions = "1"
3743

38-
[features]
39-
default = []
40-
parser = ["nom"]
41-
4244
[package.metadata.docs.rs]
4345
all-features = true
4446

examples/alarm.rs

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
use chrono::*;
2+
use icalendar::*;
3+
4+
fn main() {
5+
let mut calendar = Calendar::new();
6+
7+
let now = Utc::now();
8+
let soon = Utc::now() + Duration::minutes(12);
9+
let tomorrow = Utc::now() + Duration::days(1);
10+
11+
let todo_test_audio = Todo::new()
12+
.summary("TODO with audio alarm -15min")
13+
.sequence(1)
14+
.starts(now)
15+
.due(soon)
16+
.status(TodoStatus::NeedsAction)
17+
.percent_complete(98)
18+
.alarm(
19+
Alarm::audio(-Duration::minutes(10))
20+
.duration_and_repeat(chrono::Duration::minutes(1), 4),
21+
)
22+
.done();
23+
24+
let event_test_display = Event::new()
25+
.summary("test event")
26+
.description("here I have something really important to do")
27+
.starts(Utc::now() + Duration::minutes(5))
28+
.class(Class::Confidential)
29+
.ends(Utc::now() + Duration::hours(1))
30+
.alarm(
31+
Alarm::display(
32+
"you should test your implementation",
33+
Utc::now() + Duration::minutes(1),
34+
)
35+
.duration_and_repeat(chrono::Duration::minutes(1), 4),
36+
)
37+
.done();
38+
39+
let todo_test_display = Todo::new()
40+
.summary("TODO with display alarm now + 1 min")
41+
.sequence(3)
42+
.starts(now)
43+
.due(soon)
44+
.status(TodoStatus::NeedsAction)
45+
.alarm(
46+
Alarm::display(
47+
"you should test your implementation",
48+
(-Duration::minutes(10), Related::End),
49+
)
50+
.duration_and_repeat(chrono::Duration::minutes(1), 4),
51+
)
52+
.done();
53+
54+
let todo_taxes = Todo::new()
55+
.summary("Submit Income Taxes")
56+
.sequence(4)
57+
.starts(now)
58+
.due(tomorrow)
59+
.status(TodoStatus::NeedsAction)
60+
.alarm(
61+
Alarm::audio(now + Duration::minutes(1))
62+
.duration_and_repeat(chrono::Duration::minutes(1), 4),
63+
)
64+
.done();
65+
66+
calendar.push(event_test_display);
67+
calendar.push(todo_test_audio);
68+
calendar.push(todo_test_display);
69+
calendar.push(todo_taxes);
70+
71+
println!("{calendar}");
72+
73+
#[cfg(feature = "parser")]
74+
{
75+
use std::str::FromStr;
76+
77+
let source = calendar.to_string();
78+
let reparse = Calendar::from_str(&source).unwrap();
79+
println!("{:#?}", reparse);
80+
}
81+
}

examples/alarm_minimal.rs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use chrono::*;
2+
use icalendar::*;
3+
4+
fn main() {
5+
// alarm will occur one minute from now
6+
let event_with_absolute_audio_alarm = Event::new()
7+
.alarm(
8+
Alarm::audio(Utc::now() + Duration::minutes(1))
9+
.duration_and_repeat(Duration::minutes(1), 4),
10+
)
11+
.done();
12+
13+
// alarm will occur one minute before the start
14+
let event_with_relative_display_alarm = Event::new()
15+
.alarm(
16+
Alarm::display("ALARM! ALARM!", -Duration::minutes(1))
17+
.duration_and_repeat(Duration::minutes(1), 4),
18+
)
19+
.done();
20+
// alarm will occur one minute before the end
21+
let event_with_relative_display_alarm_end = Event::new()
22+
.alarm(
23+
Alarm::display("ALARM! ALARM!", (-Duration::minutes(1), Related::End))
24+
.duration_and_repeat(Duration::minutes(1), 4),
25+
)
26+
.done();
27+
event_with_absolute_audio_alarm.print().unwrap();
28+
event_with_relative_display_alarm.print().unwrap();
29+
event_with_relative_display_alarm_end.print().unwrap();
30+
}

examples/custom_property_parsed.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
66
.summary("test event")
77
.append_property(
88
"TEST;IMPORTANCE=very;DUE=tomorrow:FOOBAR\n"
9-
.parse()
9+
.parse::<Property>()
1010
.unwrap(),
1111
)
1212
// .uid("my.own.id")

examples/full_circle.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
use std::str::FromStr;
33

44
use chrono::*;
5-
use icalendar::{
6-
parser::{read_calendar, unfold},
7-
Calendar, Class, Component, Event, Property, Todo,
8-
};
5+
use icalendar::parser;
6+
use icalendar::*;
97

108
fn main() {
119
let event = Event::new()
@@ -39,5 +37,8 @@ fn main() {
3937
println!("{}", from_parsed); // print what parsed
4038
println!("{:#?}", built_calendar); // inner representation of what we built
4139
println!("{:#?}", from_parsed); // inner representation of what we built and then parsed
42-
println!("{:#?}", read_calendar(&unfold(&ical)).unwrap()); // inner presentation of the parser's data structure
40+
println!(
41+
"{:#?}",
42+
parser::read_calendar(&parser::unfold(&ical)).unwrap()
43+
); // inner presentation of the parser's data structure
4344
}

src/calendar.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ impl Calendar {
8686
}
8787

8888
/// Append a given `Property` to the `Calendar`
89-
pub fn append_property(&mut self, property: Property) -> &mut Self {
90-
self.properties.push(property);
89+
pub fn append_property(&mut self, property: impl Into<Property>) -> &mut Self {
90+
self.properties.push(property.into());
9191
self
9292
}
9393

0 commit comments

Comments
 (0)