summaryrefslogtreecommitdiffstats
path: root/src/actions/new.rs
blob: 613f04f93d04226c781923d4c1b8fa60a13f8cee (plain)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
use crate::calendars;
use crate::cursorfile;
use crate::defaults;
use crate::icalwrap::{IcalTime, IcalTimeZone, IcalVCalendar};
use crate::khline::KhLine;
use crate::utils::{fileutil, misc};
use crate::KhResult;
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
pub struct NewArgs {
  /// the calendar
  #[structopt(name = "calendar")]
  pub calendar: String,
  /// from
  #[structopt(name = "from")]
  pub from: String,
  /// to
  #[structopt(name = "to")]
  pub to: String,
  /// summary
  #[structopt(name = "summary")]
  pub summary: String,
  /// location
  #[structopt(name = "location")]
  pub location: String,
}

struct EventProperties {
  calendar: String,
  from: IcalTime,
  to: IcalTime,
  summary: String,
  location: String,
}

impl EventProperties {
  fn parse_from_args(args: &NewArgs) -> KhResult<EventProperties> {
    let calendar = EventProperties::parse_calendar(&args.calendar)?;
    let from = EventProperties::parse_from(&args.from)?;
    let to = EventProperties::parse_to(&args.to)?;
    let summary = EventProperties::parse_summary(&args.summary)?;
    let location = EventProperties::parse_location(&args.location)?;
    Ok(EventProperties {
      calendar,
      from,
      to,
      summary,
      location,
    })
  }

  fn parse_from(arg: &str) -> KhResult<IcalTime> {
    if arg.is_empty() {
      Err("no start date/time given")?
    };
    let time = arg.parse::<IcalTime>()?;
    Ok(time)
  }

  fn parse_to(arg: &str) -> KhResult<IcalTime> {
    if arg.is_empty() {
      Err("no end date/time given")?
    };
    let time = arg.parse::<IcalTime>()?;
    Ok(time)
  }

  fn parse_location(arg: &str) -> KhResult<String> {
    if arg.is_empty() {
      Err("no location given")?
    };
    Ok(arg.to_string())
  }

  fn parse_summary(arg: &str) -> KhResult<String> {
    if arg.is_empty() {
      Err("no summary given")?
    };
    Ok(arg.to_string())
  }

  fn parse_calendar(arg: &str) -> KhResult<String> {
    if arg.is_empty() {
      Err("no calendar given")?
    };
    let cal = arg.to_string();
    if !calendars::calendar_list().contains(&cal) {
      Err("calendar does not exist")?
    }
    Ok(cal)
  }
}

pub fn do_new(args: &NewArgs) -> KhResult<()> {
  let uid = misc::make_new_uid();
  let ep = EventProperties::parse_from_args(args)?;

  let mut path = defaults::get_caldir();
  path.push(&ep.calendar);
  path.push(&(uid.clone() + ".ics"));

  let new_cal = IcalVCalendar::from_str(TEMPLATE_EVENT, Some(&path))?
    .with_uid(&uid)?
    .with_dtstamp_now()
    .with_last_modified_now()
    .with_eventprops(&ep);

  let khline = KhLine::from(&new_cal);

  fileutil::write_cal(&new_cal)?;

  cursorfile::write_cursorfile(&khline.to_string())?;
  khprintln!("{}", khline);

  Ok(())
}

impl IcalVCalendar {
  fn with_eventprops(self, ep: &EventProperties) -> Self {
    self
      .with_dtstart(&ep.from)
      .with_dtend(&ep.to)
      .with_summary(&ep.summary)
      .with_location(&ep.location)
  }
}

static TEMPLATE_EVENT: &str = indoc!(
  "
  BEGIN:VCALENDAR
  VERSION:2.0
  PRODID:-//khaleesi //EN
  BEGIN:VTIMEZONE
  TZID:Europe/Berlin
  BEGIN:STANDARD
  DTSTART:19711025T030000
  TZOFFSETFROM:+0200
  TZOFFSETTO:+0100
  RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10
  END:STANDARD
  BEGIN:DAYLIGHT
  DTSTART:19710329T020000
  TZOFFSETFROM:+0100
  TZOFFSETTO:+0200
  RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=3
  END:DAYLIGHT
  END:VTIMEZONE
  BEGIN:VEVENT
  SUMMARY:<<EDIT ME>>
  LOCATION:<<EDIT ME>>
  DTSTART;TZID=Europe/Berlin;VALUE=DATE-TIME:20181026T133000
  DTEND;TZID=Europe/Berlin;VALUE=DATE-TIME:20181026T160000
  DTSTAMP;VALUE=DATE-TIME:20181022T145405Z
  UID:foo
  END:VEVENT
  END:VCALENDAR
"
);

