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_path: 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 MoveGroupRequest {
122    #[serde(default, skip_serializing_if = "Option::is_none")]
123    pub target_parent_group_path: Option<String>,
124}
125
126#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
127pub struct UpsertMembershipRequest {
128    pub login_name: String,
129    pub role: MembershipRole,
130}
131
132#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
133pub struct GroupResponse {
134    pub group_id: i64,
135    pub group_key: String,
136    #[serde(default, skip_serializing_if = "Option::is_none")]
137    pub group_path: Option<String>,
138    #[serde(default, skip_serializing_if = "Option::is_none")]
139    pub parent_group_path: Option<String>,
140    pub name: String,
141    pub visibility: Visibility,
142    pub kind: GroupKind,
143    #[serde(default, skip_serializing_if = "Option::is_none")]
144    pub current_role: Option<MembershipRole>,
145    pub created_at: DateTime<Utc>,
146    pub updated_at: DateTime<Utc>,
147}
148
149#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
150pub struct GroupMemberResponse {
151    pub user_id: i64,
152    pub login_name: String,
153    pub display_name: String,
154    pub role: MembershipRole,
155}
156
157#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
158pub struct UserDirectoryEntryResponse {
159    pub user_id: i64,
160    pub login_name: String,
161    pub display_name: String,
162}