Skip to content

Commit

Permalink
Merge pull request #56 from hoodie/feature/alarms
Browse files Browse the repository at this point in the history
Alarms
  • Loading branch information
hoodie authored Nov 20, 2022
2 parents 1b3165b + 1d6cb60 commit 41e94cd
Show file tree
Hide file tree
Showing 18 changed files with 1,121 additions and 84 deletions.
10 changes: 6 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@ readme = "README.md"
rust-version = "1.56"
exclude = ["fixtures", ".github", ".gitignore", "*.json"]

[features]
default = []
parser = ["nom"]

[dependencies]
serde = { version = "1.0", optional = true, features = ["derive"] }
serde_json = { version = "1.0", optional = true }
iso8601 = "0.5"
pretty_assertions = "1"

[dependencies.chrono]
version = "0.4"
Expand All @@ -35,10 +41,6 @@ version = "1"
[dev-dependencies]
pretty_assertions = "1"

[features]
default = []
parser = ["nom"]

[package.metadata.docs.rs]
all-features = true

Expand Down
81 changes: 81 additions & 0 deletions examples/alarm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use chrono::*;
use icalendar::*;

fn main() {
let mut calendar = Calendar::new();

let now = Utc::now();
let soon = Utc::now() + Duration::minutes(12);
let tomorrow = Utc::now() + Duration::days(1);

let todo_test_audio = Todo::new()
.summary("TODO with audio alarm -15min")
.sequence(1)
.starts(now)
.due(soon)
.status(TodoStatus::NeedsAction)
.percent_complete(98)
.alarm(
Alarm::audio(-Duration::minutes(10))
.duration_and_repeat(chrono::Duration::minutes(1), 4),
)
.done();

let event_test_display = Event::new()
.summary("test event")
.description("here I have something really important to do")
.starts(Utc::now() + Duration::minutes(5))
.class(Class::Confidential)
.ends(Utc::now() + Duration::hours(1))
.alarm(
Alarm::display(
"you should test your implementation",
Utc::now() + Duration::minutes(1),
)
.duration_and_repeat(chrono::Duration::minutes(1), 4),
)
.done();

let todo_test_display = Todo::new()
.summary("TODO with display alarm now + 1 min")
.sequence(3)
.starts(now)
.due(soon)
.status(TodoStatus::NeedsAction)
.alarm(
Alarm::display(
"you should test your implementation",
(-Duration::minutes(10), Related::End),
)
.duration_and_repeat(chrono::Duration::minutes(1), 4),
)
.done();

let todo_taxes = Todo::new()
.summary("Submit Income Taxes")
.sequence(4)
.starts(now)
.due(tomorrow)
.status(TodoStatus::NeedsAction)
.alarm(
Alarm::audio(now + Duration::minutes(1))
.duration_and_repeat(chrono::Duration::minutes(1), 4),
)
.done();

calendar.push(event_test_display);
calendar.push(todo_test_audio);
calendar.push(todo_test_display);
calendar.push(todo_taxes);

println!("{calendar}");

#[cfg(feature = "parser")]
{
use std::str::FromStr;

let source = calendar.to_string();
let reparse = Calendar::from_str(&source).unwrap();
println!("{:#?}", reparse);
}
}
30 changes: 30 additions & 0 deletions examples/alarm_minimal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use chrono::*;
use icalendar::*;

fn main() {
// alarm will occur one minute from now
let event_with_absolute_audio_alarm = Event::new()
.alarm(
Alarm::audio(Utc::now() + Duration::minutes(1))
.duration_and_repeat(Duration::minutes(1), 4),
)
.done();

// alarm will occur one minute before the start
let event_with_relative_display_alarm = Event::new()
.alarm(
Alarm::display("ALARM! ALARM!", -Duration::minutes(1))
.duration_and_repeat(Duration::minutes(1), 4),
)
.done();
// alarm will occur one minute before the end
let event_with_relative_display_alarm_end = Event::new()
.alarm(
Alarm::display("ALARM! ALARM!", (-Duration::minutes(1), Related::End))
.duration_and_repeat(Duration::minutes(1), 4),
)
.done();
event_with_absolute_audio_alarm.print().unwrap();
event_with_relative_display_alarm.print().unwrap();
event_with_relative_display_alarm_end.print().unwrap();
}
2 changes: 1 addition & 1 deletion examples/custom_property_parsed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.summary("test event")
.append_property(
"TEST;IMPORTANCE=very;DUE=tomorrow:FOOBAR\n"
.parse()
.parse::<Property>()
.unwrap(),
)
// .uid("my.own.id")
Expand Down
11 changes: 6 additions & 5 deletions examples/full_circle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
use std::str::FromStr;

use chrono::*;
use icalendar::{
parser::{read_calendar, unfold},
Calendar, Class, Component, Event, Property, Todo,
};
use icalendar::parser;
use icalendar::*;

fn main() {
let event = Event::new()
Expand Down Expand Up @@ -39,5 +37,8 @@ fn main() {
println!("{}", from_parsed); // print what parsed
println!("{:#?}", built_calendar); // inner representation of what we built
println!("{:#?}", from_parsed); // inner representation of what we built and then parsed
println!("{:#?}", read_calendar(&unfold(&ical)).unwrap()); // inner presentation of the parser's data structure
println!(
"{:#?}",
parser::read_calendar(&parser::unfold(&ical)).unwrap()
); // inner presentation of the parser's data structure
}
4 changes: 2 additions & 2 deletions src/calendar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ impl Calendar {
}

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

Expand Down
Loading

0 comments on commit 41e94cd

Please sign in to comment.