thisweek_core/
week_info.rs1use crate::{calendar::Calendar, language::Language, prelude::Result as AppResult};
2use serde::Serialize;
3
4#[derive(Debug, Serialize, Clone, Default)]
5pub struct WeekInfo {
6 calendar: Calendar,
7 language: Language,
8 dates: Vec<DateView>,
9 direction: String,
10 month_year_info: String,
11}
12
13#[derive(Serialize, Clone)]
14pub struct Date {
15 pub calendar: Calendar,
16 pub weekday: u32,
17 pub day: u32,
18 pub month: u32,
19 pub year: i32,
20}
21
22#[derive(Debug, Serialize, Clone, Default)]
23pub struct DateView {
24 pub unix_day: i32,
25 pub day: String,
26 pub month: String,
27 pub year: String,
28 pub weekday: String,
29 pub full_format: String,
30 pub weekend_holiday: bool,
31}
32
33impl WeekInfo {
34 pub fn from_unix_start_end_days(
35 start_day: i32,
36 end_day: i32,
37 today: i32,
38 calendar: Calendar,
39 language: Language,
40 ) -> AppResult<Self> {
41 let direction = calendar.into_direction();
42 let dates = calendar.get_dates_view(start_day, end_day, &language)?;
43 let today_view = calendar.get_date_view(today, &language);
44 let month_year_info = Self::calculate_month_year_info(&dates, &today_view);
45 Ok(WeekInfo {
46 calendar,
47 dates,
48 direction,
49 language,
50 month_year_info,
51 })
52 }
53
54 fn calculate_month_year_info(dates: &[DateView], today: &DateView) -> String {
56 let first_day_year = dates.first().unwrap().year.clone();
57 let last_day_year = dates.last().unwrap().year.clone();
58 let first_day_month = dates.first().unwrap().month.clone();
59 let last_day_month = dates.last().unwrap().month.clone();
60 let today_year = today.year.clone();
61 if first_day_month == last_day_month && first_day_year == today_year {
64 return first_day_month.to_string();
65 }
66
67 if first_day_month != last_day_month
68 && first_day_year == today_year
69 && last_day_year == today_year
70 {
71 return format!("{} - {}", first_day_month, last_day_month);
72 }
73
74 if first_day_year != last_day_year {
75 return format!(
76 "{} {} - {} {}",
77 first_day_month, first_day_year, last_day_month, last_day_year
78 );
79 }
80
81 if first_day_month == last_day_month && first_day_year == last_day_year {
82 return format!("{} {}", first_day_month, first_day_year);
83 }
84
85 format!(
86 "{} {} - {} {}",
87 first_day_month, first_day_year, last_day_month, last_day_year
88 )
89 }
90}