Skip to content

Commit 410cbe6

Browse files
authored
Merge pull request #316 from kysshsy/cell_fmt
feat: Add user-defined cell fmt interface
2 parents 5558926 + e96cabe commit 410cbe6

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

supabase-wrappers/src/interface.rs

+29-5
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,23 @@ impl FromDatum for Cell {
216216
}
217217
}
218218

219+
pub trait CellFormatter {
220+
fn fmt_cell(&mut self, cell: &Cell) -> String;
221+
}
222+
223+
struct DefaultFormatter {}
224+
225+
impl DefaultFormatter {
226+
fn new() -> Self {
227+
Self {}
228+
}
229+
}
230+
231+
impl CellFormatter for DefaultFormatter {
232+
fn fmt_cell(&mut self, cell: &Cell) -> String {
233+
format!("{}", cell)
234+
}
235+
}
219236
/// A data row in a table
220237
///
221238
/// The row contains a column name list and cell list with same number of
@@ -352,13 +369,20 @@ pub struct Qual {
352369

353370
impl Qual {
354371
pub fn deparse(&self) -> String {
372+
let mut formatter = DefaultFormatter::new();
373+
self.deparse_with_fmt(&mut formatter)
374+
}
375+
376+
pub fn deparse_with_fmt<T: CellFormatter>(&self, t: &mut T) -> String {
355377
if self.use_or {
356378
match &self.value {
357379
Value::Cell(_) => unreachable!(),
358380
Value::Array(cells) => {
359381
let conds: Vec<String> = cells
360382
.iter()
361-
.map(|cell| format!("{} {} {}", self.field, self.operator, cell))
383+
.map(|cell| {
384+
format!("{} {} {}", self.field, self.operator, t.fmt_cell(cell))
385+
})
362386
.collect();
363387
conds.join(" or ")
364388
}
@@ -370,11 +394,11 @@ impl Qual {
370394
Cell::String(cell) if cell == "null" => {
371395
format!("{} {} null", self.field, self.operator)
372396
}
373-
_ => format!("{} {} {}", self.field, self.operator, cell),
397+
_ => format!("{} {} {}", self.field, self.operator, t.fmt_cell(cell)),
374398
},
375-
"~~" => format!("{} like {}", self.field, cell),
376-
"!~~" => format!("{} not like {}", self.field, cell),
377-
_ => format!("{} {} {}", self.field, self.operator, cell),
399+
"~~" => format!("{} like {}", self.field, t.fmt_cell(cell)),
400+
"!~~" => format!("{} not like {}", self.field, t.fmt_cell(cell)),
401+
_ => format!("{} {} {}", self.field, self.operator, t.fmt_cell(cell)),
378402
},
379403
Value::Array(_) => unreachable!(),
380404
}

0 commit comments

Comments
 (0)