use std::collections::HashMap; use diesel::{ pg::{Pg, row::{NamedRow, Row}, QueryableByName, }; /// Internally a HashMap mapping field names to a custom sum type capable of /// deserializing common SQL types. This allows Diesel to load rows without a /// hard-coded structure. pub struct FlexiRow { internal: HashMap, } /// Sum type representing a range of SQL data types. pub enum FlexiField { Text(String), Int(i32), Unknown, } impl QueryableByName for FlexiRow { fn build<'a>(row: &impl NamedRow<'a, Pg>) -> diesel::deserialize::Result { let mut hm: HashMap = HashMap::new(); for i in 0..row.field_count() { if let Some(field) = diesel::row::Row::<'a, Pg>::get(&row, i) { let name = field.field_name().or("Unnamed"); } } diesel::deserialize::Result::Ok(FlexiRow { internal: hm }) } }