Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add user-defined cell fmt interface #316

Merged
merged 1 commit into from
Aug 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: Add user-defined cell fmt interface
  • Loading branch information
kysshsy committed Aug 6, 2024
commit e96cabe7859d425bd3ea50c05765989ad1f0880a
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
Loading