lrzcc_wire/user/
project.rs

1use crate::resources::FlavorGroupMinimal;
2use crate::user::UserMinimal;
3use serde::{Deserialize, Serialize};
4use sqlx::FromRow;
5use std::fmt::Display;
6use tabled::Tabled;
7
8#[derive(Clone, Debug, Deserialize, Serialize, Tabled, FromRow, PartialEq)]
9pub struct Project {
10    #[sqlx(try_from = "i32")]
11    pub id: u32,
12    pub name: String,
13    pub openstack_id: String, // UUIDv4 without dashes
14    pub user_class: u32,
15}
16
17impl Display for Project {
18    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19        f.write_str(&format!("Project(id={}, name={}", self.id, self.name))
20    }
21}
22
23impl PartialEq<ProjectMinimal> for Project {
24    fn eq(&self, other: &ProjectMinimal) -> bool {
25        self.id == other.id
26            && self.name == other.name
27            && self.user_class == other.user_class
28    }
29}
30
31impl PartialEq<ProjectDetailed> for Project {
32    fn eq(&self, other: &ProjectDetailed) -> bool {
33        self.id == other.id
34            && self.name == other.name
35            && self.openstack_id == other.openstack_id
36            && self.user_class == other.user_class
37    }
38}
39
40#[derive(Clone, Debug, Deserialize, Serialize, Tabled, FromRow, PartialEq)]
41pub struct ProjectMinimal {
42    #[sqlx(try_from = "i32", rename = "project__id")]
43    pub id: u32,
44    #[sqlx(rename = "project__name")]
45    pub name: String,
46    #[sqlx(rename = "project__user_class")]
47    pub user_class: u32,
48}
49
50impl PartialEq<Project> for ProjectMinimal {
51    fn eq(&self, other: &Project) -> bool {
52        self.id == other.id
53            && self.name == other.name
54            && self.user_class == other.user_class
55    }
56}
57
58impl PartialEq<ProjectDetailed> for ProjectMinimal {
59    fn eq(&self, other: &ProjectDetailed) -> bool {
60        self.id == other.id
61            && self.name == other.name
62            && self.user_class == other.user_class
63    }
64}
65
66impl Display for ProjectMinimal {
67    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
68        f.write_str(&format!("Project(id={}, name={})", self.id, self.name))
69    }
70}
71
72#[derive(Clone, Debug, Deserialize, Serialize, Tabled, PartialEq)]
73pub struct ProjectDetailed {
74    pub id: u32,
75    pub name: String,
76    pub openstack_id: String, // UUIDv4 without dashes
77    pub user_class: u32,
78    // TODO rethink list output in detailed structs:
79    // maybe we could have only the first few entries followed by ...
80    // in the output
81    #[tabled(skip)]
82    pub users: Vec<UserMinimal>,
83    #[tabled(skip)]
84    pub flavor_groups: Vec<FlavorGroupMinimal>,
85}
86
87impl PartialEq<ProjectMinimal> for ProjectDetailed {
88    fn eq(&self, other: &ProjectMinimal) -> bool {
89        self.id == other.id
90            && self.name == other.name
91            && self.user_class == other.user_class
92    }
93}
94
95impl PartialEq<Project> for ProjectDetailed {
96    fn eq(&self, other: &Project) -> bool {
97        self.id == other.id
98            && self.name == other.name
99            && self.openstack_id == other.openstack_id
100            && self.user_class == other.user_class
101    }
102}
103
104impl Display for ProjectDetailed {
105    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
106        f.write_str(&format!("Project(id={}, name={}", self.id, self.name))
107    }
108}
109
110#[derive(Clone, Debug, Deserialize, Serialize, Tabled, PartialEq)]
111#[serde(untagged)]
112pub enum ProjectRetrieved {
113    Detailed(ProjectDetailed),
114    Normal(Project),
115}
116
117#[derive(Debug, Serialize, Deserialize)]
118pub struct ProjectListParams {
119    pub all: Option<bool>,
120    pub userclass: Option<u32>,
121}
122
123#[derive(Clone, Debug, Serialize, Deserialize)]
124pub struct ProjectCreateData {
125    pub name: String,
126    pub openstack_id: String, // UUIDv4
127    // #[serde(skip_serializing_if = "Option::is_none")]
128    pub user_class: Option<u32>,
129}
130
131impl ProjectCreateData {
132    pub fn new(name: String, openstack_id: String) -> Self {
133        Self {
134            name,
135            openstack_id,
136            user_class: None,
137        }
138    }
139}
140
141#[derive(Clone, Debug, Serialize, Deserialize)]
142pub struct ProjectModifyData {
143    // TODO: why again is this here? since this is already a URL parameter
144    pub id: u32,
145
146    #[serde(skip_serializing_if = "Option::is_none")]
147    pub name: Option<String>,
148    #[serde(skip_serializing_if = "Option::is_none")]
149    pub openstack_id: Option<String>, // UUIDv4
150    #[serde(skip_serializing_if = "Option::is_none")]
151    pub user_class: Option<u32>,
152}
153
154impl ProjectModifyData {
155    pub fn new(id: u32) -> Self {
156        Self {
157            id,
158            name: None,
159            openstack_id: None,
160            user_class: None,
161        }
162    }
163}