houseflow_types/
user.rs

1use crate::common::Credential;
2use std::convert::TryFrom;
3use strum::{EnumIter, IntoEnumIterator};
4
5#[cfg(feature = "serde")]
6use serde::{Deserialize, Serialize};
7
8pub type UserID = Credential<16>;
9
10#[derive(Debug, Clone, Copy, Eq, PartialEq, EnumIter, strum::Display)]
11#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
12#[cfg_attr(feature = "serde", serde(rename = "snake_case"))]
13#[repr(u8)]
14pub enum UserAgent {
15    Internal,
16    GoogleSmartHome,
17}
18
19use std::time::Duration;
20impl UserAgent {
21    pub fn refresh_token_duration(&self) -> Option<Duration> {
22        match *self {
23            Self::Internal => Some(Duration::from_secs(3600 * 24 * 7)), // One week
24            Self::GoogleSmartHome => None,                              // Never
25        }
26    }
27
28    pub fn access_token_duration(&self) -> Option<Duration> {
29        match *self {
30            Self::Internal => Some(Duration::from_secs(60 * 10)), // 10 Minutes
31            Self::GoogleSmartHome => Some(Duration::from_secs(60 * 10)), // 10 Minutes
32        }
33    }
34}
35
36impl TryFrom<u8> for UserAgent {
37    type Error = ();
38
39    fn try_from(v: u8) -> Result<Self, Self::Error> {
40        Self::iter().find(|e| *e as u8 == v).ok_or(())
41    }
42}
43
44impl rand::distributions::Distribution<UserAgent> for rand::distributions::Standard {
45    fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> UserAgent {
46        UserAgent::try_from(rng.gen_range(0..UserAgent::iter().len() as u8)).unwrap()
47    }
48}
49
50#[derive(Debug, Clone, PartialEq, Eq)]
51#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
52pub struct User {
53    /// Unique ID of the user
54    pub id: UserID,
55
56    /// Name of the user
57    pub username: String,
58
59    /// Email of the user
60    pub email: String,
61
62    /// Hashed user password
63    pub password_hash: String,
64}