Skip to main content

hey_sdk/services/
identity.rs

1//! The two identity preferences HEY lets a client write.
2
3use std::str::FromStr;
4
5use chrono::Weekday;
6
7use crate::error::Error;
8use crate::generated::types::{
9    FirstWeekDayParams, UpdateFirstWeekDayRequestContent, UpdateTimeFormatRequestContent,
10};
11
12pub use crate::generated::services::identity::*;
13
14/// The clock HEY renders times on.
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16#[non_exhaustive]
17pub enum TimeFormat {
18    /// A 12-hour clock, HEY's `twelve_hour`.
19    TwelveHour,
20    /// A 24-hour clock, HEY's `twenty_four_hour`.
21    TwentyFourHour,
22}
23
24impl TimeFormat {
25    /// The format as HEY's identity names it.
26    pub fn as_str(&self) -> &'static str {
27        match self {
28            TimeFormat::TwelveHour => "twelve_hour",
29            TimeFormat::TwentyFourHour => "twenty_four_hour",
30        }
31    }
32}
33
34impl Identity<'_> {
35    /// Sets which day the identity's calendar weeks start on, and answers the day HEY
36    /// stored. The write reaches every HEY client — web, mobile and this SDK read the same
37    /// identity preference.
38    pub async fn set_first_week_day(&self, day: Weekday) -> Result<Weekday, Error> {
39        let body = UpdateFirstWeekDayRequestContent {
40            identity_preference: FirstWeekDayParams {
41                first_week_day: day_name(day).to_string(),
42            },
43        };
44        let stored = self.update_first_week_day(&body).await?;
45        weekday_at(stored.first_week_day)
46    }
47
48    /// Sets whether HEY renders times on a 12-hour or a 24-hour clock, and answers the
49    /// format HEY stored.
50    pub async fn set_time_format(&self, format: TimeFormat) -> Result<TimeFormat, Error> {
51        let body = UpdateTimeFormatRequestContent {
52            twenty_four_hour_time_format: format == TimeFormat::TwentyFourHour,
53        };
54        self.update_time_format(&body).await?.time_format.parse()
55    }
56}
57
58/// HEY takes the day as its lowercased English name.
59fn day_name(day: Weekday) -> &'static str {
60    match day {
61        Weekday::Mon => "monday",
62        Weekday::Tue => "tuesday",
63        Weekday::Wed => "wednesday",
64        Weekday::Thu => "thursday",
65        Weekday::Fri => "friday",
66        Weekday::Sat => "saturday",
67        Weekday::Sun => "sunday",
68    }
69}
70
71/// HEY answers the stored day as an index the way it serves the identity: 0 is Sunday.
72fn weekday_at(index: i32) -> Result<Weekday, Error> {
73    match index {
74        0 => Ok(Weekday::Sun),
75        1 => Ok(Weekday::Mon),
76        2 => Ok(Weekday::Tue),
77        3 => Ok(Weekday::Wed),
78        4 => Ok(Weekday::Thu),
79        5 => Ok(Weekday::Fri),
80        6 => Ok(Weekday::Sat),
81        _ => Err(Error::api(
82            0,
83            format!("first week day {index} is not a day of the week"),
84        )),
85    }
86}
87
88impl FromStr for TimeFormat {
89    type Err = Error;
90
91    fn from_str(source: &str) -> Result<TimeFormat, Error> {
92        match source {
93            "twelve_hour" => Ok(TimeFormat::TwelveHour),
94            "twenty_four_hour" => Ok(TimeFormat::TwentyFourHour),
95            _ => Err(Error::usage(format!(
96                "time format {source:?} is neither \"twelve_hour\" nor \"twenty_four_hour\""
97            ))),
98        }
99    }
100}