valence_core/query/
predicates.rs1use serde::{Deserialize, Serialize};
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum SortDirection {
11 Asc,
13 Desc,
15}
16
17#[derive(Debug, Clone)]
19pub enum IntPredicate {
20 Equals(i64),
22 GreaterThan(i64),
24 GreaterThanOrEqual(i64),
26 LessThan(i64),
28 LessThanOrEqual(i64),
30}
31
32#[derive(Debug, Clone)]
34pub enum StringPredicate {
35 Equals(String),
37 Contains(String),
39 StartsWith(String),
41 EndsWith(String),
43 LessThan(String),
45}
46
47#[derive(Debug, Clone)]
49pub enum DateTimePredicate {
50 Equals(chrono::DateTime<chrono::Utc>),
52 After(chrono::DateTime<chrono::Utc>),
54 Before(chrono::DateTime<chrono::Utc>),
56}
57
58#[derive(Debug, Clone)]
60pub enum RecordPredicate {
61 Equals(crate::RecordId),
63}
64
65#[derive(Debug, Clone, Copy)]
67pub enum NullPredicate {
68 IsNone,
70 IsSome,
72}
73
74#[derive(Debug, Clone)]
76pub struct OrderBy {
77 pub field: String,
78 pub direction: SortDirection,
79}
80
81#[derive(Debug, Clone, Serialize)]
86pub struct IdOnlyRecord {
87 pub id: String,
88}
89
90impl<'de> Deserialize<'de> for IdOnlyRecord {
91 fn deserialize<D: serde::Deserializer<'de>>(
92 deserializer: D,
93 ) -> std::result::Result<Self, D::Error> {
94 use serde::de::Error;
95 use serde_json::Value;
96
97 let value = Value::deserialize(deserializer)?;
98 let id = match value {
99 Value::String(s) => crate::row_json::thing_to_id_only(s),
101 Value::Object(map) => match map.get("id") {
102 Some(Value::String(s)) => crate::row_json::thing_to_id_only(s.clone()),
103 Some(Value::Object(inner)) => inner
104 .get("id")
105 .and_then(|v| v.as_str())
106 .map(str::to_string)
107 .ok_or_else(|| D::Error::custom("IdOnlyRecord.id object missing string id"))?,
108 Some(other) => {
109 return Err(D::Error::custom(format!(
110 "IdOnlyRecord.id expected string or {{table,id}} object, got {other}"
111 )));
112 }
113 None => {
114 return Err(D::Error::custom("IdOnlyRecord missing id field"));
115 }
116 },
117 other => {
118 return Err(D::Error::custom(format!(
119 "IdOnlyRecord expected string or object, got {other}"
120 )));
121 }
122 };
123 Ok(IdOnlyRecord { id })
124 }
125}