Skip to main content

fiberplane_models/
sorting.rs

1#[cfg(feature = "fp-bindgen")]
2use fp_bindgen::prelude::Serializable;
3use serde::{Deserialize, Serialize};
4use std::borrow::Cow;
5use strum_macros::IntoStaticStr;
6use typed_builder::TypedBuilder;
7
8// Keep this here for backwards compatibility
9#[deprecated(note = "Use `fiberplane_models::paging::Pagination` instead")]
10pub type Pagination = crate::paging::Pagination;
11
12#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq, TypedBuilder)]
13#[non_exhaustive]
14#[serde(rename_all = "snake_case")]
15pub struct Sorting<T: SortField> {
16    #[serde(default = "T::default_sort_field")]
17    #[builder(default_code = "T::default_sort_field()")]
18    pub sort_by: T,
19
20    #[serde(default = "T::default_sort_direction")]
21    #[builder(default_code = "T::default_sort_direction()")]
22    pub sort_direction: SortDirection,
23}
24
25impl<T: SortField> Sorting<T> {
26    /// Create a new `Sorting` instance using the Builder.
27    pub fn new(sort_by: T, sort_direction: SortDirection) -> Self {
28        Self::builder()
29            .sort_by(sort_by)
30            .sort_direction(sort_direction)
31            .build()
32    }
33
34    /// Sort by T and its default sort direction.
35    pub fn sort_by(sort_by: T) -> Self {
36        Self::builder().sort_by(sort_by).build()
37    }
38
39    /// Sort by T and force the sort direction to be ascending.
40    pub fn sort_ascending_by(sort_by: T) -> Self {
41        Self::new(sort_by, SortDirection::Ascending)
42    }
43
44    /// Sort by T and force the sort direction to be descending.
45    pub fn sort_descending_by(sort_by: T) -> Self {
46        Self::new(sort_by, SortDirection::Descending)
47    }
48}
49
50impl<T: SortField> Default for Sorting<T> {
51    fn default() -> Self {
52        Self {
53            sort_by: T::default_sort_field(),
54            sort_direction: T::default_sort_direction(),
55        }
56    }
57}
58
59#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq, IntoStaticStr)]
60#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
61#[cfg_attr(
62    feature = "fp-bindgen",
63    derive(Serializable),
64    fp(rust_module = "fiberplane_models::sorting")
65)]
66#[cfg_attr(
67    feature = "sqlx",
68    derive(sqlx::Type),
69    sqlx(type_name = "sort_direction")
70)]
71#[serde(rename_all = "snake_case")]
72#[strum(serialize_all = "snake_case")]
73pub enum SortDirection {
74    Ascending,
75    Descending,
76}
77
78impl SortDirection {
79    #[inline]
80    pub fn to_sql(&self) -> &'static str {
81        match self {
82            SortDirection::Ascending => "ASC",
83            SortDirection::Descending => "DESC",
84        }
85    }
86}
87
88pub trait SortField {
89    fn default_sort_field() -> Self;
90
91    #[inline]
92    fn default_sort_direction() -> SortDirection {
93        SortDirection::Ascending
94    }
95}
96
97#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq, IntoStaticStr)]
98#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
99#[cfg_attr(
100    feature = "fp-bindgen",
101    derive(Serializable),
102    fp(rust_module = "fiberplane_models::sorting")
103)]
104#[non_exhaustive]
105#[serde(rename_all = "snake_case")]
106#[strum(serialize_all = "snake_case")]
107pub enum TemplateListSortFields {
108    Name,
109    CreatedAt,
110    UpdatedAt,
111}
112
113impl TemplateListSortFields {
114    #[inline]
115    pub fn to_sql(&self) -> &'static str {
116        match self {
117            TemplateListSortFields::Name => "name",
118            TemplateListSortFields::UpdatedAt => "updated_at",
119            TemplateListSortFields::CreatedAt => "created_at",
120        }
121    }
122}
123
124impl SortField for TemplateListSortFields {
125    #[inline]
126    fn default_sort_field() -> Self {
127        Self::Name
128    }
129}
130
131#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq, IntoStaticStr)]
132#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
133#[cfg_attr(
134    feature = "fp-bindgen",
135    derive(Serializable),
136    fp(rust_module = "fiberplane_models::sorting")
137)]
138#[non_exhaustive]
139#[serde(rename_all = "snake_case")]
140#[strum(serialize_all = "snake_case")]
141pub enum SnippetListSortFields {
142    Name,
143    CreatedAt,
144    UpdatedAt,
145}
146
147impl SnippetListSortFields {
148    #[inline]
149    pub fn to_sql(&self) -> &'static str {
150        match self {
151            SnippetListSortFields::Name => "name",
152            SnippetListSortFields::UpdatedAt => "updated_at",
153            SnippetListSortFields::CreatedAt => "created_at",
154        }
155    }
156}
157
158impl SortField for SnippetListSortFields {
159    #[inline]
160    fn default_sort_field() -> Self {
161        Self::Name
162    }
163}
164
165#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq, IntoStaticStr)]
166#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
167#[cfg_attr(
168    feature = "fp-bindgen",
169    derive(Serializable),
170    fp(rust_module = "fiberplane_models::sorting")
171)]
172#[non_exhaustive]
173#[serde(rename_all = "snake_case")]
174#[strum(serialize_all = "snake_case")]
175pub enum EventSortFields {
176    Title,
177    OccurrenceTime,
178    CreatedAt,
179    UpdatedAt,
180}
181
182impl EventSortFields {
183    #[inline]
184    pub fn to_sql(&self) -> &'static str {
185        match self {
186            EventSortFields::Title => "title",
187            EventSortFields::OccurrenceTime => "occurrence_time",
188            EventSortFields::UpdatedAt => "updated_at",
189            EventSortFields::CreatedAt => "created_at",
190        }
191    }
192}
193
194impl SortField for EventSortFields {
195    #[inline]
196    fn default_sort_field() -> Self {
197        Self::OccurrenceTime
198    }
199}
200
201#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq, IntoStaticStr)]
202#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
203#[cfg_attr(
204    feature = "fp-bindgen",
205    derive(Serializable),
206    fp(rust_module = "fiberplane_models::sorting")
207)]
208#[non_exhaustive]
209#[serde(rename_all = "snake_case")]
210#[strum(serialize_all = "snake_case")]
211pub enum TokenListSortFields {
212    Title,
213    CreatedAt,
214    ExpiresAt,
215}
216
217impl TokenListSortFields {
218    #[inline]
219    pub fn to_sql(&self) -> &'static str {
220        match self {
221            TokenListSortFields::Title => "title",
222            TokenListSortFields::CreatedAt => "created_at",
223            TokenListSortFields::ExpiresAt => "expires_at",
224        }
225    }
226}
227
228impl SortField for TokenListSortFields {
229    #[inline]
230    fn default_sort_field() -> Self {
231        Self::Title
232    }
233}
234
235#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq, IntoStaticStr)]
236#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
237#[cfg_attr(
238    feature = "fp-bindgen",
239    derive(Serializable),
240    fp(rust_module = "fiberplane_models::sorting")
241)]
242#[non_exhaustive]
243#[serde(rename_all = "snake_case")]
244#[strum(serialize_all = "snake_case")]
245pub enum WorkspaceMembershipSortFields {
246    Name,
247    Email,
248    JoinedAt,
249}
250
251impl WorkspaceMembershipSortFields {
252    pub fn to_sql(&self, users_table_alias: &'static str) -> Cow<str> {
253        match self {
254            WorkspaceMembershipSortFields::Name => format!("{users_table_alias}.name").into(),
255            WorkspaceMembershipSortFields::Email => format!("{users_table_alias}.email").into(),
256            WorkspaceMembershipSortFields::JoinedAt => "created_at".into(),
257        }
258    }
259}
260
261impl SortField for WorkspaceMembershipSortFields {
262    #[inline]
263    fn default_sort_field() -> Self {
264        Self::Name
265    }
266}
267
268#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq, IntoStaticStr)]
269#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
270#[cfg_attr(
271    feature = "fp-bindgen",
272    derive(Serializable),
273    fp(rust_module = "fiberplane_models::sorting")
274)]
275#[non_exhaustive]
276#[serde(rename_all = "snake_case")]
277#[strum(serialize_all = "snake_case")]
278pub enum WorkspaceListingSortFields {
279    Name,
280    Type,
281    JoinedAt,
282}
283
284impl WorkspaceListingSortFields {
285    pub fn to_sql(&self) -> &'static str {
286        match self {
287            WorkspaceListingSortFields::Name => "name",
288            WorkspaceListingSortFields::Type => "ty",
289            WorkspaceListingSortFields::JoinedAt => "created_at",
290        }
291    }
292}
293
294impl SortField for WorkspaceListingSortFields {
295    #[inline]
296    fn default_sort_field() -> Self {
297        Self::Name
298    }
299}
300
301#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq, IntoStaticStr)]
302#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
303#[cfg_attr(
304    feature = "fp-bindgen",
305    derive(Serializable),
306    fp(rust_module = "fiberplane_models::sorting")
307)]
308#[non_exhaustive]
309#[serde(rename_all = "snake_case")]
310#[strum(serialize_all = "snake_case")]
311pub enum WorkspaceInviteListingSortFields {
312    Id,
313    Sender,
314    Receiver,
315    CreatedAt,
316    ExpiresAt,
317}
318
319impl WorkspaceInviteListingSortFields {
320    pub fn to_sql(&self) -> &'static str {
321        match self {
322            WorkspaceInviteListingSortFields::Id => "id",
323            WorkspaceInviteListingSortFields::Sender => "sender",
324            WorkspaceInviteListingSortFields::Receiver => "receiver",
325            WorkspaceInviteListingSortFields::CreatedAt => "created_at",
326            WorkspaceInviteListingSortFields::ExpiresAt => "expires_at",
327        }
328    }
329}
330
331impl SortField for WorkspaceInviteListingSortFields {
332    #[inline]
333    fn default_sort_field() -> Self {
334        Self::Id
335    }
336}
337
338#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq, IntoStaticStr)]
339#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
340#[cfg_attr(
341    feature = "fp-bindgen",
342    derive(Serializable),
343    fp(rust_module = "fiberplane_models::sorting")
344)]
345#[non_exhaustive]
346#[serde(rename_all = "snake_case")]
347#[strum(serialize_all = "snake_case")]
348pub enum ProxyListingSortFields {
349    Id,
350    Name,
351    CreatedAt,
352    UpdatedAt,
353    Status,
354}
355
356impl ProxyListingSortFields {
357    pub fn to_sql(&self) -> &'static str {
358        match self {
359            ProxyListingSortFields::Id => "id",
360            ProxyListingSortFields::Name => "name",
361            ProxyListingSortFields::CreatedAt => "created_at",
362            ProxyListingSortFields::UpdatedAt => "updated_at",
363            ProxyListingSortFields::Status => "status",
364        }
365    }
366}
367
368impl SortField for ProxyListingSortFields {
369    #[inline]
370    fn default_sort_field() -> Self {
371        Self::Name
372    }
373}
374
375#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq, IntoStaticStr)]
376#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
377#[cfg_attr(
378    feature = "fp-bindgen",
379    derive(Serializable),
380    fp(rust_module = "fiberplane_models::sorting")
381)]
382#[non_exhaustive]
383#[serde(rename_all = "snake_case")]
384#[strum(serialize_all = "snake_case")]
385pub enum DataSourceListingSortFields {
386    Id,
387    Name,
388    ProxyName,
389    CreatedAt,
390    UpdatedAt,
391    Status,
392}
393
394impl DataSourceListingSortFields {
395    pub fn to_sql(&self) -> &'static str {
396        match self {
397            DataSourceListingSortFields::Id => "id",
398            DataSourceListingSortFields::Name => "name",
399            DataSourceListingSortFields::ProxyName => "proxy_name",
400            DataSourceListingSortFields::CreatedAt => "created_at",
401            DataSourceListingSortFields::UpdatedAt => "updated_at",
402            DataSourceListingSortFields::Status => "status",
403        }
404    }
405}
406
407impl SortField for DataSourceListingSortFields {
408    #[inline]
409    fn default_sort_field() -> Self {
410        Self::Name
411    }
412}
413
414#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq, IntoStaticStr)]
415#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
416#[cfg_attr(
417    feature = "fp-bindgen",
418    derive(Serializable),
419    fp(rust_module = "fiberplane_models::sorting")
420)]
421#[non_exhaustive]
422#[serde(rename_all = "snake_case")]
423#[strum(serialize_all = "snake_case")]
424pub enum ViewSortFields {
425    Name,
426    DisplayName,
427    Description,
428    Labels,
429    CreatedAt,
430    UpdatedAt,
431}
432
433impl ViewSortFields {
434    #[inline]
435    pub fn to_sql(&self) -> &'static str {
436        match self {
437            ViewSortFields::Name => "name",
438            ViewSortFields::DisplayName => "display_name",
439            ViewSortFields::Description => "description",
440            ViewSortFields::Labels => "labels",
441            ViewSortFields::CreatedAt => "created_at",
442            ViewSortFields::UpdatedAt => "updated_at",
443        }
444    }
445}
446
447impl SortField for ViewSortFields {
448    #[inline]
449    fn default_sort_field() -> Self {
450        Self::DisplayName
451    }
452}
453
454#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq, IntoStaticStr)]
455#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
456#[cfg_attr(
457    feature = "fp-bindgen",
458    derive(Serializable),
459    fp(rust_module = "fiberplane_models::sorting")
460)]
461#[cfg_attr(
462    feature = "sqlx",
463    derive(sqlx::Type),
464    sqlx(type_name = "notebook_sort_fields")
465)]
466#[serde(rename_all = "snake_case")]
467#[strum(serialize_all = "snake_case")]
468pub enum NotebookSortFields {
469    Title,
470    CreatedAt,
471    UpdatedAt,
472}
473
474impl NotebookSortFields {
475    #[inline]
476    pub fn to_sql(&self) -> &'static str {
477        match self {
478            NotebookSortFields::Title => "title",
479            NotebookSortFields::CreatedAt => "created_at",
480            NotebookSortFields::UpdatedAt => "updated_at",
481        }
482    }
483}
484
485impl SortField for NotebookSortFields {
486    #[inline]
487    fn default_sort_field() -> Self {
488        Self::UpdatedAt
489    }
490}