stucco_core/collection/mod.rs
1//! Collection presentation state, independent of storage and HTTP.
2mod cursor;
3mod filter;
4mod query;
5pub use cursor::Cursor;
6pub use filter::{ColumnKind, ColumnSpec, Filter};
7pub use query::CollectionQuery;
8
9/// Sort direction.
10#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
11pub enum Direction {
12 /// Ascending order.
13 #[default]
14 Asc,
15 /// Descending order.
16 Desc,
17}
18impl Direction {
19 /// Query representation.
20 pub fn as_str(self) -> &'static str {
21 match self {
22 Self::Asc => "asc",
23 Self::Desc => "desc",
24 }
25 }
26 /// The opposite direction.
27 pub fn reversed(self) -> Self {
28 match self {
29 Self::Asc => Self::Desc,
30 Self::Desc => Self::Asc,
31 }
32 }
33}
34/// Position within a collection.
35#[derive(Clone, Debug, PartialEq, Eq)]
36pub enum Window {
37 /// One-based numbered page.
38 Offset {
39 /// Page number.
40 page: u64,
41 },
42 /// Records after this exclusive anchor.
43 After(Cursor),
44 /// Records before this exclusive anchor.
45 Before(Cursor),
46}
47impl Default for Window {
48 fn default() -> Self {
49 Self::Offset { page: 1 }
50 }
51}
52/// Operations supported by a data source.
53#[derive(Clone, Debug, Default, PartialEq, Eq)]
54pub struct Capabilities {
55 /// Sortable column keys.
56 pub sortable: Vec<String>,
57 /// Filterable column keys.
58 pub filterable: Vec<String>,
59 /// Whether shared text search is supported.
60 pub searchable: bool,
61 /// Whether filtered totals can be computed.
62 pub total_count: bool,
63 /// Whether numbered offsets are supported.
64 pub offset: bool,
65}
66/// A loaded collection: the query parsed from a request, the backend's
67/// capabilities, and the page of rows they produced. Hand it to a data
68/// table, which needs all three.
69#[derive(Clone, Debug, PartialEq)]
70pub struct Collection<T> {
71 /// The query parsed from the request.
72 pub query: CollectionQuery,
73 /// What the backend supports.
74 pub capabilities: Capabilities,
75 /// The rows the query produced.
76 pub page: CollectionPage<T>,
77}
78/// Owned rows and navigation metadata from one query.
79#[derive(Clone, Debug, PartialEq, Eq)]
80pub struct CollectionPage<T> {
81 /// Rows in display order.
82 pub rows: Vec<T>,
83 /// Next-page cursor.
84 pub next: Option<Cursor>,
85 /// Previous-page cursor.
86 pub prev: Option<Cursor>,
87 /// Filtered count, if supported.
88 pub total: Option<u64>,
89}