use serde_json::{Map, Value};
use sqlx::{Column, ColumnIndex, Row, TypeInfo, ValueRef};
use crate::cell::{Cell, encode};
pub(crate) trait DecodeCell: Row {
fn decode_cell(&self, idx: usize, type_name: &str) -> Cell;
}
pub(crate) fn row_to_json<R>(row: &R) -> Value
where
R: DecodeCell,
usize: ColumnIndex<R>,
{
let columns = row.columns();
let mut map = Map::with_capacity(columns.len());
for column in columns {
let idx = column.ordinal();
let cell = if row.try_get_raw(idx).is_ok_and(|v| v.is_null()) {
Cell::Null
} else {
row.decode_cell(idx, column.type_info().name())
};
map.insert(column.name().to_string(), encode(cell));
}
Value::Object(map)
}