I want to read aggregated information from my database in a way that is hard to represent as a table but easy to return as JSONB and, subsequently, easy to parse from JSON to a Rust struct using Serde. The naïve approach is to duplicate the entire struct with the exception of one serde_json::Value field, and then implement conversion of the entire type.
To avoid code duplication, I wanted to use #[from_row(try_from = "JsonVersionOfMyType")] and a custom impl TryFrom<JsonVersionOfMyType> for MyType. However, this requires type Error = serde_json::Error; but then I'm getting
error[E0277]: the trait bound `tokio_postgres::Error: std::convert::From<serde_json::Error>` is not satisfied
--> crates/x/src/x/x.rs:26:35
|
26 | #[derive(Clone, Debug, Serialize, FromRow)]
| ^^^^^^^ the trait `std::convert::From<serde_json::Error>` is not implemented for `tokio_postgres::Error`
|
= note: this error originates in the derive macro `FromRow` (in Nightly builds, run with -Z macro-backtrace for more info)
The problem is that there seems to be no external way to construct tokio_postgres::Error, which means that it can only be generated during communication with the database.
Am I doing something wrong? In that case, maybe you could provide sample code for how to use try_from? I've found no useful information on the Internet.
Or can this feature only be used by code that potentially returns tokio_postgres::Error when converting between two "internal" Rust types? Because that seems like a non-existent use case.
I want to read aggregated information from my database in a way that is hard to represent as a table but easy to return as JSONB and, subsequently, easy to parse from JSON to a Rust struct using Serde. The naïve approach is to duplicate the entire struct with the exception of one
serde_json::Valuefield, and then implement conversion of the entire type.To avoid code duplication, I wanted to use
#[from_row(try_from = "JsonVersionOfMyType")]and a customimpl TryFrom<JsonVersionOfMyType> for MyType. However, this requirestype Error = serde_json::Error;but then I'm gettingThe problem is that there seems to be no external way to construct
tokio_postgres::Error, which means that it can only be generated during communication with the database.Am I doing something wrong? In that case, maybe you could provide sample code for how to use
try_from? I've found no useful information on the Internet.Or can this feature only be used by code that potentially returns
tokio_postgres::Errorwhen converting between two "internal" Rust types? Because that seems like a non-existent use case.