Crates.io | tiberius-mappers |
lib.rs | tiberius-mappers |
version | 0.6.0 |
source | src |
created_at | 2023-10-01 22:20:29.497286 |
updated_at | 2023-12-14 17:59:13.922745 |
description | Row mappers for Tiberius |
homepage | |
repository | https://github.com/stevewillcock/tiberius-mappers |
max_upload_size | |
id | 989480 |
size | 6,500 |
Row mappers for the Tiberius SQL Server driver.
See the published crate and the documentation for more information.
TryFromRow
trait for tiberius::Row
TryFromRow
traits for structs via the tiberius-mappers-derive crateThe existing tiberius-derive crate currently offers more options for mapping, but does not seem to be maintained and doesn't work with newer versions of Tiberius. I have been maintaining a fork of this crate to support newer versions of Tiberius in internal builds, but I wanted to start from scratch with a simpler implementation. Note that this implementation is based on the original tiberius-derive crate, so credit to the original authors for the idea and some of the code.
This is a work in progress. Currently, the TryFromRow
mapper is implemented.
use tiberius_mappers::TryFromRow;
#[derive(TryFromRow)] // Derive the FromRow trait on our struct
pub struct Customer {
pub id: i32,
pub first_name: String,
pub last_name: String,
pub description: Option<String>,
}
pub async fn print_customers(rows: Vec<tiberius::Row>) -> Result<(), Box<dyn std::error::Error>> {
// Now we can call the try_from_row method on each row to get a Customer struct
let customers: Vec<Customer> = rows
.into_iter()
.map(Customer::try_from_row).collect::<Result<Vec<Customer>, _>>()?;
for customer in customers {
println!("Customer: {} - {:?} - {:?}", customer.id, customer.first_name, customer.last_name);
}
Ok(())
}