aboutsummaryrefslogblamecommitdiffstats
path: root/src/main.rs
blob: c56ffb35b8948b91437b3172e747add15dea7789 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
















                                                                         
                              
               
                               


                        

                          

           
                                  


                                                                       



                              





                                                           
                                   

 




                                   
                                    




                                  
 





                                              



                                           

                                                       
     


                            

                                              

         



                                                                     


                                                   






                                                                              

                                                                 

                                                    
                             



                                   
// primstav - look up marked days on the norwegian primstav.
// Copyright (C) 2018  Harald Eilertsen <haraldei@anduin.net>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use chrono;
use serde_derive::Deserialize;
use serde_yaml;
use std::collections::BTreeMap;
use std::io::BufReader;
use std::iter::Iterator;
use std::fs::File;
use std::string::ToString;

mod locale;

fn capitalize(s: &str) -> String {
    let mut chars = s.chars();
    match chars.next() {
        Some(ch) => ch.to_uppercase().chain(chars).collect::<String>(),
        None => String::new(),
    }
}

#[test]
fn test_capitalize() {
    assert_eq!("Flaccid", capitalize("flaccid"));
    assert_eq!("Ångström", capitalize("ångström"));
    assert_eq!("Å i lofoten", capitalize("å i lofoten"));
    assert_eq!("Ååååh", capitalize("ååååh"));
    assert_eq!("", capitalize(""));
}

#[derive(Deserialize)]
struct Mark {
    name: String,
    alternative_names: Vec<String>,
    symbols: Vec<String>,
    links: BTreeMap<String, String>,
}

struct Primstav {
    marks: BTreeMap<String, Mark>,
}

fn print_entry(m: &Mark) {
    println!("{}", capitalize(&m.name));

    if !m.alternative_names.is_empty() {
        let names = m.alternative_names.iter()
            .map(|v| capitalize(&v))
            .collect::<Vec<_>>();
        println!("({})", names.join(", "));
    }

    if !m.symbols.is_empty() {
        println!("Symboler: {}", m.symbols.join(", "));
    }

    if !m.links.is_empty() {
        println!("Kilder:");
        for (source, url) in m.links.iter() {
            println!("  {}: {}", source, url);
        }
    }
}

fn main() {
    let f = BufReader::new(File::open("data/primstav.yml").unwrap());
    let p = Primstav {
        marks: serde_yaml::from_reader(f).unwrap(),
    };

    let mut args = std::env::args().skip(1);
    let local_date = match args.next() {
        Some(s) => chrono::NaiveDate::parse_from_str(&s, "%d-%m-%Y").unwrap(),
        None => chrono::Local::today().naive_utc(),
    };

    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) {
        Some(d) => print_entry(&d),
        None => ()
    }
}