#[cfg(test)]
mod integration {
  use assert_fs::prelude::*;
  use predicates::prelude::*;

  use super::*;
  use crate::testdata;
  use crate::testutils;

  #[test]
  fn test_parse_calendar() {
    let _testdir = testutils::prepare_testdir("testdir_two_cals");
    let calendar = EventProperties::parse_calendar("first").unwrap();
    assert_eq!("first", calendar);
  }

  #[test]
  fn test_parse_calendar_neg() {
    let _testdir = testutils::prepare_testdir("testdir_two_cals");
    let calendar = EventProperties::parse_calendar("");
    assert!(calendar.is_err());
    let calendar = EventProperties::parse_calendar("foo");
    assert!(calendar.is_err());
  }

  #[test]
  fn test_parse_location() {
    let location = EventProperties::parse_location("room 101").unwrap();
    assert_eq!("room 101", location);
  }

  #[test]
  fn test_parse_location_neg() {
    let location = EventProperties::parse_location("");
    assert!(location.is_err());
  }

  #[test]
  fn test_parse_summary() {
    let summary = EventProperties::parse_summary("first").unwrap();
    assert_eq!("first", summary);
  }

  #[test]
  fn test_parse_summary_neg() {
    let summary = EventProperties::parse_summary("");
    assert!(summary.is_err());
  }

  #[test]
  fn test_parse_from() {
    testdata::setup();
    let from = EventProperties::parse_from("2017-07-14T17:45:00").unwrap();
    let expected = IcalTime::floating_ymd(2017, 7, 14).and_hms(17, 45, 0);
    assert_eq!(expected, from);
  }

  #[test]
  fn test_parse_from_neg() {
    let from = EventProperties::parse_from("blödsinn");
    assert!(from.is_err());
    let from = EventProperties::parse_from("");
    assert!(from.is_err());
  }

  #[test]
  fn test_parse_to() {
    testdata::setup();
    let to = EventProperties::parse_to("2017-07-14T17:45:00").unwrap();
    let expected = IcalTime::floating_ymd(2017, 7, 14).and_hms(17, 45, 0);
    assert_eq!(expected, to);
  }

  #[test]
  fn test_parse_to_neg() {
    let to = EventProperties::parse_to("quatsch");
    assert!(to.is_err());
    let to = EventProperties::parse_to("");
    assert!(to.is_err());
  }

  #[test]
  fn test_parse_from_args() {
    let _testdir = testutils::prepare_testdir("testdir_two_cals");
    let args = NewArgs {
      calendar: "second".to_string(),
      from: "2017-11-03T12:30:00".to_string(),
      to: "2017-11-07T11:11:00".to_string(),
      summary: "summary text".to_string(),
      location: "location text".to_string(),
    };
    let ep = EventProperties::parse_from_args(&args).unwrap();
    assert_eq!("second".to_string(), ep.calendar);
    assert_eq!("summary text".to_string(), ep.summary);
    assert_eq!("location text".to_string(), ep.location);
  }

  //#[test]
  //fn test_parse_from_args_neg() {
  //  let args = &["1", "2", "3", "4"];
  //  let ep = EventProperties::parse_from_args(args);
  //  assert!(ep.is_err());
  //}

  #[test]
  fn test_with_eventprops() {
    testdata::setup();

    let calendar = "foo".to_string();
    let from = IcalTime::floating_ymd(2015, 04, 17).and_hms(8, 17, 3);
    let to = IcalTime::floating_ymd(2015, 05, 17).and_hms(8, 17, 3);
    let summary = "summary";
    let location = "home";
    let ep = EventProperties {
      calendar,
      from: from.clone(),
      to: to.clone(),
      summary: summary.to_string(),
      location: location.to_string(),
    };

    let _testdir = testutils::prepare_testdir("testdir");
    let khline = "twodaysacrossbuckets.ics".parse::<KhLine>().unwrap();

    let cal =