forked from explodingcamera/liwan
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.rs
More file actions
109 lines (98 loc) · 2.64 KB
/
models.rs
File metadata and controls
109 lines (98 loc) · 2.64 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use std::fmt::Display;
use chrono::{DateTime, Utc};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone)]
pub struct Event {
pub entity_id: String,
pub visitor_id: String,
pub event: String,
pub created_at: DateTime<Utc>,
pub fqdn: Option<String>,
pub path: Option<String>,
pub referrer: Option<String>,
pub platform: Option<String>,
pub browser: Option<String>,
pub mobile: Option<bool>,
pub country: Option<String>,
pub city: Option<String>,
pub utm_source: Option<String>,
pub utm_medium: Option<String>,
pub utm_campaign: Option<String>,
pub utm_content: Option<String>,
pub utm_term: Option<String>,
pub screen_size: Option<String>,
}
#[derive(Debug, Clone)]
pub struct Project {
pub id: String,
pub display_name: String,
pub public: bool,
pub secret: Option<String>, // enable public access with password protection
}
#[derive(Debug, Clone)]
pub struct Entity {
pub id: String,
pub display_name: String,
}
#[derive(Debug, Clone)]
pub struct User {
pub username: String,
pub role: UserRole,
pub projects: Vec<String>,
}
#[derive(Debug, JsonSchema, Serialize, Deserialize, PartialEq, Eq, Clone, Copy, Default)]
#[serde(rename_all = "snake_case")]
pub enum UserRole {
#[serde(rename = "admin")]
Admin,
#[serde(rename = "user")]
#[default]
User,
}
impl Display for UserRole {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
UserRole::Admin => write!(f, "admin"),
UserRole::User => write!(f, "user"),
}
}
}
impl TryFrom<String> for UserRole {
type Error = String;
fn try_from(value: String) -> Result<Self, Self::Error> {
match value.as_str() {
"admin" => Ok(Self::Admin),
"user" => Ok(Self::User),
_ => Err(format!("invalid role: {value}")),
}
}
}
#[macro_export]
macro_rules! event_params {
($event:expr) => {
duckdb::params![
$event.entity_id,
$event.visitor_id,
$event.event,
$event.created_at,
$event.fqdn,
$event.path,
$event.referrer,
$event.platform,
$event.browser,
$event.mobile,
$event.country,
$event.city,
$event.utm_source,
$event.utm_medium,
$event.utm_campaign,
$event.utm_content,
$event.utm_term,
None::<std::time::Duration>,
None::<std::time::Duration>,
$event.screen_size,
]
};
}
pub use event_params;