diff options
author | Nora <nora.widdecke@tu-bs.de> | 2019-01-19 01:14:57 +0100 |
---|---|---|
committer | Nora <nora.widdecke@tu-bs.de> | 2019-01-19 02:09:33 +0100 |
commit | aa42b59c2f8eaeedc17a1a559057d2ffa4ee9e87 (patch) | |
tree | 63ca817f4ac79d32112a798455b2010d084c1062 | |
parent | eb81302a4539bcf2e10d2cc8dd1e0c25526cf7f2 (diff) |
fileutil and khline: use io::Results
-rw-r--r-- | src/actions/unroll.rs | 13 | ||||
-rw-r--r-- | src/icalwrap/icalvcalendar.rs | 7 | ||||
-rw-r--r-- | src/khline.rs | 7 | ||||
-rw-r--r-- | src/utils/fileutil.rs | 49 |
4 files changed, 31 insertions, 45 deletions
diff --git a/src/actions/unroll.rs b/src/actions/unroll.rs index 76e6f43..8bd38bb 100644 --- a/src/actions/unroll.rs +++ b/src/actions/unroll.rs @@ -6,16 +6,16 @@ use KhResult; pub fn action_unroll(args: &[String]) -> KhResult<()> { let file = &args[0]; let filepath = Path::new(file); - do_unroll(filepath); + do_unroll(filepath)?; Ok(()) } -fn do_unroll(filepath: &Path) { - let cal = filepath.to_str().ok_or_else(|| "str to path failed".to_string()) - .and_then(|path| path.parse::<KhLine>()) - .and_then(|khline| khline.to_cal()) - .unwrap(); +fn do_unroll(filepath: &Path) -> KhResult<()> { + let path = filepath.to_str().ok_or_else(|| "str to path failed")?; + let khline = path.parse::<KhLine>()?; + let cal = khline.to_cal()?; + for event in cal.events_iter() { if event.is_recur_master() { let recurs = event.get_recur_datetimes(); @@ -24,4 +24,5 @@ fn do_unroll(filepath: &Path) { } } } + Ok(()) } diff --git a/src/icalwrap/icalvcalendar.rs b/src/icalwrap/icalvcalendar.rs index 90811c0..b76e853 100644 --- a/src/icalwrap/icalvcalendar.rs +++ b/src/icalwrap/icalvcalendar.rs @@ -2,6 +2,7 @@ use chrono::{DateTime, Local}; use std::ffi::{CStr, CString}; use std::path::{PathBuf,Path}; use std::rc::Rc; +use std::io; use super::IcalVEvent; use super::IcalComponent; @@ -68,18 +69,18 @@ impl IcalVCalendar { self } - pub fn from_str(str: &str, path: Option<&Path>) -> Result<Self, String> { + pub fn from_str(str: &str, path: Option<&Path>) -> io::Result<Self> { unsafe { let c_str = CString::new(str).unwrap(); let parsed_cal = ical::icalparser_parse_string(c_str.as_ptr()); if parsed_cal.is_null() { - return Err("could not read component".to_string()); + return Err(io::Error::new(io::ErrorKind::Other, "calendar has no path")) } let kind = ical::icalcomponent_isa(parsed_cal); if kind != ical::icalcomponent_kind_ICAL_VCALENDAR_COMPONENT { let kind = CStr::from_ptr(ical::icalcomponent_kind_to_string(kind)).to_string_lossy(); - return Err(format!("expected VCALENDAR component, got {}", kind)); + return Err(io::Error::new(io::ErrorKind::Other, format!("expected VCALENDAR component, got {}", kind))) } let mut cal = IcalVCalendar::from_ptr(parsed_cal); diff --git a/src/khline.rs b/src/khline.rs index 9ef55ab..88d8c9c 100644 --- a/src/khline.rs +++ b/src/khline.rs @@ -1,6 +1,7 @@ use std::fmt; -use std::str::FromStr; +use std::io; use std::path::{PathBuf,Path}; +use std::str::FromStr; use chrono::prelude::*; use icalwrap::{IcalVCalendar,IcalVEvent}; @@ -23,7 +24,7 @@ impl KhLine { Self { path, time } } - pub fn to_cal(&self) -> Result<IcalVCalendar, String> { + pub fn to_cal(&self) -> io::Result<IcalVCalendar> { let mut calendar = fileutil::read_calendar_from_path(&self.path)?; if let Some(time) = self.time { calendar = calendar.with_internal_timestamp(time); @@ -31,7 +32,7 @@ impl KhLine { Ok(calendar) } - pub fn to_event(&self) -> Result<IcalVEvent, String> { + pub fn to_event(&self) -> io::Result<IcalVEvent> { self.to_cal().map(|cal| cal.get_principal_event()) } diff --git a/src/utils/fileutil.rs b/src/utils/fileutil.rs index d05cec7..2527b25 100644 --- a/src/utils/fileutil.rs +++ b/src/utils/fileutil.rs @@ -40,10 +40,10 @@ pub fn append_file(filepath: &Path, contents: &str) -> io::Result<()> { file.write_all(contents.as_bytes()) } -pub fn write_cal(cal: &IcalVCalendar) -> Result<(), String> { +pub fn write_cal(cal: &IcalVCalendar) -> io::Result<()> { match cal.get_path() { - Some(path) => write_file(&path, &cal.to_string()).map_err(|error| format!("{}", error)), - None => Err("calendar has no path".to_string()), + Some(path) => write_file(&path, &cal.to_string()), + None => Err(io::Error::new(io::ErrorKind::Other, "calendar has no path")), } } @@ -51,28 +51,20 @@ pub fn read_lines_from_file(filepath: &Path) -> io::Result<impl Iterator<Item = let f = fs::File::open(filepath)?; let f = BufReader::new(f); let lines: Result<Vec<String>, io::Error> = f.lines().collect(); - match lines { - Ok(result) => Ok(result.into_iter()), - Err(error) => Err(error) - } + lines.map(|result| result.into_iter()) } -pub fn read_single_char_from_stdin() -> Result<char, String> { +pub fn read_single_char_from_stdin() -> io::Result<char> { let stdin = std::io::stdin(); let stdinlock = stdin.lock(); read_single_char(stdinlock) } -pub fn read_single_char(mut source: impl BufRead) -> Result<char, String> { +pub fn read_single_char(mut source: impl BufRead) -> io::Result<char> { let mut buf = String::new(); - if let Err(error) = source.read_line(&mut buf) { - return Err(format!("{}", error)); - } + source.read_line(&mut buf)?; - match buf.chars().next() { - Some(c) => Ok(c), - None => Err("failed to read from {}".to_string()), - } + buf.chars().next().ok_or_else(|| io::Error::new(io::ErrorKind::Other, "calendar has no path")) } pub fn read_lines_from_stdin() -> io::Result<Vec<String>> { @@ -82,29 +74,20 @@ pub fn read_lines_from_stdin() -> io::Result<Vec<String>> { lines.collect() } -pub fn read_file_to_string(path: &Path) -> Result<String, String> { - if let Ok(mut file) = fs::File::open(&path) { - let mut contents = String::new(); - if file.read_to_string(&mut contents).is_ok() { - Ok(contents) - } else { - Err("Something went wrong reading the file".to_string()) - } - } else { - Err(format!("Could not open {} for reading", path.display())) - } +pub fn read_file_to_string(path: &Path) -> io::Result<String> { + let mut file = fs::File::open(&path)?; + let mut contents = String::new(); + file.read_to_string(&mut contents)?; + Ok(contents) } -pub fn read_calendar_from_path(path: &Path) -> Result<IcalVCalendar, String> { +pub fn read_calendar_from_path(path: &Path) -> io::Result<IcalVCalendar> { trace!("Reading calendar from {}", path.to_string_lossy()); - let content = match fs::read_to_string(path) { - Ok(content) => content, - Err(error) => return Err(format!("{} {:?}", error, path)) - }; + let content = fs::read_to_string(path)?; IcalVCalendar::from_str(&content, Some(path)) } -pub fn read_calendars_from_files(files: &mut Iterator<Item = String>) -> Result<Vec<IcalVCalendar>, String> { +pub fn read_calendars_from_files(files: &mut Iterator<Item = String>) -> io::Result<Vec<IcalVCalendar>> { files .map(|line| line.parse::<KhLine>()) .flatten() |