Generates a [`tabled::Table`] from a schema and rows, using the style of a psql table.
(
client: PsqlClient,
schema: &ProductType,
rows: impl Iterator<Item = Result<ProductValue, E>>,
)
| 250 | |
| 251 | /// Generates a [`tabled::Table`] from a schema and rows, using the style of a psql table. |
| 252 | fn build_table<E>( |
| 253 | client: PsqlClient, |
| 254 | schema: &ProductType, |
| 255 | rows: impl Iterator<Item = Result<ProductValue, E>>, |
| 256 | ) -> Result<tabled::Table, E> { |
| 257 | let mut builder = tabled::builder::Builder::default(); |
| 258 | builder.set_header(schema.elements.iter().enumerate().map(|(i, e)| { |
| 259 | e.name |
| 260 | .as_ref() |
| 261 | .map(|n| n.to_string()) |
| 262 | .unwrap_or_else(|| format!("column {i}")) |
| 263 | })); |
| 264 | |
| 265 | let ty = Typespace::EMPTY.with_type(schema); |
| 266 | for row in rows { |
| 267 | let row = row?; |
| 268 | builder.push_record(ty.with_values(&row).enumerate().map(|(idx, value)| { |
| 269 | let ty = satn::PsqlType { |
| 270 | client, |
| 271 | tuple: ty.ty(), |
| 272 | field: &ty.ty().elements[idx], |
| 273 | idx, |
| 274 | }; |
| 275 | |
| 276 | satn::PsqlWrapper { ty, value }.to_string() |
| 277 | })); |
| 278 | } |
| 279 | |
| 280 | let mut table = builder.build(); |
| 281 | table.with(tabled::settings::Style::psql()); |
| 282 | |
| 283 | Ok(table) |
| 284 | } |
| 285 | |
| 286 | #[cfg(test)] |
| 287 | mod tests { |
no test coverage detected
searching dependent graphs…