made the period lookup const

This commit is contained in:
zenonet
2025-09-18 21:30:12 +02:00
parent 6741ecbf2c
commit df706890be

View File

@@ -184,8 +184,7 @@ fn draw_layout(display: &mut epd_waveshare::epd7in5_v2::Display7in5, timetable:
} }
fn get_school_period(time: NaiveTime) -> u8{ const PERIOD_LOOKUP: [NaiveTime; 8] = [
let lookup: [NaiveTime; 8] = [
(NaiveTime::from_hms_opt(8, 45, 0).unwrap()), (NaiveTime::from_hms_opt(8, 45, 0).unwrap()),
(NaiveTime::from_hms_opt(9, 35, 0).unwrap()), (NaiveTime::from_hms_opt(9, 35, 0).unwrap()),
(NaiveTime::from_hms_opt(10, 40, 0).unwrap()), (NaiveTime::from_hms_opt(10, 40, 0).unwrap()),
@@ -194,9 +193,12 @@ fn get_school_period(time: NaiveTime) -> u8{
(NaiveTime::from_hms_opt(13, 30, 0).unwrap()), (NaiveTime::from_hms_opt(13, 30, 0).unwrap()),
(NaiveTime::from_hms_opt(14, 25, 0).unwrap()), (NaiveTime::from_hms_opt(14, 25, 0).unwrap()),
(NaiveTime::from_hms_opt(15, 15, 0).unwrap()), (NaiveTime::from_hms_opt(15, 15, 0).unwrap()),
]; ];
for (index, period_time) in lookup.iter().enumerate(){ fn get_school_period(time: NaiveTime) -> u8{
for (index, period_time) in PERIOD_LOOKUP.iter().enumerate(){
if time < *period_time{ if time < *period_time{
return index as u8; return index as u8;
} }
@@ -206,16 +208,6 @@ fn get_school_period(time: NaiveTime) -> u8{
fn transform_lessons(lessons: Vec<Lesson>) -> Box<Timetable>{ fn transform_lessons(lessons: Vec<Lesson>) -> Box<Timetable>{
let lookup: [NaiveTime; 8] = [
(NaiveTime::from_hms_opt(8, 45, 0).unwrap()),
(NaiveTime::from_hms_opt(9, 35, 0).unwrap()),
(NaiveTime::from_hms_opt(10, 40, 0).unwrap()),
(NaiveTime::from_hms_opt(11, 30, 0).unwrap()),
(NaiveTime::from_hms_opt(12, 40, 0).unwrap()),
(NaiveTime::from_hms_opt(13, 30, 0).unwrap()),
(NaiveTime::from_hms_opt(14, 25, 0).unwrap()),
(NaiveTime::from_hms_opt(15, 15, 0).unwrap()),
];
let mut table: Box<[[Option<Lesson>; 8]; 5]> = Box::new([0u8; 5].map(|_| [0u8; 8].map(|_| Option::<Lesson>::None))); let mut table: Box<[[Option<Lesson>; 8]; 5]> = Box::new([0u8; 5].map(|_| [0u8; 8].map(|_| Option::<Lesson>::None)));
@@ -223,7 +215,7 @@ fn transform_lessons(lessons: Vec<Lesson>) -> Box<Timetable>{
let day_idx = lesson.date.weekday().num_days_from_monday() as usize; let day_idx = lesson.date.weekday().num_days_from_monday() as usize;
let mut l = Cow::<Lesson>::Owned(lesson); let mut l = Cow::<Lesson>::Owned(lesson);
for (period_idx, time) in lookup.iter().enumerate(){ for (period_idx, time) in PERIOD_LOOKUP.iter().enumerate(){
if l.start_time.0 < *time && &l.end_time.0 >= time{ if l.start_time.0 < *time && &l.end_time.0 >= time{
table[day_idx][period_idx] = Some(l.into_owned()); table[day_idx][period_idx] = Some(l.into_owned());
l = Cow::Borrowed(&table[day_idx][period_idx].as_ref().unwrap()); l = Cow::Borrowed(&table[day_idx][period_idx].as_ref().unwrap());