From ff6ac86094ba08b4267599e69c442dc8ff1ee092 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Thu, 27 Dec 2018 14:01:25 +0100 Subject: Move executable to separate dir. --- src/bin/primstav/locale.rs | 66 ++++++++++++++++++++++++++++++++++++ src/bin/primstav/main.rs | 83 ++++++++++++++++++++++++++++++++++++++++++++++ src/locale.rs | 66 ------------------------------------ src/main.rs | 83 ---------------------------------------------- 4 files changed, 149 insertions(+), 149 deletions(-) create mode 100644 src/bin/primstav/locale.rs create mode 100644 src/bin/primstav/main.rs delete mode 100644 src/locale.rs delete mode 100644 src/main.rs diff --git a/src/bin/primstav/locale.rs b/src/bin/primstav/locale.rs new file mode 100644 index 0000000..26e3aeb --- /dev/null +++ b/src/bin/primstav/locale.rs @@ -0,0 +1,66 @@ +// This file is a part of primstav. +// Copyright (C) 2018 Harald Eilertsen +// +// 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 . + +#![allow(dead_code)] + +use chrono::Datelike; + +pub struct Locale { + weekdays: [&'static str; 7], + months: [&'static str; 12], +} + +impl Locale { + pub fn format_date(&self, d: &chrono::NaiveDate) -> String { + format!("{} {}. {}, {}", + self.weekdays[d.weekday() as usize - 1], + d.day(), + self.months[d.month() as usize - 1], + d.year()) + } +} + +fn from_locale_str(l: &str) -> &'static Locale { + match &l[0..2] { + "nb" => &NB, + "nn" => &NN, + _ => &EN, // Return default EN locale if we don't have a locale for the given str. + } +} + +pub fn get() -> &'static Locale { + match std::env::var("LC_TIME") { + Ok(l) => { + from_locale_str(&l) + }, + Err(_) => &EN, // Return default EN locale if no locale is defined + } +} + +pub const EN: Locale = Locale { + weekdays: ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"], + months: ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"], +}; + +pub const NB: Locale = Locale { + weekdays: ["mandag", "tirsdag", "onsdag", "torsdag", "fredag", "lørdag", "søndag"], + months: ["januar", "februar", "mars", "april", "mai", "juni", "juli", "august", "september", "oktober", "november", "desember"], +}; + +pub const NN: Locale = Locale { + weekdays: ["måndag", "tysdag", "onsdag", "torsdag", "fredag", "laurdag", "søndag"], + months: ["januar", "februar", "mars", "april", "mai", "juni", "juli", "august", "september", "oktober", "november", "desember"], +}; diff --git a/src/bin/primstav/main.rs b/src/bin/primstav/main.rs new file mode 100644 index 0000000..b93b19e --- /dev/null +++ b/src/bin/primstav/main.rs @@ -0,0 +1,83 @@ +// primstav - look up marked days on the norwegian primstav. +// Copyright (C) 2018 Harald Eilertsen +// +// 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 . + +use chrono; +use serde_yaml; +use std::io::BufReader; +use std::iter::Iterator; +use std::fs::File; + +mod locale; + +use primstav::{Primstav, Mark}; + +fn capitalize(s: &str) -> String { + let mut chars = s.chars(); + match chars.next() { + Some(ch) => ch.to_uppercase().chain(chars).collect::(), + 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("")); +} + +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::>(); + 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::new(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))); + + match p.get(&local_date) { + Some(d) => print_entry(&d), + None => () + } +} diff --git a/src/locale.rs b/src/locale.rs deleted file mode 100644 index 26e3aeb..0000000 --- a/src/locale.rs +++ /dev/null @@ -1,66 +0,0 @@ -// This file is a part of primstav. -// Copyright (C) 2018 Harald Eilertsen -// -// 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 . - -#![allow(dead_code)] - -use chrono::Datelike; - -pub struct Locale { - weekdays: [&'static str; 7], - months: [&'static str; 12], -} - -impl Locale { - pub fn format_date(&self, d: &chrono::NaiveDate) -> String { - format!("{} {}. {}, {}", - self.weekdays[d.weekday() as usize - 1], - d.day(), - self.months[d.month() as usize - 1], - d.year()) - } -} - -fn from_locale_str(l: &str) -> &'static Locale { - match &l[0..2] { - "nb" => &NB, - "nn" => &NN, - _ => &EN, // Return default EN locale if we don't have a locale for the given str. - } -} - -pub fn get() -> &'static Locale { - match std::env::var("LC_TIME") { - Ok(l) => { - from_locale_str(&l) - }, - Err(_) => &EN, // Return default EN locale if no locale is defined - } -} - -pub const EN: Locale = Locale { - weekdays: ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"], - months: ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"], -}; - -pub const NB: Locale = Locale { - weekdays: ["mandag", "tirsdag", "onsdag", "torsdag", "fredag", "lørdag", "søndag"], - months: ["januar", "februar", "mars", "april", "mai", "juni", "juli", "august", "september", "oktober", "november", "desember"], -}; - -pub const NN: Locale = Locale { - weekdays: ["måndag", "tysdag", "onsdag", "torsdag", "fredag", "laurdag", "søndag"], - months: ["januar", "februar", "mars", "april", "mai", "juni", "juli", "august", "september", "oktober", "november", "desember"], -}; diff --git a/src/main.rs b/src/main.rs deleted file mode 100644 index b93b19e..0000000 --- a/src/main.rs +++ /dev/null @@ -1,83 +0,0 @@ -// primstav - look up marked days on the norwegian primstav. -// Copyright (C) 2018 Harald Eilertsen -// -// 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 . - -use chrono; -use serde_yaml; -use std::io::BufReader; -use std::iter::Iterator; -use std::fs::File; - -mod locale; - -use primstav::{Primstav, Mark}; - -fn capitalize(s: &str) -> String { - let mut chars = s.chars(); - match chars.next() { - Some(ch) => ch.to_uppercase().chain(chars).collect::(), - 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("")); -} - -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::>(); - 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::new(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))); - - match p.get(&local_date) { - Some(d) => print_entry(&d), - None => () - } -} -- cgit v1.2.3