hey_sdk/services/
identity.rs1use 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#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16#[non_exhaustive]
17pub enum TimeFormat {
18 TwelveHour,
20 TwentyFourHour,
22}
23
24impl TimeFormat {
25 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 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 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
58fn 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
71fn 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}