uhppote_rs/types/
time_profile.rs

1use crate::messages::GetTimeProfileResponse;
2use anyhow::Result;
3use chrono::{NaiveDate, NaiveTime};
4
5#[derive(Debug)]
6pub struct TimeProfile {
7    pub id: u8,
8    pub linked_profile_id: u8,
9    pub from: NaiveDate,
10    pub to: NaiveDate,
11    pub monday: bool,
12    pub tuesday: bool,
13    pub wednesday: bool,
14    pub thursday: bool,
15    pub friday: bool,
16    pub saturday: bool,
17    pub sunday: bool,
18    pub segments: [TimeProfileSegment; 3],
19}
20
21impl TryFrom<GetTimeProfileResponse> for TimeProfile {
22    type Error = anyhow::Error;
23    fn try_from(response: GetTimeProfileResponse) -> Result<Self> {
24        let mut segments = [TimeProfileSegment {
25            start: NaiveTime::from_hms(0, 0, 0),
26            end: NaiveTime::from_hms(0, 0, 0),
27        }; 3];
28
29        segments[0] = TimeProfileSegment {
30            start: response.segment1_start.try_into()?,
31            end: response.segment1_end.try_into()?,
32        };
33        segments[1] = TimeProfileSegment {
34            start: response.segment2_start.try_into()?,
35            end: response.segment2_end.try_into()?,
36        };
37        segments[2] = TimeProfileSegment {
38            start: response.segment3_start.try_into()?,
39            end: response.segment3_end.try_into()?,
40        };
41
42        Ok(TimeProfile {
43            id: response.profile_id,
44            linked_profile_id: response.linked_profile_id,
45            from: response.from.try_into()?,
46            to: response.to.try_into()?,
47            monday: response.monday,
48            tuesday: response.tuesday,
49            wednesday: response.wednesday,
50            thursday: response.thursday,
51            friday: response.friday,
52            saturday: response.saturday,
53            sunday: response.sunday,
54            segments,
55        })
56    }
57}
58
59#[derive(Debug, Copy, Clone)]
60pub struct TimeProfileSegment {
61    pub start: NaiveTime,
62    pub end: NaiveTime,
63}