diff --git a/supabase-wrappers/src/interface.rs b/supabase-wrappers/src/interface.rs index 733e7009..bccc801c 100644 --- a/supabase-wrappers/src/interface.rs +++ b/supabase-wrappers/src/interface.rs @@ -216,6 +216,23 @@ impl FromDatum for Cell { } } +pub trait CellFormatter { + fn fmt_cell(&mut self, cell: &Cell) -> String; +} + +struct DefaultFormatter {} + +impl DefaultFormatter { + fn new() -> Self { + Self {} + } +} + +impl CellFormatter for DefaultFormatter { + fn fmt_cell(&mut self, cell: &Cell) -> String { + format!("{}", cell) + } +} /// A data row in a table /// /// The row contains a column name list and cell list with same number of @@ -352,13 +369,20 @@ pub struct Qual { impl Qual { pub fn deparse(&self) -> String { + let mut formatter = DefaultFormatter::new(); + self.deparse_with_fmt(&mut formatter) + } + + pub fn deparse_with_fmt(&self, t: &mut T) -> String { if self.use_or { match &self.value { Value::Cell(_) => unreachable!(), Value::Array(cells) => { let conds: Vec = cells .iter() - .map(|cell| format!("{} {} {}", self.field, self.operator, cell)) + .map(|cell| { + format!("{} {} {}", self.field, self.operator, t.fmt_cell(cell)) + }) .collect(); conds.join(" or ") } @@ -370,11 +394,11 @@ impl Qual { Cell::String(cell) if cell == "null" => { format!("{} {} null", self.field, self.operator) } - _ => format!("{} {} {}", self.field, self.operator, cell), + _ => format!("{} {} {}", self.field, self.operator, t.fmt_cell(cell)), }, - "~~" => format!("{} like {}", self.field, cell), - "!~~" => format!("{} not like {}", self.field, cell), - _ => format!("{} {} {}", self.field, self.operator, cell), + "~~" => format!("{} like {}", self.field, t.fmt_cell(cell)), + "!~~" => format!("{} not like {}", self.field, t.fmt_cell(cell)), + _ => format!("{} {} {}", self.field, self.operator, t.fmt_cell(cell)), }, Value::Array(_) => unreachable!(), }