diff options
author | Nora <nora.widdecke@tu-bs.de> | 2018-10-07 14:10:54 +0200 |
---|---|---|
committer | Nora <nora.widdecke@tu-bs.de> | 2018-10-09 15:10:59 +0200 |
commit | a9ab4f6892b82b84b4af7d5bc8ec3c3f37db5d95 (patch) | |
tree | 9c91f320cb2af2f808e70c405f98b7838e198523 | |
parent | 1ad33a2596a021e271c0a42f85f54a7dc52f378f (diff) |
move index functition to module
-rw-r--r-- | .gitignore | 5 | ||||
-rw-r--r-- | src/index.rs | 68 | ||||
-rw-r--r-- | src/main.rs | 81 |
3 files changed, 74 insertions, 80 deletions
@@ -4,10 +4,13 @@ # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries # More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html -Cargo.lock +# Cargo.lock # These are backup files generated by rustfmt **/*.rs.bk # Don't commit ics files in this repo (typically) *.ics + +# Don't commit the Index folder +/Index/ diff --git a/src/index.rs b/src/index.rs new file mode 100644 index 0000000..94bbedc --- /dev/null +++ b/src/index.rs @@ -0,0 +1,68 @@ +extern crate chrono; +extern crate libc; + +use chrono::{Datelike, Duration}; +use std::collections::HashMap; +use std::fs; +use ::icalwrap::*; +use ::utils; + +fn get_buckets(comp: &mut Icalcomponent) -> Vec<String> { + let mut buckets: Vec<String> = comp + .map(|x| { + let mut start_date = x.get_dtstart(); + let end_date = x.get_dtend(); + let mut buckets = Vec::new(); + while start_date.iso_week() <= end_date.iso_week() { + let bucket = format!( + "{}-{:02}", + start_date.iso_week().year(), + start_date.iso_week().week() + ); + buckets.push(bucket); + start_date = start_date.checked_add_signed(Duration::days(7)).unwrap(); + } + buckets + }).flatten() + .collect(); + buckets.sort(); + buckets.dedup(); + buckets +} + +pub fn index_dir(dir: &str) { + let mut buckets: HashMap<String, Vec<String>> = HashMap::new(); + + if let Ok(entries) = fs::read_dir(dir) { + for entry in entries { + if let Ok(entry) = entry { + // Here, `entry` is a `DirEntry`. + if ! entry.path().is_file() { + continue + } + if entry + .path() + .extension() + .map_or(false, |extension| extension == "ics") + { + if let Ok(contents) = utils::read_file_to_string(&entry.path()) { + let mut comp = Icalcomponent::from_str(&contents); // + let comp_buckets = get_buckets(&mut comp); + for bucketid in comp_buckets { + buckets + .entry(bucketid) + .and_modify(|items| items.push(comp.get_uid())) + .or_insert(::utils::vec_from_string(comp.get_uid())); + } + } + } + } + } + } + info!("{} buckets", buckets.len()); + for (key, val) in buckets.iter() { + if let Err(error) = utils::write_file(key, val.join("\n")) { + error!("{}", error); + } + } +} diff --git a/src/main.rs b/src/main.rs index e66e72f..9c3a0b1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ pub mod prettyprint; pub mod utils; pub mod ical; pub mod cal; +pub mod index; extern crate chrono; extern crate libc; @@ -13,36 +14,7 @@ extern crate log; extern crate simple_logger; use std::env; -//use std::fs::{File, read_dir}; -use chrono::{Datelike, Duration}; -use std::collections::HashMap; use std::path::Path; -use std::fs; - -use icalwrap::*; - -pub fn get_buckets(comp: &mut Icalcomponent) -> Vec<String> { - let mut buckets: Vec<String> = comp - .map(|x| { - let mut start_date = x.get_dtstart(); - let end_date = x.get_dtend(); - let mut buckets = Vec::new(); - while start_date.iso_week() <= end_date.iso_week() { - let bucket = format!( - "{}-{:02}", - start_date.iso_week().year(), - start_date.iso_week().week() - ); - buckets.push(bucket); - start_date = start_date.checked_add_signed(Duration::days(7)).unwrap(); - } - buckets - }).flatten() - .collect(); - buckets.sort(); - buckets.dedup(); - buckets -} fn main() { simple_logger::init().unwrap(); @@ -69,54 +41,5 @@ fn action_prettyprint(args: &[String]) { fn action_index(args: &[String]) { //let filename = &args[1]; let dir = &args[0]; - let mut buckets: HashMap<String, Vec<String>> = HashMap::new(); - - if let Ok(entries) = fs::read_dir(dir) { - for entry in entries { - if let Ok(entry) = entry { - // Here, `entry` is a `DirEntry`. - if ! entry.path().is_file() { - continue - } - if entry - .path() - .extension() - .map_or(false, |extension| extension == "ics") - { - if let Ok(contents) = utils::read_file_to_string(&entry.path()) { - let mut comp = Icalcomponent::from_str(&contents); // - let comp_buckets = get_buckets(&mut comp); - for bucketid in comp_buckets { - buckets - .entry(bucketid) - .and_modify(|items| items.push(comp.get_uid())) - .or_insert(::utils::vec_from_string(comp.get_uid())); - } - } - } - } - } - } - info!("{} buckets", buckets.len()); - for (key, val) in buckets.iter() { - if let Err(error) = utils::write_file(key, val.join("\n")) { - error!("{}", error); - } - } - - // //println!("Searching for {}", query); - // println!("In file {}", filename); - // - // let mut f = File::open(filename).expect("file not found"); - // - // let mut contents = String::new(); - // f.read_to_string(&mut contents) - // .expect("something went wrong reading the file"); - // - // println!("With text:\n{}", contents); - // - // let mut comp = parse_component(&contents); - // - // let mut foo = get_buckets(&mut comp); - // println!("{}", foo.join("\n")); + index::index_dir(dir) } |