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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
pub use crate::labels::Label;
use crate::names::Name;
use crate::timestamps::Timestamp;
use crate::{data_sources::SelectedDataSources, front_matter_schemas::FrontMatterSchema};
use base64uuid::Base64Uuid;
#[cfg(feature = "fp-bindgen")]
use fp_bindgen::prelude::Serializable;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use strum_macros::Display;
use typed_builder::TypedBuilder;

/// Workspace representation.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize, TypedBuilder)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::workspaces")
)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct Workspace {
    #[builder(setter(into))]
    pub id: Base64Uuid,

    pub name: Name,

    #[builder(setter(into))]
    pub display_name: String,

    #[serde(rename = "type")]
    pub ty: WorkspaceType,

    #[builder(setter(into))]
    pub owner_id: Base64Uuid,

    #[builder(default)]
    pub default_data_sources: SelectedDataSources,

    #[builder(setter(into))]
    pub created_at: Timestamp,

    #[builder(setter(into))]
    pub updated_at: Timestamp,

    #[builder(default)]
    pub front_matter_schemas: BTreeMap<String, FrontMatterSchema>,
}

#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize, Display)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::workspaces")
)]
#[non_exhaustive]
#[serde(rename_all = "snake_case")]
pub enum WorkspaceType {
    Personal,
    Organization,
}

/// Payload to be able to invite someone to a workspace.
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::workspaces")
)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct NewWorkspaceInvite {
    pub email: String,

    #[serde(default)]
    pub role: AuthRole,
}

impl NewWorkspaceInvite {
    pub fn new(email: impl Into<String>, role: AuthRole) -> Self {
        Self {
            email: email.into(),
            role,
        }
    }
}

/// Response received from create a new workspace endpoint.
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize, TypedBuilder)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::workspaces")
)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct WorkspaceInviteResponse {
    #[builder(setter(into))]
    pub url: String,
}

/// Payload to create a new organization workspace.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize, TypedBuilder)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::workspaces")
)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct NewWorkspace {
    pub name: Name,

    /// The display name of the workspace. The `name` will be used if none is provided.
    #[builder(default, setter(into, strip_option))]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub display_name: Option<String>,

    #[builder(default, setter(into, strip_option))]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub default_data_sources: Option<SelectedDataSources>,

    #[builder(default, setter(strip_option))]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub front_matter_schemas: Option<WorkspaceFrontMatterSchemas>,
}

impl NewWorkspace {
    pub fn new(name: Name) -> Self {
        Self {
            name,
            display_name: None,
            default_data_sources: None,
            front_matter_schemas: None,
        }
    }
}

#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize, Default, TypedBuilder)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::workspaces")
)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct NewWorkspaceFrontMatterSchema {
    pub name: String,
    pub schema: FrontMatterSchema,
}

#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize, Default)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::workspaces")
)]
#[non_exhaustive]
#[repr(transparent)]
pub struct WorkspaceFrontMatterSchemas(pub BTreeMap<String, FrontMatterSchema>);

impl std::ops::Deref for WorkspaceFrontMatterSchemas {
    type Target = BTreeMap<String, FrontMatterSchema>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl std::ops::DerefMut for WorkspaceFrontMatterSchemas {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl From<BTreeMap<String, FrontMatterSchema>> for WorkspaceFrontMatterSchemas {
    fn from(value: BTreeMap<String, FrontMatterSchema>) -> Self {
        Self(value)
    }
}

impl FromIterator<(String, FrontMatterSchema)> for WorkspaceFrontMatterSchemas {
    fn from_iter<T: IntoIterator<Item = (String, FrontMatterSchema)>>(iter: T) -> Self {
        iter.into_iter()
            .collect::<BTreeMap<String, FrontMatterSchema>>()
            .into()
    }
}

/// Payload to update workspace settings
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize, TypedBuilder)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::workspaces")
)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct UpdateWorkspace {
    #[builder(default, setter(into, strip_option))]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub display_name: Option<String>,

    #[builder(default, setter(into, strip_option))]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub owner: Option<Base64Uuid>,

    #[builder(default, setter(strip_option))]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub default_data_sources: Option<SelectedDataSources>,

    #[builder(default, setter(strip_option))]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub front_matter_schemas: Option<BTreeMap<String, FrontMatterSchema>>,
}

/// Payload to update a workspace members' role
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize, TypedBuilder)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::workspaces")
)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct UpdateWorkspaceUser {
    #[builder(default, setter(strip_option))]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub role: Option<AuthRole>,
}

#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Eq, Serialize, Display)]
#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::workspaces")
)]
#[non_exhaustive]
#[serde(rename_all = "snake_case")]
pub enum AuthRole {
    #[strum(serialize = "Viewer")]
    Read,
    #[strum(serialize = "Editor")]
    Write,
    Admin,
}

impl Default for AuthRole {
    fn default() -> Self {
        Self::Write
    }
}

#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize, TypedBuilder)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::workspaces")
)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct WorkspaceInvite {
    /// ID of the invitation.
    #[builder(setter(into))]
    pub id: Base64Uuid,

    /// ID of the user who sent the invitation.
    #[builder(setter(into))]
    pub sender: Base64Uuid,

    /// Email address of the invitee.
    #[builder(setter(into))]
    pub receiver: String,

    /// Timestamp at which the invitation was created.
    #[builder(setter(into))]
    pub created_at: Timestamp,

    /// Timestamp at which the invitation expires.
    #[builder(setter(into))]
    pub expires_at: Timestamp,
}

#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize, TypedBuilder)]
#[cfg_attr(
    feature = "fp-bindgen",
    derive(Serializable),
    fp(rust_module = "fiberplane_models::workspaces")
)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct Membership {
    #[builder(setter(into))]
    pub id: Base64Uuid,

    #[builder(setter(into))]
    pub email: String,

    #[builder(setter(into))]
    pub name: String,

    pub role: AuthRole,
}