-
-
Notifications
You must be signed in to change notification settings - Fork 851
Closed
Labels
Description
I'm trying to write a custom Serialize
implementation for the results of a database query, where I do not have the table structure upfront (I cannot just use a struct
and #[derive(Serialize)]
).
I want to write the output in csv, so I'm using the csv
crate. Unfortunately csv
doesn't support serializing maps (BurntSushi/rust-csv#98), so I'm trying to use serialize_struct
. Unfortunately, serialize_field
requires the key to be &'static
, but the key is the column name which comes from the database at runtime and therefore is not static. I don't see any particular reason that serialize_field
must have a static lifetime on the key (I guess performance), is there a way around this?
This is my Serialize
implementation thus far:
impl<T> Serialize for ScrubbableRow<T>
where
T: Scrubber,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let cols = self.row.columns();
let mut state = serializer.serialize_struct(Some(cols.len()))?;
for col in cols {
state.serialize_field(
col.name(), // Uhoh
&T::scrub_column(&self.row, col).map_err(serde::ser::Error::custom)?,
)?;
}
state.end()
}
}
charrondev, olanod, wiiznokes, max-heller and GyulyVGC