diff --git a/Cargo.lock b/Cargo.lock index 335a880..2f2952c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,91 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +[[package]] +name = "autocfg" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" + +[[package]] +name = "chrono" +version = "0.4.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73" +dependencies = [ + "libc", + "num-integer", + "num-traits", + "time", + "winapi", +] + +[[package]] +name = "libc" +version = "0.2.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8916b1f6ca17130ec6568feccee27c156ad12037880833a3b842a823236502e7" + [[package]] name = "matsudo-gomi-hook" version = "0.1.0" +dependencies = [ + "chrono", +] + +[[package]] +name = "num-integer" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" +dependencies = [ + "autocfg", +] + +[[package]] +name = "time" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" +dependencies = [ + "libc", + "wasi", + "winapi", +] + +[[package]] +name = "wasi" +version = "0.10.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml index 667352c..4b39e23 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,4 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +chrono = "0.4.19" diff --git a/src/calendar.rs b/src/calendar.rs new file mode 100644 index 0000000..cbf84df --- /dev/null +++ b/src/calendar.rs @@ -0,0 +1,9 @@ +use chrono::{DateTime, Local}; + +use crate::gomi::Gomi; + +pub(crate) mod matsudo; + +pub(crate) trait Calendar { + fn gomi_at(&self, time: DateTime) -> Vec; +} diff --git a/src/calendar/matsudo.rs b/src/calendar/matsudo.rs new file mode 100644 index 0000000..879af71 --- /dev/null +++ b/src/calendar/matsudo.rs @@ -0,0 +1,83 @@ +use crate::calendar::Calendar; +use crate::gomi::Gomi; + +use chrono::{DateTime, Datelike, Local, Weekday, Weekday::*}; + +// Defines the gomi rules for Matsudo using a macro. +macro_rules! matsudo_rules { + ( + $current_base: expr, + $current_time: expr, + // This repetition represents each rules based on the base day. + $(( + $base: path, + ($burnable1: path, $burnable2: path, $burnable3: path), + ($glass_week_num: expr, $glass_week_day: path), // The Nth day of X + $recycle_plastic: path, + $other_plastic: path + )),* + $(,)? + ) => { + // Matches the current base day for the location. + match ($current_base) { + $( + $base => (vec![ + match ($current_time.weekday()) { + $base => Some(Gomi::new("資源ごみ")), + $burnable1 | $burnable2 | $burnable3 => Some(Gomi::new("燃やせるごみ")), + $recycle_plastic => Some(Gomi::new("リサイクルするプラスチック")), + $other_plastic => Some(Gomi::new("その他のプラスチックなどのごみ")), + _ => None, + }, + if (week_num_by_weekday($current_time, $glass_week_day) == $glass_week_num) { + Some(Gomi::new("陶磁器・ガラスなどのごみ")) + } else { + None + } + ] + // Converts Vec> to Vec, remaining only Some elements. + .into_iter() + .flat_map(|x| x) + .collect() + ) + ),*, + // Ignores the unknown base day, returning an empty Vec. + _ => vec![], + } + } +} + +pub(crate) struct Matsudo { + base: Weekday, +} + +impl Matsudo { + pub(crate) fn new(base: Weekday) -> Self { + Self { base } + } +} + +impl Calendar for Matsudo { + fn gomi_at(&self, time: DateTime) -> Vec { + // Using the defined rules, determines which Gomi's are available to take out. + matsudo_rules!( + self.base, + time, + (Mon, (Tue, Thu, Sat), (2, Thu), Wed, Fri), + (Tue, (Mon, Wed, Fri), (2, Wed), Thu, Sat), + (Wed, (Tue, Thu, Sat), (3, Thu), Fri, Mon), + (Thu, (Mon, Wed, Fri), (3, Wed), Sat, Tue), + (Fri, (Tue, Thu, Sat), (4, Thu), Mon, Wed), + (Sat, (Mon, Wed, Fri), (4, Wed), Tue, Thu), + ) + } +} + +fn week_num_by_weekday(time: DateTime, weekday: Weekday) -> u32 { + time.day() / 7 + + if time.weekday().num_days_from_sunday() < weekday.num_days_from_sunday() { + 0 + } else { + 1 + } +} diff --git a/src/gomi.rs b/src/gomi.rs new file mode 100644 index 0000000..1fd6655 --- /dev/null +++ b/src/gomi.rs @@ -0,0 +1,19 @@ +use std::fmt::{Display, Formatter, Result}; + +pub(crate) struct Gomi { + name: String, +} + +impl Gomi { + pub(crate) fn new(name: &str) -> Self { + Self { + name: name.to_string(), + } + } +} + +impl Display for Gomi { + fn fmt(&self, f: &mut Formatter<'_>) -> Result { + write!(f, "{}", self.name) + } +} diff --git a/src/main.rs b/src/main.rs index e7a11a9..cb69197 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,21 @@ +pub(crate) mod calendar; +pub(crate) mod gomi; + +use chrono::{Local, TimeZone, Weekday}; + +use crate::calendar::Calendar; + fn main() { - println!("Hello, world!"); + let calendar = calendar::matsudo::Matsudo::new(Weekday::Thu); + let today = Local.ymd(2021, 3, 17).and_hms(0, 0, 0); + let gomi = calendar.gomi_at(today); + + println!( + "今日 {} は {} の日です", + today, + gomi.iter() + .map(|g| g.to_string()) + .collect::>() + .join(", ") + ) }