Trait pgde::RowConsumer
source · pub trait RowConsumer {
// Required method
fn from_row(row: Row) -> Result<Self, (Self, Vec<String>)>
where Self: Sized;
// Provided methods
fn from_rows(rows: Vec<Row>) -> Result<Vec<Self>, Vec<Self>>
where Self: Sized { ... }
fn consume(
conn: &Client,
query: &str,
params: &[&(dyn ToSql + Sync)],
) -> impl Future<Output = Result<Vec<Self>, ConsumeError>> + Send
where Self: Sized { ... }
}Expand description
The derivable trait for implementing PostgreSQL row consumption.
Required Methods§
sourcefn from_row(row: Row) -> Result<Self, (Self, Vec<String>)>where
Self: Sized,
fn from_row(row: Row) -> Result<Self, (Self, Vec<String>)>where
Self: Sized,
The unit row consumer implemented by the pgde_derive crate that consumes row data into another struct. Upon error, provides field and class information for the first encountered error in the form of a String as well as partially converted data.
§Example
Provided the following struct Foo:
#[derive(RowConsumer)]
struct Foo {
Id: i32,
Data: String,
}The pgde_derive crate will implement the following:
impl pgde::RowConsumer for Foo {
fn from_row(row: Row) -> Result<Foo, (Foo, Vec<String>)>
where
Foo: Sized,
{
let mut errors : Vec<String> = Vec::new();
let result = Foo {
Id: match row.try_get::<usize, i32>(0) {
Ok(v) => v,
Err(_) => {
errors.push(format!("Conversion error occurred for field \"{}\" on class \"{}\"", "Id", "Foo"));
i32::default()
},
},
Data: match row.try_get::<usize, String>(1) {
Ok(v) => v,
Err(_) => {
errors.push(format!("Conversion error occurred for field \"{}\" on class \"{}\"", "Data", "Foo"));
String::default()
},
},
};
match errors.first() {
None => Ok(result),
Some(v) => Err((result, v.to_string())),
}
}
}