lrzcc_wire/user/
user.rs

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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use crate::user::ProjectMinimal;
use serde::{Deserialize, Serialize};
use sqlx::FromRow;
use std::cmp::PartialEq;
use std::fmt::Display;
use tabled::Tabled;

#[derive(Clone, Debug, Deserialize, Serialize, Tabled, FromRow, PartialEq)]
pub struct User {
    #[sqlx(try_from = "i32")]
    pub id: u32,
    pub name: String,
    pub openstack_id: String, // UUIDv4 without dashes
    #[sqlx(try_from = "i32")]
    pub project: u32,
    pub project_name: String,
    pub role: u32,
    pub is_staff: bool,
    pub is_active: bool,
}

impl Display for User {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&format!("User(id={}, name={}", self.id, self.name))
    }
}

impl PartialEq<UserMinimal> for User {
    fn eq(&self, other: &UserMinimal) -> bool {
        self.id == other.id && self.name == other.name
    }
}

impl PartialEq<UserDetailed> for User {
    fn eq(&self, other: &UserDetailed) -> bool {
        self.id == other.id
            && self.name == other.name
            && self.openstack_id == other.openstack_id
            && self.project == other.project.id
            && self.project_name == other.project_name
            && self.project_name == other.project.name
            && self.is_staff == other.is_staff
            && self.is_active == other.is_active
            && self.role == other.role
    }
}

#[derive(Clone, Debug, Deserialize, Serialize, Tabled, FromRow, PartialEq)]
pub struct UserMinimal {
    #[sqlx(try_from = "i32")]
    pub id: u32,
    pub name: String,
}

impl PartialEq<User> for UserMinimal {
    fn eq(&self, other: &User) -> bool {
        self.id == other.id && self.name == other.name
    }
}

impl PartialEq<UserDetailed> for UserMinimal {
    fn eq(&self, other: &UserDetailed) -> bool {
        self.id == other.id && self.name == other.name
    }
}

impl Display for UserMinimal {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&format!("User(id={}, name={}", self.id, self.name))
    }
}

#[derive(Clone, Debug, Deserialize, Serialize, Tabled, FromRow, PartialEq)]
pub struct UserDetailed {
    #[sqlx(try_from = "i32")]
    pub id: u32,
    pub name: String,
    pub openstack_id: String, // UUIDv4 without dashes
    #[sqlx(flatten)]
    pub project: ProjectMinimal,
    pub project_name: String,
    pub role: u32,
    pub is_staff: bool,
    pub is_active: bool,
}

impl PartialEq<UserMinimal> for UserDetailed {
    fn eq(&self, other: &UserMinimal) -> bool {
        self.id == other.id && self.name == other.name
    }
}

impl PartialEq<User> for UserDetailed {
    fn eq(&self, other: &User) -> bool {
        self.id == other.id
            && self.name == other.name
            && self.openstack_id == other.openstack_id
            && self.project.id == other.project
            && self.project.name == other.project_name
            && self.project_name == other.project_name
            && self.is_staff == other.is_staff
            && self.is_active == other.is_active
            && self.role == other.role
    }
}

#[derive(Clone, Debug, Deserialize, Serialize, Tabled, PartialEq)]
pub struct UserImport {
    pub new_project_count: u32,
    pub new_user_count: u32,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct UserListParams {
    pub all: Option<bool>,
    pub project: Option<u32>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct UserCreateData {
    pub name: String,
    pub openstack_id: String, // UUIDv4
    // TODO can't this be optional?
    pub project: u32,
    #[serde(skip_serializing_if = "Option::is_none")]
    // this could be an enum right
    pub role: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub is_staff: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub is_active: Option<bool>,
}

impl UserCreateData {
    pub fn new(name: String, openstack_id: String, project: u32) -> Self {
        Self {
            name,
            openstack_id,
            project,
            role: None,
            is_staff: None,
            is_active: None,
        }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct UserModifyData {
    pub id: u32,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub openstack_id: Option<String>, // UUIDv4
    #[serde(skip_serializing_if = "Option::is_none")]
    pub project: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub role: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub is_staff: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub is_active: Option<bool>,
}

impl UserModifyData {
    pub fn new(id: u32) -> Self {
        Self {
            id,
            name: None,
            openstack_id: None,
            project: None,
            role: None,
            is_staff: None,
            is_active: None,
        }
    }
}