lrzcc_wire/user/
user.rs

1use crate::user::ProjectMinimal;
2use serde::{Deserialize, Serialize};
3use sqlx::FromRow;
4use std::cmp::PartialEq;
5use std::fmt::Display;
6use tabled::Tabled;
7
8#[derive(Clone, Debug, Deserialize, Serialize, Tabled, FromRow, PartialEq)]
9pub struct User {
10    #[sqlx(try_from = "i32")]
11    pub id: u32,
12    pub name: String,
13    pub openstack_id: String, // UUIDv4 without dashes
14    #[sqlx(try_from = "i32")]
15    pub project: u32,
16    pub project_name: String,
17    pub role: u32,
18    pub is_staff: bool,
19    pub is_active: bool,
20}
21
22impl Display for User {
23    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24        f.write_str(&format!("User(id={}, name={}", self.id, self.name))
25    }
26}
27
28impl PartialEq<UserMinimal> for User {
29    fn eq(&self, other: &UserMinimal) -> bool {
30        self.id == other.id && self.name == other.name
31    }
32}
33
34impl PartialEq<UserDetailed> for User {
35    fn eq(&self, other: &UserDetailed) -> bool {
36        self.id == other.id
37            && self.name == other.name
38            && self.openstack_id == other.openstack_id
39            && self.project == other.project.id
40            && self.project_name == other.project_name
41            && self.project_name == other.project.name
42            && self.is_staff == other.is_staff
43            && self.is_active == other.is_active
44            && self.role == other.role
45    }
46}
47
48#[derive(Clone, Debug, Deserialize, Serialize, Tabled, FromRow, PartialEq)]
49pub struct UserMinimal {
50    #[sqlx(try_from = "i32")]
51    pub id: u32,
52    pub name: String,
53}
54
55impl PartialEq<User> for UserMinimal {
56    fn eq(&self, other: &User) -> bool {
57        self.id == other.id && self.name == other.name
58    }
59}
60
61impl PartialEq<UserDetailed> for UserMinimal {
62    fn eq(&self, other: &UserDetailed) -> bool {
63        self.id == other.id && self.name == other.name
64    }
65}
66
67impl Display for UserMinimal {
68    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
69        f.write_str(&format!("User(id={}, name={}", self.id, self.name))
70    }
71}
72
73#[derive(Clone, Debug, Deserialize, Serialize, Tabled, FromRow, PartialEq)]
74pub struct UserDetailed {
75    #[sqlx(try_from = "i32")]
76    pub id: u32,
77    pub name: String,
78    pub openstack_id: String, // UUIDv4 without dashes
79    #[sqlx(flatten)]
80    pub project: ProjectMinimal,
81    pub project_name: String,
82    pub role: u32,
83    pub is_staff: bool,
84    pub is_active: bool,
85}
86
87impl PartialEq<UserMinimal> for UserDetailed {
88    fn eq(&self, other: &UserMinimal) -> bool {
89        self.id == other.id && self.name == other.name
90    }
91}
92
93impl PartialEq<User> for UserDetailed {
94    fn eq(&self, other: &User) -> bool {
95        self.id == other.id
96            && self.name == other.name
97            && self.openstack_id == other.openstack_id
98            && self.project.id == other.project
99            && self.project.name == other.project_name
100            && self.project_name == other.project_name
101            && self.is_staff == other.is_staff
102            && self.is_active == other.is_active
103            && self.role == other.role
104    }
105}
106
107#[derive(Clone, Debug, Deserialize, Serialize, Tabled, PartialEq)]
108pub struct UserImport {
109    pub new_project_count: u32,
110    pub new_user_count: u32,
111}
112
113#[derive(Debug, Serialize, Deserialize)]
114pub struct UserListParams {
115    pub all: Option<bool>,
116    pub project: Option<u32>,
117}
118
119#[derive(Clone, Debug, Serialize, Deserialize)]
120pub struct UserCreateData {
121    pub name: String,
122    pub openstack_id: String, // UUIDv4
123    // TODO can't this be optional?
124    pub project: u32,
125    #[serde(skip_serializing_if = "Option::is_none")]
126    // this could be an enum right
127    pub role: Option<u32>,
128    #[serde(skip_serializing_if = "Option::is_none")]
129    pub is_staff: Option<bool>,
130    #[serde(skip_serializing_if = "Option::is_none")]
131    pub is_active: Option<bool>,
132}
133
134impl UserCreateData {
135    pub fn new(name: String, openstack_id: String, project: u32) -> Self {
136        Self {
137            name,
138            openstack_id,
139            project,
140            role: None,
141            is_staff: None,
142            is_active: None,
143        }
144    }
145}
146
147#[derive(Clone, Debug, Serialize, Deserialize)]
148pub struct UserModifyData {
149    pub id: u32,
150
151    #[serde(skip_serializing_if = "Option::is_none")]
152    pub name: Option<String>,
153    #[serde(skip_serializing_if = "Option::is_none")]
154    pub openstack_id: Option<String>, // UUIDv4
155    #[serde(skip_serializing_if = "Option::is_none")]
156    pub project: Option<u32>,
157    #[serde(skip_serializing_if = "Option::is_none")]
158    pub role: Option<u32>,
159    #[serde(skip_serializing_if = "Option::is_none")]
160    pub is_staff: Option<bool>,
161    #[serde(skip_serializing_if = "Option::is_none")]
162    pub is_active: Option<bool>,
163}
164
165impl UserModifyData {
166    pub fn new(id: u32) -> Self {
167        Self {
168            id,
169            name: None,
170            openstack_id: None,
171            project: None,
172            role: None,
173            is_staff: None,
174            is_active: None,
175        }
176    }
177}