Skip to content

Commit

Permalink
feat: Add user-defined cell fmt interface
Browse files Browse the repository at this point in the history
  • Loading branch information
kysshsy committed Aug 6, 2024
1 parent 43d17dc commit e96cabe
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions supabase-wrappers/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<T: CellFormatter>(&self, t: &mut T) -> String {
if self.use_or {
match &self.value {
Value::Cell(_) => unreachable!(),
Value::Array(cells) => {
let conds: Vec<String> = 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 ")
}
Expand All @@ -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!(),
}
Expand Down

0 comments on commit e96cabe

Please sign in to comment.