diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2018-12-26 23:06:46 +0100 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2018-12-26 23:06:46 +0100 |
commit | ae2c49a315e48dc16dce28a12f215c50f20e9324 (patch) | |
tree | d34a977d78f65528d43d48e3ba7eea02cc0ed41b /src | |
parent | bd1d3827f1a0f36d78c48e714d8a0214d53c479f (diff) | |
download | primstav-ae2c49a315e48dc16dce28a12f215c50f20e9324.tar.gz primstav-ae2c49a315e48dc16dce28a12f215c50f20e9324.tar.bz2 primstav-ae2c49a315e48dc16dce28a12f215c50f20e9324.zip |
Move more functionality to lib.
Add a constructor for initializing the Primstav object, and a method
to get the entry for a given date if the entry exists.
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 12 | ||||
-rw-r--r-- | src/main.rs | 8 |
2 files changed, 14 insertions, 6 deletions
@@ -28,3 +28,15 @@ pub struct Mark { pub struct Primstav { pub marks: BTreeMap<String, Mark>, } + +impl Primstav { + pub fn new(marks: BTreeMap<String, Mark>) -> Primstav { + Primstav { marks } + } + + /// Get an entry for the given date if one exists. + pub fn get(&self, date: &chrono::NaiveDate) -> Option<&Mark> { + let key = date.format("%d%m").to_string(); + self.marks.get(&key) + } +} diff --git a/src/main.rs b/src/main.rs index fa18e1d..b93b19e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,7 +19,6 @@ use serde_yaml; use std::io::BufReader; use std::iter::Iterator; use std::fs::File; -use std::string::ToString; mod locale; @@ -66,9 +65,7 @@ fn print_entry(m: &Mark) { fn main() { let f = BufReader::new(File::open("data/primstav.yml").unwrap()); - let p = Primstav { - marks: serde_yaml::from_reader(f).unwrap(), - }; + let p = Primstav::new(serde_yaml::from_reader(f).unwrap()); let mut args = std::env::args().skip(1); let local_date = match args.next() { @@ -79,8 +76,7 @@ fn main() { let locale = locale::get(); println!("{}", capitalize(&locale.format_date(&local_date))); - let key = local_date.format("%d%m").to_string(); - match p.marks.get(&key) { + match p.get(&local_date) { Some(d) => print_entry(&d), None => () } |