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}
44
45#[derive(Debug, Clone)]
47pub enum DateTimePredicate {
48 Equals(chrono::DateTime<chrono::Utc>),
50 After(chrono::DateTime<chrono::Utc>),
52 Before(chrono::DateTime<chrono::Utc>),
54}
55
56#[derive(Debug, Clone)]
58pub enum RecordPredicate {
59 Equals(crate::RecordId),
61}
62
63#[derive(Debug, Clone, Copy)]
65pub enum NullPredicate {
66 IsNone,
68 IsSome,
70}
71
72#[derive(Debug, Clone)]
74pub struct OrderBy {
75 pub field: String,
76 pub direction: SortDirection,
77}
78
79#[derive(Debug, Clone, Serialize)]
84pub struct IdOnlyRecord {
85 pub id: String,
86}
87
88impl<'de> Deserialize<'de> for IdOnlyRecord {
89 fn deserialize<D: serde::Deserializer<'de>>(
90 deserializer: D,
91 ) -> std::result::Result<Self, D::Error> {
92 use serde::de::Error;
93 use serde_json::Value;
94
95 let value = Value::deserialize(deserializer)?;
96 let id = match value {
97 Value::String(s) => crate::row_json::thing_to_id_only(s),
99 Value::Object(map) => match map.get("id") {
100 Some(Value::String(s)) => crate::row_json::thing_to_id_only(s.clone()),
101 Some(Value::Object(inner)) => inner
102 .get("id")
103 .and_then(|v| v.as_str())
104 .map(str::to_string)
105 .ok_or_else(|| D::Error::custom("IdOnlyRecord.id object missing string id"))?,
106 Some(other) => {
107 return Err(D::Error::custom(format!(
108 "IdOnlyRecord.id expected string or {{table,id}} object, got {other}"
109 )));
110 }
111 None => {
112 return Err(D::Error::custom("IdOnlyRecord missing id field"));
113 }
114 },
115 other => {
116 return Err(D::Error::custom(format!(
117 "IdOnlyRecord expected string or object, got {other}"
118 )));
119 }
120 };
121 Ok(IdOnlyRecord { id })
122 }
123}