diff options
Diffstat (limited to 'src/parse.rs')
-rw-r--r-- | src/parse.rs | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/src/parse.rs b/src/parse.rs index 079e83a..a896cc2 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -1,10 +1,34 @@ +use std::fs::File; +use std::io::prelude::*; use std::str::FromStr; use chrono::{Datelike, NaiveDate, NaiveTime, Weekday, Duration, Timelike}; -// mod task; use crate::task::*; +use crate::dates::*; +use crate::html_calendar::*; -pub fn parse_file_str(file_str: &str) -> Vec<Task> { +// Translates a Path into a vector of tasks. Panics if the path cannot be opened or read from. +pub fn tasks_from_path(wtd_path: &WTDPath) -> Vec<Task> { + let path = wtd_path.path; + let display = path.display(); + + // Open the path in read-only mode, returns `io::Result<File>` + let mut file = match File::open(&path) { + Err(why) => panic!("Error opening {}: {}", display, why), + Ok(file) => file, + }; + + // Read the file contents into a string, returns `io::Result<usize>` + let mut s = String::new(); + match file.read_to_string(&mut s) { + Err(why) => panic!("Couldn't read {}: {}", display, why), + Ok(_) => { + return parse_file_str(&s); + } + } +} + +fn parse_file_str(file_str: &str) -> Vec<Task> { let mut tasks = Vec::new(); let mut start_date = None; let mut the_date = None; |