perspective_client/config/
filters.rs1use std::fmt::Display;
14
15use itertools::Itertools;
16use serde::{Deserialize, Serialize};
17use ts_rs::TS;
18
19use crate::proto;
20use crate::proto::scalar;
21
22#[derive(Clone, Default, Deserialize, Debug, PartialEq, Serialize, TS)]
25#[serde(untagged)]
26pub enum Scalar {
27 Float(f64),
28 String(String),
29 Bool(bool),
30 #[default]
34 Null,
35}
36
37impl From<&str> for Scalar {
38 fn from(value: &str) -> Self {
39 Self::String(value.into())
40 }
41}
42
43impl Display for Scalar {
44 fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
45 match self {
46 Self::Float(x) => write!(fmt, "{x}"),
47 Self::String(x) => write!(fmt, "{x}"),
48 Self::Bool(x) => write!(fmt, "{x}"),
49 Self::Null => write!(fmt, ""),
50 }
51 }
52}
53
54#[derive(Clone, Deserialize, Debug, PartialEq, Serialize, TS)]
55#[serde(untagged)]
56pub enum FilterTerm {
57 Array(Vec<Scalar>),
58 Scalar(#[serde(default)] Scalar),
59}
60
61impl<'a, T> From<T> for FilterTerm
62where
63 T: AsRef<[&'a str]>,
64{
65 fn from(value: T) -> Self {
66 Self::Array(value.as_ref().iter().map(|x| (*x).into()).collect())
67 }
68}
69
70impl Default for FilterTerm {
71 fn default() -> Self {
72 Self::Scalar(Scalar::Null)
73 }
74}
75
76impl Display for FilterTerm {
77 fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
78 match self {
79 Self::Scalar(x) => {
80 write!(fmt, "{x}")?;
81 },
82 Self::Array(xs) => write!(
83 fmt,
84 "{}",
85 Itertools::intersperse(xs.iter().map(|x| format!("{x}")), ",".to_owned())
86 .collect::<String>()
87 )?,
88 }
89
90 Ok(())
91 }
92}
93
94impl FilterTerm {
95 pub fn is_null(&self) -> bool {
96 matches!(self, FilterTerm::Scalar(Scalar::Null))
97 }
98}
99
100#[derive(Clone, Deserialize, Debug, PartialEq, Serialize, TS)]
101#[serde()]
102pub struct Filter(String, String, #[serde(default)] FilterTerm);
103
104impl Filter {
105 pub fn new<T>(column: &str, op: &str, term: T) -> Self
106 where
107 FilterTerm: From<T>,
108 {
109 Filter(column.to_string(), op.to_string(), term.into())
110 }
111
112 pub fn scalar(column: &str, op: &str, value: Scalar) -> Self {
114 Filter(
115 column.to_string(),
116 op.to_string(),
117 FilterTerm::Scalar(value),
118 )
119 }
120
121 pub fn column(&self) -> &str {
122 self.0.as_str()
123 }
124
125 pub fn op(&self) -> &str {
126 self.1.as_str()
127 }
128
129 pub fn term(&self) -> &FilterTerm {
130 &self.2
131 }
132
133 pub fn column_mut(&mut self) -> &mut String {
134 &mut self.0
135 }
136
137 pub fn op_mut(&mut self) -> &mut String {
138 &mut self.1
139 }
140
141 pub fn term_mut(&mut self) -> &mut FilterTerm {
142 &mut self.2
143 }
144}
145
146#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq, Eq, TS)]
147pub enum FilterReducer {
148 #[default]
149 #[serde(rename = "and")]
150 And,
151 #[serde(rename = "or")]
152 Or,
153}
154
155impl From<Scalar> for proto::Scalar {
156 fn from(value: Scalar) -> Self {
157 match value {
158 Scalar::Float(x) => proto::Scalar {
159 scalar: Some(scalar::Scalar::Float(x)),
160 },
161 Scalar::String(x) => proto::Scalar {
162 scalar: Some(scalar::Scalar::String(x)),
163 },
164 Scalar::Bool(x) => proto::Scalar {
165 scalar: Some(scalar::Scalar::Bool(x)),
166 },
167 Scalar::Null => proto::Scalar {
168 scalar: Some(scalar::Scalar::Null(0)),
169 },
170 }
171 }
172}
173
174impl From<proto::Scalar> for Scalar {
175 fn from(value: proto::Scalar) -> Self {
176 match value.scalar {
177 Some(scalar::Scalar::Bool(x)) => Scalar::Bool(x),
178 Some(scalar::Scalar::String(x)) => Scalar::String(x),
179 Some(scalar::Scalar::Float(x)) => Scalar::Float(x),
180 Some(scalar::Scalar::Null(_)) => Scalar::Null,
181 None => Scalar::Null,
182 }
183 }
184}
185
186impl From<Filter> for proto::view_config::Filter {
187 fn from(value: Filter) -> Self {
188 proto::view_config::Filter {
189 column: value.0,
190 op: value.1,
191 value: match value.2 {
192 FilterTerm::Scalar(x) => vec![x.into()],
193 FilterTerm::Array(x) => x.into_iter().map(|x| x.into()).collect(),
194 },
195 }
196 }
197}
198
199impl From<proto::view_config::Filter> for Filter {
200 fn from(value: proto::view_config::Filter) -> Self {
201 Filter(
202 value.column,
203 value.op,
204 if value.value.len() == 1 {
205 FilterTerm::Scalar(value.value.into_iter().next().unwrap().into())
206 } else {
207 FilterTerm::Array(value.value.into_iter().map(|x| x.into()).collect())
208 },
209 )
210 }
211}