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