lrzcc_wire/user/
project.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
use crate::resources::FlavorGroupMinimal;
use crate::user::UserMinimal;
use serde::{Deserialize, Serialize};
use sqlx::FromRow;
use std::fmt::Display;
use tabled::Tabled;

#[derive(Clone, Debug, Deserialize, Serialize, Tabled, FromRow, PartialEq)]
pub struct Project {
    #[sqlx(try_from = "i32")]
    pub id: u32,
    pub name: String,
    pub openstack_id: String, // UUIDv4 without dashes
    pub user_class: u32,
}

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

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

impl PartialEq<ProjectDetailed> for Project {
    fn eq(&self, other: &ProjectDetailed) -> bool {
        self.id == other.id
            && self.name == other.name
            && self.openstack_id == other.openstack_id
            && self.user_class == other.user_class
    }
}

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

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

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

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

#[derive(Clone, Debug, Deserialize, Serialize, Tabled, PartialEq)]
pub struct ProjectDetailed {
    pub id: u32,
    pub name: String,
    pub openstack_id: String, // UUIDv4 without dashes
    pub user_class: u32,
    // TODO rethink list output in detailed structs:
    // maybe we could have only the first few entries followed by ...
    // in the output
    #[tabled(skip)]
    pub users: Vec<UserMinimal>,
    #[tabled(skip)]
    pub flavor_groups: Vec<FlavorGroupMinimal>,
}

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

impl PartialEq<Project> for ProjectDetailed {
    fn eq(&self, other: &Project) -> bool {
        self.id == other.id
            && self.name == other.name
            && self.openstack_id == other.openstack_id
            && self.user_class == other.user_class
    }
}

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

#[derive(Clone, Debug, Deserialize, Serialize, Tabled, PartialEq)]
#[serde(untagged)]
pub enum ProjectRetrieved {
    Detailed(ProjectDetailed),
    Normal(Project),
}

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

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ProjectCreateData {
    pub name: String,
    pub openstack_id: String, // UUIDv4
    // #[serde(skip_serializing_if = "Option::is_none")]
    pub user_class: Option<u32>,
}

impl ProjectCreateData {
    pub fn new(name: String, openstack_id: String) -> Self {
        Self {
            name,
            openstack_id,
            user_class: None,
        }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ProjectModifyData {
    // TODO: why again is this here? since this is already a URL parameter
    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 user_class: Option<u32>,
}

impl ProjectModifyData {
    pub fn new(id: u32) -> Self {
        Self {
            id,
            name: None,
            openstack_id: None,
            user_class: None,
        }
    }
}