Skip to main content

context69_contracts/
namespace.rs

1use chrono::{DateTime, Utc};
2use schemars::JsonSchema;
3use serde::{Deserialize, Serialize};
4use utoipa::ToSchema;
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, ToSchema, JsonSchema)]
7#[serde(rename_all = "snake_case")]
8pub enum Visibility {
9    Public,
10    Private,
11}
12
13impl Visibility {
14    pub fn as_str(self) -> &'static str {
15        match self {
16            Self::Public => "public",
17            Self::Private => "private",
18        }
19    }
20}
21
22impl std::str::FromStr for Visibility {
23    type Err = anyhow::Error;
24
25    fn from_str(value: &str) -> Result<Self, Self::Err> {
26        match value {
27            "public" => Ok(Self::Public),
28            "private" => Ok(Self::Private),
29            other => Err(anyhow::anyhow!("unsupported visibility: {other}")),
30        }
31    }
32}
33
34#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, ToSchema, JsonSchema)]
35#[serde(rename_all = "snake_case")]
36pub enum MembershipRole {
37    Owner,
38    Maintainer,
39    Viewer,
40}
41
42impl MembershipRole {
43    pub fn as_str(self) -> &'static str {
44        match self {
45            Self::Owner => "owner",
46            Self::Maintainer => "maintainer",
47            Self::Viewer => "viewer",
48        }
49    }
50
51    pub fn rank(self) -> i16 {
52        match self {
53            Self::Viewer => 1,
54            Self::Maintainer => 2,
55            Self::Owner => 3,
56        }
57    }
58}
59
60impl std::str::FromStr for MembershipRole {
61    type Err = anyhow::Error;
62
63    fn from_str(value: &str) -> Result<Self, Self::Err> {
64        match value {
65            "owner" => Ok(Self::Owner),
66            "maintainer" => Ok(Self::Maintainer),
67            "viewer" => Ok(Self::Viewer),
68            other => Err(anyhow::anyhow!("unsupported membership role: {other}")),
69        }
70    }
71}
72
73#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, ToSchema, JsonSchema)]
74#[serde(rename_all = "snake_case")]
75pub enum GroupKind {
76    Personal,
77    Shared,
78}
79
80impl GroupKind {
81    pub fn as_str(self) -> &'static str {
82        match self {
83            Self::Personal => "personal",
84            Self::Shared => "shared",
85        }
86    }
87}
88
89impl std::str::FromStr for GroupKind {
90    type Err = anyhow::Error;
91
92    fn from_str(value: &str) -> Result<Self, Self::Err> {
93        match value {
94            "personal" => Ok(Self::Personal),
95            "shared" => Ok(Self::Shared),
96            other => Err(anyhow::anyhow!("unsupported group kind: {other}")),
97        }
98    }
99}
100
101#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
102pub struct CreateGroupRequest {
103    #[serde(default, skip_serializing_if = "Option::is_none")]
104    pub parent_group_key: Option<String>,
105    pub group_key: String,
106    pub name: String,
107    pub visibility: Visibility,
108    #[serde(default)]
109    pub kind: Option<GroupKind>,
110}
111
112#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
113pub struct UpdateGroupRequest {
114    #[serde(default, skip_serializing_if = "Option::is_none")]
115    pub name: Option<String>,
116    #[serde(default, skip_serializing_if = "Option::is_none")]
117    pub visibility: Option<Visibility>,
118}
119
120#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
121pub struct CreateProjectRequest {
122    pub project_key: String,
123    pub name: String,
124    pub visibility: Visibility,
125}
126
127#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
128pub struct UpdateProjectRequest {
129    #[serde(default, skip_serializing_if = "Option::is_none")]
130    pub name: Option<String>,
131    #[serde(default, skip_serializing_if = "Option::is_none")]
132    pub visibility: Option<Visibility>,
133}
134
135#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
136pub struct MoveProjectRequest {
137    pub target_group_key: String,
138}
139
140#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
141pub struct UpsertMembershipRequest {
142    pub login_name: String,
143    pub role: MembershipRole,
144}
145
146#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
147pub struct GroupResponse {
148    pub group_id: i64,
149    pub group_key: String,
150    #[serde(default, skip_serializing_if = "Option::is_none")]
151    pub parent_group_key: Option<String>,
152    pub name: String,
153    pub visibility: Visibility,
154    pub kind: GroupKind,
155    #[serde(default, skip_serializing_if = "Option::is_none")]
156    pub current_role: Option<MembershipRole>,
157    pub created_at: DateTime<Utc>,
158    pub updated_at: DateTime<Utc>,
159}
160
161#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
162pub struct ProjectResponse {
163    pub project_id: i64,
164    pub group_key: String,
165    pub project_key: String,
166    pub name: String,
167    pub visibility: Visibility,
168    #[serde(default, skip_serializing_if = "Option::is_none")]
169    pub current_role: Option<MembershipRole>,
170    pub created_at: DateTime<Utc>,
171    pub updated_at: DateTime<Utc>,
172}
173
174#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
175pub struct GroupMemberResponse {
176    pub user_id: i64,
177    pub login_name: String,
178    pub display_name: String,
179    pub role: MembershipRole,
180}
181
182#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
183pub struct ProjectMemberResponse {
184    pub user_id: i64,
185    pub login_name: String,
186    pub display_name: String,
187    pub role: MembershipRole,
188}
189
190#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
191pub struct UserDirectoryEntryResponse {
192    pub user_id: i64,
193    pub login_name: String,
194    pub display_name: String,
195}