1#![cfg_attr(not(test), warn(unused_crate_dependencies))]
7#![cfg_attr(docsrs, feature(doc_cfg))]
8
9use uuid::Uuid;
10
11macro_rules! api_key_scopes {
13 (
14 $(
15 $(#[$meta:meta])*
16 $variant:ident => $wire_name:literal
17 ),+ $(,)?
18 ) => {
19 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
21 #[cfg_attr(feature = "wire", derive(serde::Serialize, serde::Deserialize, schemars::JsonSchema))]
22 pub enum ApiKeyScope {
23 $(
24 $(#[$meta])*
25 #[cfg_attr(feature = "wire", serde(rename = $wire_name))]
26 $variant,
27 )+
28 }
29
30 impl ApiKeyScope {
31 pub const ALL: &[Self] = &[
33 $(Self::$variant,)+
34 ];
35
36 #[must_use]
38 pub const fn as_str(self) -> &'static str {
39 match self {
40 $(Self::$variant => $wire_name,)+
41 }
42 }
43 }
44
45 impl std::str::FromStr for ApiKeyScope {
46 type Err = ();
47
48 fn from_str(value: &str) -> Result<Self, Self::Err> {
49 match value {
50 $($wire_name => Ok(Self::$variant),)+
51 _ => Err(()),
52 }
53 }
54 }
55 };
56}
57
58api_key_scopes! {
59 WorkspaceRead => "workspaces:read",
61 WorkspaceWrite => "workspaces:write",
64 ObjectRead => "objects:read",
66 ObjectWrite => "objects:write",
69 AttachmentRead => "attachments:read",
71 AttachmentWrite => "attachments:write",
74 GraphRead => "graph:read",
76 GraphWrite => "graph:write",
79 EventRead => "events:read",
81 RealtimeRead => "realtime:read",
83 AccessManage => "access:manage",
85 Admin => "admin",
88}
89
90impl ApiKeyScope {
91 #[must_use]
96 pub const fn permits(self, required: Self) -> bool {
97 match required {
98 Self::WorkspaceRead => matches!(self, Self::WorkspaceRead | Self::WorkspaceWrite),
99 Self::WorkspaceWrite => matches!(self, Self::WorkspaceWrite),
100 Self::ObjectRead => matches!(self, Self::ObjectRead | Self::ObjectWrite),
101 Self::ObjectWrite => matches!(self, Self::ObjectWrite),
102 Self::AttachmentRead => matches!(self, Self::AttachmentRead | Self::AttachmentWrite),
103 Self::AttachmentWrite => matches!(self, Self::AttachmentWrite),
104 Self::GraphRead => matches!(self, Self::GraphRead | Self::GraphWrite),
105 Self::GraphWrite => matches!(self, Self::GraphWrite),
106 Self::EventRead => matches!(self, Self::EventRead),
107 Self::RealtimeRead => matches!(self, Self::RealtimeRead),
108 Self::AccessManage => matches!(self, Self::AccessManage),
109 Self::Admin => matches!(self, Self::Admin),
110 }
111 }
112}
113
114#[derive(Debug, Clone, Copy, PartialEq, Eq)]
116#[cfg_attr(feature = "wire", derive(serde::Serialize, serde::Deserialize, schemars::JsonSchema))]
117#[cfg_attr(feature = "wire", serde(rename_all = "lowercase"))]
118pub enum ObjectRole {
119 Viewer,
121 Editor,
123 Admin,
125}
126
127impl ObjectRole {
128 #[must_use]
130 pub const fn as_str(self) -> &'static str {
131 match self {
132 Self::Viewer => "viewer",
133 Self::Editor => "editor",
134 Self::Admin => "admin",
135 }
136 }
137
138 #[must_use]
140 pub fn parse(value: &str) -> Option<Self> {
141 value.parse().ok()
142 }
143}
144
145impl std::str::FromStr for ObjectRole {
146 type Err = ();
147
148 fn from_str(value: &str) -> Result<Self, Self::Err> {
149 match value {
150 "viewer" => Ok(Self::Viewer),
151 "editor" => Ok(Self::Editor),
152 "admin" => Ok(Self::Admin),
153 _ => Err(()),
154 }
155 }
156}
157
158impl std::fmt::Display for ObjectRole {
159 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
160 formatter.write_str(self.as_str())
161 }
162}
163
164#[derive(Debug, Clone, Copy, PartialEq, Eq)]
166#[cfg_attr(feature = "wire", derive(serde::Serialize, serde::Deserialize, schemars::JsonSchema))]
167#[cfg_attr(feature = "wire", serde(rename_all = "lowercase"))]
168pub enum MembershipRole {
169 Member,
171 Admin,
173}
174
175impl MembershipRole {
176 #[must_use]
178 pub const fn as_str(self) -> &'static str {
179 match self {
180 Self::Member => "member",
181 Self::Admin => "admin",
182 }
183 }
184}
185
186impl std::str::FromStr for MembershipRole {
187 type Err = ();
188
189 fn from_str(value: &str) -> Result<Self, Self::Err> {
190 match value {
191 "member" => Ok(Self::Member),
192 "admin" => Ok(Self::Admin),
193 _ => Err(()),
194 }
195 }
196}
197
198impl std::fmt::Display for MembershipRole {
199 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
200 formatter.write_str(self.as_str())
201 }
202}
203
204#[derive(Debug, Clone, Copy, PartialEq, Eq)]
206#[cfg_attr(feature = "wire", derive(serde::Serialize, serde::Deserialize, schemars::JsonSchema))]
207#[cfg_attr(feature = "wire", serde(tag = "type", content = "id", rename_all = "snake_case"))]
208pub enum GrantPrincipal {
209 User(Uuid),
211 Group(Uuid),
213}
214
215#[derive(Debug, Clone, Copy, PartialEq, Eq)]
217#[cfg_attr(feature = "wire", derive(serde::Serialize, serde::Deserialize, schemars::JsonSchema))]
218#[cfg_attr(feature = "wire", serde(rename_all = "lowercase"))]
219pub enum ArchiveStatus {
220 Active,
222 Archived,
224}
225
226impl ArchiveStatus {
227 #[must_use]
229 pub const fn as_str(self) -> &'static str {
230 match self {
231 Self::Active => "active",
232 Self::Archived => "archived",
233 }
234 }
235}
236
237impl std::str::FromStr for ArchiveStatus {
238 type Err = ();
239
240 fn from_str(value: &str) -> Result<Self, Self::Err> {
241 match value {
242 "active" => Ok(Self::Active),
243 "archived" => Ok(Self::Archived),
244 _ => Err(()),
245 }
246 }
247}
248
249impl std::fmt::Display for ArchiveStatus {
250 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
251 formatter.write_str(self.as_str())
252 }
253}
254
255#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
257#[cfg_attr(feature = "wire", derive(serde::Serialize, serde::Deserialize, schemars::JsonSchema))]
258#[cfg_attr(feature = "wire", serde(rename_all = "lowercase"))]
259pub enum ArchiveListStatus {
260 #[default]
262 Active,
263 Archived,
265 All,
267}
268
269impl ArchiveListStatus {
270 #[must_use]
272 pub const fn as_str(self) -> &'static str {
273 match self {
274 Self::Active => "active",
275 Self::Archived => "archived",
276 Self::All => "all",
277 }
278 }
279}
280
281#[derive(Debug, Clone, Copy, PartialEq, Eq)]
283#[cfg_attr(feature = "wire", derive(serde::Serialize, serde::Deserialize, schemars::JsonSchema))]
284#[cfg_attr(feature = "wire", serde(rename_all = "lowercase"))]
285pub enum UserStatus {
286 Active,
288 Disabled,
290}
291
292impl UserStatus {
293 #[must_use]
295 pub const fn as_str(self) -> &'static str {
296 match self {
297 Self::Active => "active",
298 Self::Disabled => "disabled",
299 }
300 }
301}
302
303impl std::str::FromStr for UserStatus {
304 type Err = ();
305
306 fn from_str(value: &str) -> Result<Self, Self::Err> {
307 match value {
308 "active" => Ok(Self::Active),
309 "disabled" => Ok(Self::Disabled),
310 _ => Err(()),
311 }
312 }
313}
314
315impl std::fmt::Display for UserStatus {
316 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
317 formatter.write_str(self.as_str())
318 }
319}
320
321#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
323#[cfg_attr(feature = "wire", derive(serde::Serialize, serde::Deserialize, schemars::JsonSchema))]
324#[cfg_attr(feature = "wire", serde(rename_all = "lowercase"))]
325pub enum UserListStatus {
326 #[default]
328 Active,
329 Disabled,
331 All,
333}
334
335impl UserListStatus {
336 #[must_use]
338 pub const fn as_str(self) -> &'static str {
339 match self {
340 Self::Active => "active",
341 Self::Disabled => "disabled",
342 Self::All => "all",
343 }
344 }
345}
346
347#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
349#[cfg_attr(feature = "wire", derive(serde::Serialize, serde::Deserialize, schemars::JsonSchema))]
350#[cfg_attr(feature = "wire", serde(rename_all = "lowercase"))]
351pub enum ObjectListOrder {
352 #[default]
354 Created,
355 Updated,
357}
358
359impl ObjectListOrder {
360 #[must_use]
362 pub const fn as_str(self) -> &'static str {
363 match self {
364 Self::Created => "created",
365 Self::Updated => "updated",
366 }
367 }
368}
369
370#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
372#[cfg_attr(feature = "wire", derive(serde::Serialize, serde::Deserialize, schemars::JsonSchema))]
373#[cfg_attr(feature = "wire", serde(rename_all = "lowercase"))]
374pub enum SearchCategory {
375 Title,
377 Body,
379 Metadata,
381}
382
383impl SearchCategory {
384 #[must_use]
386 pub const fn as_str(self) -> &'static str {
387 match self {
388 Self::Title => "title",
389 Self::Body => "body",
390 Self::Metadata => "metadata",
391 }
392 }
393}
394
395impl std::str::FromStr for SearchCategory {
396 type Err = ();
397
398 fn from_str(value: &str) -> Result<Self, Self::Err> {
399 match value {
400 "title" => Ok(Self::Title),
401 "body" => Ok(Self::Body),
402 "metadata" => Ok(Self::Metadata),
403 _ => Err(()),
404 }
405 }
406}
407
408impl std::fmt::Display for SearchCategory {
409 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
410 formatter.write_str(self.as_str())
411 }
412}
413
414#[derive(Debug, Clone, Copy, PartialEq, Eq)]
416#[cfg_attr(feature = "wire", derive(serde::Serialize, serde::Deserialize, schemars::JsonSchema))]
417#[cfg_attr(feature = "wire", serde(rename_all = "snake_case"))]
418pub enum SearchMatchKind {
419 Text,
421 Literal,
423 Exact,
425}
426
427impl SearchMatchKind {
428 #[must_use]
430 pub const fn as_str(self) -> &'static str {
431 match self {
432 Self::Text => "text",
433 Self::Literal => "literal",
434 Self::Exact => "exact",
435 }
436 }
437}
438
439impl std::str::FromStr for SearchMatchKind {
440 type Err = ();
441
442 fn from_str(value: &str) -> Result<Self, Self::Err> {
443 match value {
444 "text" => Ok(Self::Text),
445 "literal" => Ok(Self::Literal),
446 "exact" => Ok(Self::Exact),
447 _ => Err(()),
448 }
449 }
450}
451
452impl std::fmt::Display for SearchMatchKind {
453 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
454 formatter.write_str(self.as_str())
455 }
456}
457
458#[derive(Debug, Clone, Copy, PartialEq, Eq)]
460#[cfg_attr(feature = "wire", derive(serde::Serialize, serde::Deserialize, schemars::JsonSchema))]
461#[cfg_attr(feature = "wire", serde(rename_all = "snake_case"))]
462pub enum CommentStatus {
463 Active,
465 Deleted,
467 Expired,
469}
470
471#[derive(Debug, Clone, Copy, PartialEq, Eq)]
473#[cfg_attr(feature = "wire", derive(serde::Serialize, serde::Deserialize, schemars::JsonSchema))]
474#[cfg_attr(feature = "wire", serde(rename_all = "snake_case"))]
475pub enum SearchMode {
476 Auto,
479 Text,
481 Literal,
483 Exact,
485}
486
487impl SearchMode {
488 #[must_use]
490 pub const fn as_str(self) -> &'static str {
491 match self {
492 Self::Auto => "auto",
493 Self::Text => "text",
494 Self::Literal => "literal",
495 Self::Exact => "exact",
496 }
497 }
498}
499
500#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
502#[cfg_attr(feature = "wire", derive(serde::Serialize, serde::Deserialize, schemars::JsonSchema))]
503#[cfg_attr(feature = "wire", serde(rename_all = "lowercase"))]
504pub enum ObjectGraphDirection {
505 Outgoing,
507 Incoming,
509 #[default]
511 Both,
512}
513
514impl ObjectGraphDirection {
515 #[must_use]
517 pub const fn as_str(self) -> &'static str {
518 match self {
519 Self::Outgoing => "outgoing",
520 Self::Incoming => "incoming",
521 Self::Both => "both",
522 }
523 }
524
525 #[must_use]
527 pub fn parse(value: &str) -> Option<Self> {
528 match value {
529 "outgoing" => Some(Self::Outgoing),
530 "incoming" => Some(Self::Incoming),
531 "both" => Some(Self::Both),
532 _ => None,
533 }
534 }
535}
536
537impl std::fmt::Display for ObjectGraphDirection {
538 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
539 formatter.write_str(self.as_str())
540 }
541}
542
543#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
545#[cfg_attr(feature = "wire", derive(serde::Serialize, serde::Deserialize, schemars::JsonSchema))]
546#[cfg_attr(feature = "wire", serde(rename_all = "lowercase"))]
547pub enum EventOrder {
548 #[default]
550 Asc,
551 Desc,
553}
554
555impl EventOrder {
556 #[must_use]
558 pub const fn as_str(self) -> &'static str {
559 match self {
560 Self::Asc => "asc",
561 Self::Desc => "desc",
562 }
563 }
564}
565
566impl std::str::FromStr for EventOrder {
567 type Err = &'static str;
568
569 fn from_str(value: &str) -> Result<Self, Self::Err> {
570 match value {
571 "asc" => Ok(Self::Asc),
572 "desc" => Ok(Self::Desc),
573 _ => Err("expected `asc` or `desc`"),
574 }
575 }
576}
577
578impl std::fmt::Display for EventOrder {
579 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
580 formatter.write_str(self.as_str())
581 }
582}
583
584#[cfg(test)]
585mod tests {
586 use super::{
587 ApiKeyScope, ArchiveStatus, EventOrder, MembershipRole, ObjectRole, SearchMatchKind,
588 UserStatus,
589 };
590
591 #[test]
592 fn event_order_names_round_trip() {
593 for (value, order) in [("asc", EventOrder::Asc), ("desc", EventOrder::Desc)] {
594 assert_eq!(value.parse(), Ok(order));
595 assert_eq!(order.to_string(), value);
596 }
597 assert!("newest".parse::<EventOrder>().is_err());
598 }
599
600 #[test]
601 fn api_key_scope_names_round_trip() {
602 for scope in ApiKeyScope::ALL {
603 assert_eq!(scope.as_str().parse(), Ok(*scope));
604 }
605 }
606
607 #[test]
608 fn stored_roles_and_statuses_round_trip() {
609 for role in [ObjectRole::Viewer, ObjectRole::Editor, ObjectRole::Admin] {
610 assert_eq!(role.as_str().parse(), Ok(role));
611 }
612 for role in [MembershipRole::Member, MembershipRole::Admin] {
613 assert_eq!(role.as_str().parse(), Ok(role));
614 }
615 for status in [ArchiveStatus::Active, ArchiveStatus::Archived] {
616 assert_eq!(status.as_str().parse(), Ok(status));
617 }
618 for status in [UserStatus::Active, UserStatus::Disabled] {
619 assert_eq!(status.as_str().parse(), Ok(status));
620 }
621 for kind in [SearchMatchKind::Text, SearchMatchKind::Literal, SearchMatchKind::Exact] {
622 assert_eq!(kind.as_str().parse(), Ok(kind));
623 }
624 }
625
626 #[test]
627 fn api_key_write_scopes_include_corresponding_read_scope() {
628 for (write, read) in [
629 (ApiKeyScope::WorkspaceWrite, ApiKeyScope::WorkspaceRead),
630 (ApiKeyScope::ObjectWrite, ApiKeyScope::ObjectRead),
631 (ApiKeyScope::AttachmentWrite, ApiKeyScope::AttachmentRead),
632 (ApiKeyScope::GraphWrite, ApiKeyScope::GraphRead),
633 ] {
634 assert!(write.permits(write));
635 assert!(write.permits(read));
636 assert!(!read.permits(write));
637 }
638 }
639
640 #[test]
641 fn unrelated_api_key_scopes_do_not_imply_each_other() {
642 assert!(!ApiKeyScope::Admin.permits(ApiKeyScope::ObjectRead));
643 assert!(!ApiKeyScope::ObjectWrite.permits(ApiKeyScope::AttachmentRead));
644 assert!(!ApiKeyScope::RealtimeRead.permits(ApiKeyScope::ObjectRead));
645 assert!(!ApiKeyScope::RealtimeRead.permits(ApiKeyScope::EventRead));
646 assert!(!ApiKeyScope::EventRead.permits(ApiKeyScope::RealtimeRead));
647 assert!(!ApiKeyScope::AccessManage.permits(ApiKeyScope::WorkspaceRead));
648 }
649}