1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use crate::date::{Date, Weekday};

pub trait Year: std::fmt::Display {
    type Month: Month;
    type Day: Day;

    fn ord(&self) -> i32;
    fn succ(&self) -> Self;
    fn pred(&self) -> Self;

    fn num_months(&self) -> usize;
    fn month(&self, ord: u8) -> Option<Self::Month>;
    fn first_month(&self) -> Self::Month {
        self.month(1).unwrap()
    }
    fn last_month(&self) -> Self::Month {
        self.month(self.num_months() as u8).unwrap()
    }
    fn months(&self) -> Vec<Self::Month> {
        (1..=self.num_months() as u8)
            .filter_map(|i| self.month(i))
            .collect()
    }

    fn num_days(&self) -> usize {
        self.months().iter().map(|m| m.num_days()).sum()
    }
    fn day(&self, ord: u16) -> Option<Self::Day>;
    fn first_day(&self) -> Self::Day {
        self.day(1).unwrap()
    }
    fn last_day(&self) -> Self::Day {
        self.day(self.num_days() as u16).unwrap()
    }
    fn days(&self) -> Vec<Self::Day> {
        (1..=self.num_days() as u16)
            .filter_map(|i| self.day(i))
            .collect()
    }

    fn is_leap(&self) -> bool {
        false
    }
}

pub trait Month: std::fmt::Display {
    type Year: Year;
    type Day: Day;

    fn ord(&self) -> u8;
    fn succ(&self) -> Self;
    fn pred(&self) -> Self;

    fn year(&self) -> Self::Year;

    fn num_days(&self) -> usize;
    fn day(&self, ord: u8) -> Option<Self::Day>;
    fn first_day(&self) -> Self::Day {
        self.day(1).unwrap()
    }
    fn last_day(&self) -> Self::Day {
        self.day(self.num_days() as u8).unwrap()
    }
    fn days(&self) -> Vec<Self::Day> {
        (1..=self.num_days() as u8)
            .filter_map(|i| self.day(i))
            .collect()
    }

    fn is_leap(&self) -> bool {
        false
    }
}

pub trait Day: Clone + Copy + std::fmt::Display + Into<Date> {
    type Year: Year;
    type Month: Month;

    fn ord(&self) -> u8;
    fn ord_in_year(&self) -> u16 {
        (1..self.month().ord())
            .map(|m| self.year().month(m).unwrap().num_days() as u16)
            .sum::<u16>()
            + self.ord() as u16
            + 1
    }
    fn succ(&self) -> Self;
    fn pred(&self) -> Self;

    fn year(&self) -> Self::Year;
    fn month(&self) -> Self::Month;

    fn is_leap(&self) -> bool {
        false
    }
    fn weekday(&self) -> Weekday {
        let date: Date = (*self).into();
        date.weekday()
    }
}