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