supabase_client_graphql/
order.rs1use serde_json::{json, Value};
2
3#[derive(Debug, Clone, PartialEq)]
7pub enum OrderByDirection {
8 AscNullsFirst,
9 AscNullsLast,
10 DescNullsFirst,
11 DescNullsLast,
12}
13
14impl OrderByDirection {
15 pub fn as_graphql_literal(&self) -> &'static str {
17 match self {
18 OrderByDirection::AscNullsFirst => "AscNullsFirst",
19 OrderByDirection::AscNullsLast => "AscNullsLast",
20 OrderByDirection::DescNullsFirst => "DescNullsFirst",
21 OrderByDirection::DescNullsLast => "DescNullsLast",
22 }
23 }
24}
25
26#[derive(Debug, Clone)]
28pub struct OrderByEntry {
29 pub column: String,
30 pub direction: OrderByDirection,
31}
32
33impl OrderByEntry {
34 pub fn to_value(&self) -> Value {
39 json!({ self.column.as_str(): self.direction.as_graphql_literal() })
40 }
41}
42
43#[cfg(test)]
44mod tests {
45 use super::*;
46
47 #[test]
48 fn order_by_asc_nulls_last() {
49 let entry = OrderByEntry {
50 column: "createdAt".into(),
51 direction: OrderByDirection::AscNullsLast,
52 };
53 assert_eq!(entry.to_value(), json!({"createdAt": "AscNullsLast"}));
54 }
55
56 #[test]
57 fn order_by_desc_nulls_first() {
58 let entry = OrderByEntry {
59 column: "name".into(),
60 direction: OrderByDirection::DescNullsFirst,
61 };
62 assert_eq!(entry.to_value(), json!({"name": "DescNullsFirst"}));
63 }
64
65 #[test]
66 fn graphql_literal_values() {
67 assert_eq!(OrderByDirection::AscNullsFirst.as_graphql_literal(), "AscNullsFirst");
68 assert_eq!(OrderByDirection::AscNullsLast.as_graphql_literal(), "AscNullsLast");
69 assert_eq!(OrderByDirection::DescNullsFirst.as_graphql_literal(), "DescNullsFirst");
70 assert_eq!(OrderByDirection::DescNullsLast.as_graphql_literal(), "DescNullsLast");
71 }
72}