summaryrefslogtreecommitdiffstats
path: root/src/actions/edit.rs
blob: 5fee9276fb075e91f435732f04bd3b26b58bc711 (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
use tempfile::NamedTempFile;

use crate::backup::backup;
use crate::edit;
use crate::input;
use crate::khline::KhLine;
use crate::utils::fileutil;
use crate::KhResult;

pub fn do_edit() -> KhResult<()> {
  let khline = input::default_input_khline()?;
  edit(&khline)
}

fn edit(khline: &KhLine) -> KhResult<()> {
  let tempfile = NamedTempFile::new()?;
  let calendar = khline.to_cal()?;

  fileutil::write_file(tempfile.path(), &calendar.to_string())?;
  edit::edit_loop(&tempfile.path())?;

  let backup_path = backup(&khline).unwrap();
  info!("Backup written to {}", backup_path.display());

  let edited_cal = KhLine::new(tempfile.path(), None).to_cal()?.with_dtstamp_now().with_last_modified_now();
  fileutil::write_file(&khline.path, &edited_cal.to_string())?;
  info!("Successfully edited file {}", khline.path.display());

  Ok(())
}

#[cfg(test)]
mod integration {
  use super::*;

  use crate::testutils::prepare_testdir;
  use crate::icalwrap::IcalComponent;

  #[test]
  fn edit_test() {
    let _testdir = prepare_testdir("testdir");

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

    assert!(edit(&khline).is_ok());
    let event = khline.to_event().unwrap();

    let dtstamp_prop = ical::icalproperty_kind_ICAL_DTSTAMP_PROPERTY;
    assert_eq!("20130101T010203Z", event.get_property(dtstamp_prop).unwrap().get_value());

    let last_modified_kind = ical::icalproperty_kind_ICAL_LASTMODIFIED_PROPERTY;
    assert_eq!("20130101T010203Z", event.get_property(last_modified_kind).unwrap().get_value());
  }
}